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

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀機(jī)器學(xué)習(xí):形如拋物線的散點(diǎn)圖在python和R中的非線性回歸擬合方法
機(jī)器學(xué)習(xí):形如拋物線的散點(diǎn)圖在python和R中的非線性回歸擬合方法
2017-04-28
收藏

機(jī)器學(xué)習(xí):形如拋物線的散點(diǎn)圖python和R中的非線性回歸擬合方法

對(duì)于樣本數(shù)據(jù)的散點(diǎn)圖形如函數(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è)置樣本數(shù)據(jù),真實(shí)數(shù)據(jù)需要在這里處理
'''
##樣本數(shù)據(jù)(Xi,Yi),需要轉(zhuǎn)換成數(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ù)
    函數(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中是一一對(duì)應(yīng)的
def error(p,x,y):
    return func(p,x)-y

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

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

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

#讀取結(jié)果
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默認(rèn)不支持中文,label設(shè)置中文的話需要另行設(shè)置
   如果報(bào)錯(cuò),改成英文就可以
'''

#畫樣本點(diǎn)
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個(gè)連續(xù)點(diǎn)
y=a*x*x+b*x+c ##函數(shù)式
plt.plot(x,y,color="red",label="擬合直線",linewidth=2)
plt.legend() #繪制圖例
plt.show()
 運(yùn)行結(jié)果:
a= 2.06607141425 b= 2.5975001036 c= 4.68999985496
cost:1
求解的擬合直線為:
y=2.07x*x+2.6x+4.68999985496

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

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

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(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(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".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 = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+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); }