python 機(jī)器學(xué)習(xí)之支持向量機(jī)非線性回歸SVR模型
本文介紹了python 支持向量機(jī)非線性回歸SVR模型,廢話不多說,具體如下:
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm from sklearn.model_selection import train_test_split def load_data_regression(): ''' 加載用于回歸問題的數(shù)據(jù)集 ''' diabetes = datasets.load_diabetes() #使用 scikit-learn 自帶的一個(gè)糖尿病病人的數(shù)據(jù)集 # 拆分成訓(xùn)練集和測(cè)試集,測(cè)試集大小為原始數(shù)據(jù)集大小的 1/4 return train_test_split(diabetes.data,diabetes.target,test_size=0.25,random_state=0) #支持向量機(jī)非線性回歸SVR模型 def test_SVR_linear(*data): X_train,X_test,y_train,y_test=data regr=svm.SVR(kernel='linear') regr.fit(X_train,y_train) print('Coefficients:%s, intercept %s'%(regr.coef_,regr.intercept_)) print('Score: %.2f' % regr.score(X_test, y_test)) # 生成用于回歸問題的數(shù)據(jù)集 X_train,X_test,y_train,y_test=load_data_regression() # 調(diào)用 test_LinearSVR test_SVR_linear(X_train,X_test,y_train,y_test)
def test_SVR_poly(*data): ''' 測(cè)試 多項(xiàng)式核的 SVR 的預(yù)測(cè)性能隨 degree、gamma、coef0 的影響. ''' X_train,X_test,y_train,y_test=data fig=plt.figure() ### 測(cè)試 degree #### degrees=range(1,20) train_scores=[] test_scores=[] for degree in degrees: regr=svm.SVR(kernel='poly',degree=degree,coef0=1) regr.fit(X_train,y_train) train_scores.append(regr.score(X_train,y_train)) test_scores.append(regr.score(X_test, y_test)) ax=fig.add_subplot(1,3,1) ax.plot(degrees,train_scores,label="Training score ",marker='+' ) ax.plot(degrees,test_scores,label= " Testing score ",marker='o' ) ax.set_title( "SVR_poly_degree r=1") ax.set_xlabel("p") ax.set_ylabel("score") ax.set_ylim(-1,1.) ax.legend(loc="best",framealpha=0.5) ### 測(cè)試 gamma,固定 degree為3, coef0 為 1 #### gammas=range(1,40) train_scores=[] test_scores=[] for gamma in gammas: regr=svm.SVR(kernel='poly',gamma=gamma,degree=3,coef0=1) regr.fit(X_train,y_train) train_scores.append(regr.score(X_train,y_train)) test_scores.append(regr.score(X_test, y_test)) ax=fig.add_subplot(1,3,2) ax.plot(gammas,train_scores,label="Training score ",marker='+' ) ax.plot(gammas,test_scores,label= " Testing score ",marker='o' ) ax.set_title( "SVR_poly_gamma r=1") ax.set_xlabel(r"$\gamma$") ax.set_ylabel("score") ax.set_ylim(-1,1) ax.legend(loc="best",framealpha=0.5) ### 測(cè)試 r,固定 gamma 為 20,degree為 3 ###### rs=range(0,20) train_scores=[] test_scores=[] for r in rs: regr=svm.SVR(kernel='poly',gamma=20,degree=3,coef0=r) regr.fit(X_train,y_train) train_scores.append(regr.score(X_train,y_train)) test_scores.append(regr.score(X_test, y_test)) ax=fig.add_subplot(1,3,3) ax.plot(rs,train_scores,label="Training score ",marker='+' ) ax.plot(rs,test_scores,label= " Testing score ",marker='o' ) ax.set_title( "SVR_poly_r gamma=20 degree=3") ax.set_xlabel(r"r") ax.set_ylabel("score") ax.set_ylim(-1,1.) ax.legend(loc="best",framealpha=0.5) plt.show() # 調(diào)用 test_SVR_poly test_SVR_poly(X_train,X_test,y_train,y_test)
def test_SVR_rbf(*data): ''' 測(cè)試 高斯核的 SVR 的預(yù)測(cè)性能隨 gamma 參數(shù)的影響 ''' X_train,X_test,y_train,y_test=data gammas=range(1,20) train_scores=[] test_scores=[] for gamma in gammas: regr=svm.SVR(kernel='rbf',gamma=gamma) regr.fit(X_train,y_train) train_scores.append(regr.score(X_train,y_train)) test_scores.append(regr.score(X_test, y_test)) fig=plt.figure() ax=fig.add_subplot(1,1,1) ax.plot(gammas,train_scores,label="Training score ",marker='+' ) ax.plot(gammas,test_scores,label= " Testing score ",marker='o' ) ax.set_title( "SVR_rbf") ax.set_xlabel(r"$\gamma$") ax.set_ylabel("score") ax.set_ylim(-1,1) ax.legend(loc="best",framealpha=0.5) plt.show() # 調(diào)用 test_SVR_rbf test_SVR_rbf(X_train,X_test,y_train,y_test)
def test_SVR_sigmoid(*data): ''' 測(cè)試 sigmoid 核的 SVR 的預(yù)測(cè)性能隨 gamma、coef0 的影響. ''' X_train,X_test,y_train,y_test=data fig=plt.figure() ### 測(cè)試 gammam,固定 coef0 為 0.01 #### gammas=np.logspace(-1,3) train_scores=[] test_scores=[] for gamma in gammas: regr=svm.SVR(kernel='sigmoid',gamma=gamma,coef0=0.01) regr.fit(X_train,y_train) train_scores.append(regr.score(X_train,y_train)) test_scores.append(regr.score(X_test, y_test)) ax=fig.add_subplot(1,2,1) ax.plot(gammas,train_scores,label="Training score ",marker='+' ) ax.plot(gammas,test_scores,label= " Testing score ",marker='o' ) ax.set_title( "SVR_sigmoid_gamma r=0.01") ax.set_xscale("log") ax.set_xlabel(r"$\gamma$") ax.set_ylabel("score") ax.set_ylim(-1,1) ax.legend(loc="best",framealpha=0.5) ### 測(cè)試 r ,固定 gamma 為 10 ###### rs=np.linspace(0,5) train_scores=[] test_scores=[] for r in rs: regr=svm.SVR(kernel='sigmoid',coef0=r,gamma=10) regr.fit(X_train,y_train) train_scores.append(regr.score(X_train,y_train)) test_scores.append(regr.score(X_test, y_test)) ax=fig.add_subplot(1,2,2) ax.plot(rs,train_scores,label="Training score ",marker='+' ) ax.plot(rs,test_scores,label= " Testing score ",marker='o' ) ax.set_title( "SVR_sigmoid_r gamma=10") ax.set_xlabel(r"r") ax.set_ylabel("score") ax.set_ylim(-1,1) ax.legend(loc="best",framealpha=0.5) plt.show() # 調(diào)用 test_SVR_sigmoid test_SVR_sigmoid(X_train,X_test,y_train,y_test)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)自定義函數(shù)的5種常見形式分析
這篇文章主要介紹了Python實(shí)現(xiàn)自定義函數(shù)的5種常見形式,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python自定義函數(shù)相關(guān)的參數(shù)、默認(rèn)值、隱函數(shù)等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-06-06基于Python實(shí)現(xiàn)身份證信息識(shí)別功能
身份證是用于證明個(gè)人身份和身份信息的官方證件,在現(xiàn)代社會(huì)中,身份證被廣泛應(yīng)用于各種場(chǎng)景,如就業(yè)、教育、醫(yī)療、金融等,它包含了個(gè)人的基本信息,本文給大家介紹了如何基于Python實(shí)現(xiàn)身份證信息識(shí)別功能,感興趣的朋友可以參考下2024-01-01Python數(shù)據(jù)分析之pandas函數(shù)詳解
這篇文章主要介紹了Python數(shù)據(jù)分析之pandas函數(shù)詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的pandas函數(shù)的小伙伴們有很好地幫助,需要的朋友可以參考下2021-04-04Python Pandas中根據(jù)列的值選取多行數(shù)據(jù)
這篇文章主要介紹了Python Pandas中根據(jù)列的值選取多行數(shù)據(jù)的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì) ,需要的朋友可以參考下2019-07-07python關(guān)于多值參數(shù)的實(shí)例詳解
在本篇內(nèi)容里小編給大家整理了一篇關(guān)于python關(guān)于多值參數(shù)的實(shí)例詳解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-07-07openCV中值濾波和均值濾波的代碼實(shí)現(xiàn)
在我們生活中的有很多時(shí)候都可以用到濾波,例如美顏的磨皮功能,本文就詳細(xì)的介紹了openCV中值濾波和均值濾波的代碼實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Python利用FFT進(jìn)行簡(jiǎn)單濾波的實(shí)現(xiàn)
今天小編就為大家分享一篇Python利用FFT進(jìn)行簡(jiǎn)單濾波的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02SVM算法的理解及其Python實(shí)現(xiàn)多分類和二分類問題
這篇文章主要介紹了SVM算法的理解及其Python實(shí)現(xiàn)多分類和二分類問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02