欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 機器學習之支持向量機非線性回歸SVR模型

 更新時間:2019年06月26日 09:56:25   作者:吳裕雄  
這篇文章主要介紹了python 機器學習之支持向量機非線性回歸SVR模型,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

本文介紹了python 支持向量機非線性回歸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 自帶的一個糖尿病病人的數(shù)據(jù)集
  # 拆分成訓練集和測試集,測試集大小為原始數(shù)據(jù)集大小的 1/4
  return train_test_split(diabetes.data,diabetes.target,test_size=0.25,random_state=0)

#支持向量機非線性回歸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):
  '''
  測試 多項式核的 SVR 的預測性能隨 degree、gamma、coef0 的影響.
  '''
  X_train,X_test,y_train,y_test=data
  fig=plt.figure()
  ### 測試 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)

  ### 測試 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)
  ### 測試 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):
  '''
  測試 高斯核的 SVR 的預測性能隨 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):
  '''
  測試 sigmoid 核的 SVR 的預測性能隨 gamma、coef0 的影響.
  '''
  X_train,X_test,y_train,y_test=data
  fig=plt.figure()

  ### 測試 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)
  ### 測試 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)

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Numpy之將矩陣拉成向量的實例

    Numpy之將矩陣拉成向量的實例

    今天小編就為大家分享一篇Numpy之將矩陣拉成向量的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 淺談python常用程序算法

    淺談python常用程序算法

    這篇文章主要介紹了python常用程序算法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • Python實現(xiàn)自定義函數(shù)的5種常見形式分析

    Python實現(xiàn)自定義函數(shù)的5種常見形式分析

    這篇文章主要介紹了Python實現(xiàn)自定義函數(shù)的5種常見形式,結合實例形式較為詳細的分析了Python自定義函數(shù)相關的參數(shù)、默認值、隱函數(shù)等相關操作技巧與注意事項,需要的朋友可以參考下
    2018-06-06
  • 基于Python實現(xiàn)身份證信息識別功能

    基于Python實現(xiàn)身份證信息識別功能

    身份證是用于證明個人身份和身份信息的官方證件,在現(xiàn)代社會中,身份證被廣泛應用于各種場景,如就業(yè)、教育、醫(yī)療、金融等,它包含了個人的基本信息,本文給大家介紹了如何基于Python實現(xiàn)身份證信息識別功能,感興趣的朋友可以參考下
    2024-01-01
  • Python數(shù)據(jù)分析之pandas函數(shù)詳解

    Python數(shù)據(jù)分析之pandas函數(shù)詳解

    這篇文章主要介紹了Python數(shù)據(jù)分析之pandas函數(shù)詳解,文中有非常詳細的代碼示例,對正在學習python的pandas函數(shù)的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-04-04
  • Python Pandas中根據(jù)列的值選取多行數(shù)據(jù)

    Python Pandas中根據(jù)列的值選取多行數(shù)據(jù)

    這篇文章主要介紹了Python Pandas中根據(jù)列的值選取多行數(shù)據(jù)的實例代碼,本文通過實例代碼給大家介紹的非常詳細 ,需要的朋友可以參考下
    2019-07-07
  • python關于多值參數(shù)的實例詳解

    python關于多值參數(shù)的實例詳解

    在本篇內(nèi)容里小編給大家整理了一篇關于python關于多值參數(shù)的實例詳解內(nèi)容,有興趣的朋友們可以學習下。
    2021-07-07
  • openCV中值濾波和均值濾波的代碼實現(xiàn)

    openCV中值濾波和均值濾波的代碼實現(xiàn)

    在我們生活中的有很多時候都可以用到濾波,例如美顏的磨皮功能,本文就詳細的介紹了openCV中值濾波和均值濾波的代碼實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Python利用FFT進行簡單濾波的實現(xiàn)

    Python利用FFT進行簡單濾波的實現(xiàn)

    今天小編就為大家分享一篇Python利用FFT進行簡單濾波的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • SVM算法的理解及其Python實現(xiàn)多分類和二分類問題

    SVM算法的理解及其Python實現(xiàn)多分類和二分類問題

    這篇文章主要介紹了SVM算法的理解及其Python實現(xiàn)多分類和二分類問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評論