最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python 機(jī)器學(xué)習(xí)之支持向量機(jī)非線性回歸SVR模型

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

本文介紹了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 自帶的一個糖尿病病人的數(shù)據(jù)集
  # 拆分成訓(xùn)練集和測試集,測試集大小為原始數(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):
  '''
  測試 多項(xiàng)式核的 SVR 的預(yù)測性能隨 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 的預(yù)測性能隨 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 的預(yù)測性能隨 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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python3實(shí)現(xiàn)倒計(jì)時效果

    python3實(shí)現(xiàn)倒計(jì)時效果

    這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)倒計(jì)時效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 使用NumPy讀取MNIST數(shù)據(jù)的實(shí)現(xiàn)代碼示例

    使用NumPy讀取MNIST數(shù)據(jù)的實(shí)現(xiàn)代碼示例

    這篇文章主要介紹了使用NumPy讀取MNIST數(shù)據(jù)的實(shí)現(xiàn)代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Python中的POST請求參數(shù)詳解

    Python中的POST請求參數(shù)詳解

    這篇文章主要介紹了Python中的POST請求參數(shù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • python assert斷言的實(shí)例用法

    python assert斷言的實(shí)例用法

    在本篇文章里小編給大家整理了一篇關(guān)于python assert斷言的實(shí)例用法,有需要的朋友們可以跟著學(xué)習(xí)參考下。
    2021-09-09
  • Python中數(shù)組,列表:冒號的靈活用法介紹(np數(shù)組,列表倒序)

    Python中數(shù)組,列表:冒號的靈活用法介紹(np數(shù)組,列表倒序)

    下面小編就為大家分享一篇Python中數(shù)組,列表:冒號的靈活用法介紹(np數(shù)組,列表倒序),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 使用Python批量將.ncm格式的音頻文件轉(zhuǎn)換為.mp3格式的實(shí)戰(zhàn)詳解

    使用Python批量將.ncm格式的音頻文件轉(zhuǎn)換為.mp3格式的實(shí)戰(zhàn)詳解

    本文詳細(xì)介紹了如何使用Python通過ncmdump工具批量將.ncm音頻轉(zhuǎn)換為.mp3的步驟,包括安裝、配置ffmpeg環(huán)境變量及解決轉(zhuǎn)碼警告,實(shí)現(xiàn)自動化格式轉(zhuǎn)換,需要的朋友可以參考下
    2025-09-09
  • python實(shí)現(xiàn)126郵箱發(fā)送郵件

    python實(shí)現(xiàn)126郵箱發(fā)送郵件

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)126郵箱發(fā)送郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Python中分?jǐn)?shù)的相關(guān)使用教程

    Python中分?jǐn)?shù)的相關(guān)使用教程

    這篇文章主要介紹了Python中分?jǐn)?shù)的相關(guān)使用教程,主要涉及分?jǐn)?shù)的計(jì)算、約分等簡單操作,是Python學(xué)習(xí)過程當(dāng)中的基礎(chǔ),需要的朋友可以參考下
    2015-03-03
  • python+html文字點(diǎn)選驗(yàn)證碼加固安全防線

    python+html文字點(diǎn)選驗(yàn)證碼加固安全防線

    這篇文章主要為大家介紹了python文字點(diǎn)選驗(yàn)證碼加固安全防線實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Django python雪花算法實(shí)現(xiàn)方式

    Django python雪花算法實(shí)現(xiàn)方式

    在Django項(xiàng)目中添加自定義模塊或應(yīng)用來封裝雪花算法,步驟包括創(chuàng)建應(yīng)用,編寫算法實(shí)現(xiàn)代碼至utils.py文件,及配置settings.py,此方法可方便在項(xiàng)目中隨處調(diào)用雪花算法,適用于需要唯一ID生成的場景
    2024-09-09

最新評論

尉氏县| 贡觉县| 浏阳市| 措美县| 榆林市| 青浦区| 顺义区| 漳平市| 漾濞| 永福县| 邹城市| 云霄县| 固镇县| 沁阳市| 玉屏| 祁东县| 塔城市| 肇东市| 重庆市| 桃源县| 即墨市| 易门县| 临漳县| 铜陵市| 和政县| 玛沁县| 望谟县| 长沙县| 海伦市| 鲁甸县| 邓州市| 莎车县| 乌拉特中旗| 玉树县| 辽源市| 五大连池市| 淮北市| 治多县| 佳木斯市| 武定县| 达拉特旗|