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

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

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

本文介紹了python 支持向量機(jī)非線(xiàn)性回歸SVR模型,廢話(huà)不多說(shuō),具體如下:

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():
  '''
  加載用于回歸問(wèn)題的數(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ī)非線(xiàn)性回歸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))
  
# 生成用于回歸問(wèn)題的數(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)文章

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

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

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

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

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

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

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

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

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

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

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

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

    本文詳細(xì)介紹了如何使用Python通過(guò)ncmdump工具批量將.ncm音頻轉(zhuǎn)換為.mp3的步驟,包括安裝、配置ffmpeg環(huán)境變量及解決轉(zhuǎn)碼警告,實(shí)現(xiàn)自動(dòng)化格式轉(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ì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    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ì)算、約分等簡(jiǎn)單操作,是Python學(xué)習(xí)過(guò)程當(dāng)中的基礎(chǔ),需要的朋友可以參考下
    2015-03-03
  • python+html文字點(diǎn)選驗(yàn)證碼加固安全防線(xiàn)

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

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

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

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

最新評(píng)論

沾化县| 定州市| 盐亭县| 甘德县| 广元市| 绥滨县| 濮阳市| 衡阳县| 固始县| 荣昌县| 黄平县| 西充县| 尉氏县| 额济纳旗| 苏州市| 通许县| 公主岭市| 宁阳县| 章丘市| 大庆市| 华池县| 临洮县| 孝昌县| 梅河口市| 南皮县| 甘肃省| 邵武市| 安图县| 广州市| 垣曲县| 德州市| 兰州市| 大同市| 永城市| 岐山县| 太仆寺旗| 通海县| 翁牛特旗| 揭东县| 龙州县| 新巴尔虎右旗|