99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀機器學習:形如拋物線的散點圖在python和R中的非線性回歸擬合方法
機器學習:形如拋物線的散點圖在python和R中的非線性回歸擬合方法
2017-04-28
收藏

機器學習:形如拋物線的散點圖python和R中的非線性回歸擬合方法

對于樣本數(shù)據(jù)的散點圖形如函數(shù)y=ax2+bx+c的圖像的數(shù)據(jù), 在python中的擬合過程為:
##最小二乘
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from scipy.optimize import leastsq

'''
     設置樣本數(shù)據(jù),真實數(shù)據(jù)需要在這里處理
'''
##樣本數(shù)據(jù)(Xi,Yi),需要轉換成數(shù)組(列表)形式
Xi=np.array([1,2,3,4,5,6])
#Yi=np.array([9,18,31,48,69,94])
Yi=np.array([9.1,18.3,32,47,69.5,94.8])

'''
    設定擬合函數(shù)和偏差函數(shù)
    函數(shù)的形狀確定過程:
    1.先畫樣本圖像
    2.根據(jù)樣本圖像大致形狀確定函數(shù)形式(直線、拋物線、正弦余弦等)
'''

##需要擬合的函數(shù)func :指定函數(shù)的形狀
def func(p,x):
    a,b,c=p
    return a*x*x+b*x+c

##偏差函數(shù):x,y都是列表:這里的x,y更上面的Xi,Yi中是一一對應的
def error(p,x,y):
    return func(p,x)-y

'''
    主要部分:附帶部分說明
    1.leastsq函數(shù)的返回值tuple,第一個元素是求解結果,第二個是求解的代價值(個人理解)
    2.官網(wǎng)的原話(第二個值):Value of the cost function at the solution
    3.實例:Para=>(array([ 0.61349535,  1.79409255]), 3)
    4.返回值元組中第一個值的數(shù)量跟需要求解的參數(shù)的數(shù)量一致
'''

#k,b的初始值,可以任意設定,經(jīng)過幾次試驗,發(fā)現(xiàn)p0的值會影響cost的值:Para[1]
p0=[10,10,10]

#把error函數(shù)中除了p0以外的參數(shù)打包到args中(使用要求)
Para=leastsq(error,p0,args=(Xi,Yi))

#讀取結果
a,b,c=Para[0]
print("a=",a,"b=",b,"c=",c)
print("cost:"+str(Para[1]))
print("求解的擬合直線為:")
print("y="+str(round(a,2))+"x*x+"+str(round(b,2))+"x+"+str(c))

'''
   繪圖,看擬合效果.
   matplotlib默認不支持中文,label設置中文的話需要另行設置
   如果報錯,改成英文就可以
'''

#畫樣本點
plt.figure(figsize=(8,6)) ##指定圖像比例: 8:6
plt.scatter(Xi,Yi,color="green",label="樣本數(shù)據(jù)",linewidth=2)

#畫擬合直線
x=np.linspace(0,12,100) ##在0-15直接畫100個連續(xù)點
y=a*x*x+b*x+c ##函數(shù)式
plt.plot(x,y,color="red",label="擬合直線",linewidth=2)
plt.legend() #繪制圖例
plt.show()
 運行結果:
a= 2.06607141425 b= 2.5975001036 c= 4.68999985496
cost:1
求解的擬合直線為:
y=2.07x*x+2.6x+4.68999985496

 在R中的擬合過程:(在控制臺直接敲入或者放入腳本都可以) 
###設置函數(shù)形式
func<-function(a,b,c){
a*x*x+b*x+c
}
###設置樣本數(shù)據(jù)
x<-c(1,2,3,4,5,6)
y<-c(9.1,18.3,32,47,69.5,94.8)
###把樣本數(shù)據(jù)轉換為符合nls函數(shù)需要的格式
d<-data.frame(y,x)
###執(zhí)行求解過程:如果x,y值完全一一對應,匯報錯誤(循環(huán)次數(shù)超過了50這個最大值)
nlmod<-nls(y ~ func(a1,b1,c1),data=d,start=list(a1=1,b1=1,c1=1),trace=F)
###分析結果
summary(nlmod)
 運行結果:

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師考試動態(tài)
數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }