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

python SVM 線性分類模型的實(shí)現(xiàn)

 更新時(shí)間:2019年07月19日 14:18:20   作者:Jack_丁明  
這篇文章主要介紹了python SVM 線性分類模型的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

運(yùn)行環(huán)境:win10 64位 py 3.6 pycharm 2018.1.1

導(dǎo)入對(duì)應(yīng)的包和數(shù)據(jù)

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets,linear_model,cross_validation,svm
def load_data_regression():
  diabetes = datasets.load_diabetes()
  return cross_validation.train_test_split(diabetes,diabetes.target,test_size=0.25,random_state=0)
def load_data_classfication():
  iris = datasets.load_iris()
  X_train = iris.data
  y_train = iris.target
  return cross_validation.train_test_split(X_train,y_train,test_size=0.25,random_state=0,stratify=y_train)
#線性分類SVM
def test_LinearSVC(*data):
  X_train,X_test,y_train,y_test = data
  cls = svm.LinearSVC()
  cls.fit(X_train,y_train)
  print('Coefficients:%s,intercept%s'%(cls.coef_,cls.intercept_))
  print('Score:%.2f'%cls.score(X_test,y_test))
X_train,X_test,y_train,y_test = load_data_classfication()
test_LinearSVC(X_train,X_test,y_train,y_test)
def test_LinearSVC_loss(*data):
  X_train,X_test,y_train,y_test = data
  losses = ['hinge','squared_hinge']
  for loss in losses:
    cls = svm.LinearSVC(loss=loss)
    cls.fit(X_train,y_train)
    print('loss:%s'%loss)
    print('Coefficients:%s,intercept%s'%(cls.coef_,cls.intercept_))
    print('Score:%.2f'%cls.score(X_test,y_test))
X_train,X_test,y_train,y_test = load_data_classfication()
test_LinearSVC_loss(X_train,X_test,y_train,y_test)
#考察罰項(xiàng)形式的影響
def test_LinearSVC_L12(*data):
  X_train,X_test,y_train,y_test = data
  L12 = ['l1','l2']
  for p in L12:
    cls = svm.LinearSVC(penalty=p,dual=False)
    cls.fit(X_train,y_train)
    print('penalty:%s'%p)
    print('Coefficients:%s,intercept%s'%(cls.coef_,cls.intercept_))
    print('Score:%.2f'%cls.score(X_test,y_test))
X_train,X_test,y_train,y_test = load_data_classfication()
test_LinearSVC_L12(X_train,X_test,y_train,y_test)
#考察罰項(xiàng)系數(shù)C的影響
def test_LinearSVC_C(*data):
  X_train,X_test,y_train,y_test = data
  Cs = np.logspace(-2,1)
  train_scores = []
  test_scores = []
  for C in Cs:
    cls = svm.LinearSVC(C=C)
    cls.fit(X_train,y_train)
    train_scores.append(cls.score(X_train,y_train))
    test_scores.append(cls.score(X_test,y_test))
  fig = plt.figure()
  ax = fig.add_subplot(1,1,1)
  ax.plot(Cs,train_scores,label = 'Training score')
  ax.plot(Cs,test_scores,label = 'Testing score')
  ax.set_xlabel(r'C')
  ax.set_xscale('log')
  ax.set_ylabel(r'score')
  ax.set_title('LinearSVC')
  ax.legend(loc='best')
  plt.show()
X_train,X_test,y_train,y_test = load_data_classfication()
test_LinearSVC_C(X_train,X_test,y_train,y_test)

#非線性分類SVM
#線性核
def test_SVC_linear(*data):
  X_train, X_test, y_train, y_test = data
  cls = svm.SVC(kernel='linear')
  cls.fit(X_train,y_train)
  print('Coefficients:%s,intercept%s'%(cls.coef_,cls.intercept_))
  print('Score:%.2f'%cls.score(X_test,y_test))
X_train,X_test,y_train,y_test = load_data_classfication()
test_SVC_linear(X_train,X_test,y_train,y_test)

#考察高斯核
def test_SVC_rbf(*data):
  X_train, X_test, y_train, y_test = data
  ###測(cè)試gamm###
  gamms = range(1, 20)
  train_scores = []
  test_scores = []
  for gamm in gamms:
    cls = svm.SVC(kernel='rbf', gamma=gamm)
    cls.fit(X_train, y_train)
    train_scores.append(cls.score(X_train, y_train))
    test_scores.append(cls.score(X_test, y_test))
  fig = plt.figure()
  ax = fig.add_subplot(1, 1, 1)
  ax.plot(gamms, train_scores, label='Training score', marker='+')
  ax.plot(gamms, test_scores, label='Testing score', marker='o')
  ax.set_xlabel(r'$\gamma$')
  ax.set_ylabel(r'score')
  ax.set_ylim(0, 1.05)
  ax.set_title('SVC_rbf')
  ax.legend(loc='best')
  plt.show()
X_train,X_test,y_train,y_test = load_data_classfication()
test_SVC_rbf(X_train,X_test,y_train,y_test)

#考察sigmoid核
def test_SVC_sigmod(*data):
  X_train, X_test, y_train, y_test = data
  fig = plt.figure()
  ###測(cè)試gamm###
  gamms = np.logspace(-2, 1)
  train_scores = []
  test_scores = []
  for gamm in gamms:
    cls = svm.SVC(kernel='sigmoid',gamma=gamm,coef0=0)
    cls.fit(X_train, y_train)
    train_scores.append(cls.score(X_train, y_train))
    test_scores.append(cls.score(X_test, y_test))
  ax = fig.add_subplot(1, 2, 1)
  ax.plot(gamms, train_scores, label='Training score', marker='+')
  ax.plot(gamms, test_scores, label='Testing score', marker='o')
  ax.set_xlabel(r'$\gamma$')
  ax.set_ylabel(r'score')
  ax.set_xscale('log')
  ax.set_ylim(0, 1.05)
  ax.set_title('SVC_sigmoid_gamm')
  ax.legend(loc='best')

  #測(cè)試r
  rs = np.linspace(0,5)
  train_scores = []
  test_scores = []
  for r in rs:
    cls = svm.SVC(kernel='sigmoid', gamma=0.01, coef0=r)
    cls.fit(X_train, y_train)
    train_scores.append(cls.score(X_train, y_train))
    test_scores.append(cls.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_xlabel(r'r')
  ax.set_ylabel(r'score')
  ax.set_ylim(0, 1.05)
  ax.set_title('SVC_sigmoid_r')
  ax.legend(loc='best')
  plt.show()
X_train,X_test,y_train,y_test = load_data_classfication()
test_SVC_sigmod(X_train,X_test,y_train,y_test)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python中np.multiply()、np.dot()和星號(hào)(*)三種乘法運(yùn)算的區(qū)別詳解

    python中np.multiply()、np.dot()和星號(hào)(*)三種乘法運(yùn)算的區(qū)別詳解

    這篇文章主要介紹了python中np.multiply()、np.dot()和星號(hào)(*)三種乘法運(yùn)算的區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 詳解python中的變量與注釋

    詳解python中的變量與注釋

    在 Python 中,變量是用于存儲(chǔ)數(shù)據(jù)的名稱,它可以保存不同類型的數(shù)據(jù),在Python中,有兩種類型的注釋:?jiǎn)涡凶⑨尯投嘈凶⑨?本文就給大家詳細(xì)的介紹一下python中的變量與注釋,需要的朋友可以參考下
    2023-08-08
  • python 字典和列表嵌套用法詳解

    python 字典和列表嵌套用法詳解

    python中字典和列表的使用,在數(shù)據(jù)處理中應(yīng)該是最常用的,今天通過(guò)多種場(chǎng)景給大家分享python 字典和列表嵌套用法,感興趣的朋友一起看看吧
    2021-06-06
  • python?format格式化和數(shù)字格式化

    python?format格式化和數(shù)字格式化

    這篇文章主要介紹了python?format格式化和數(shù)字格式化,格式化字符串的函數(shù)?str.format(),它增強(qiáng)了字符串格式化的功能,基本語(yǔ)法是通過(guò){}?和?:?來(lái)代替以前的?%?,下面內(nèi)容介紹,需要的朋友可以參考一下
    2022-02-02
  • 詳解python中的IO操作方法

    詳解python中的IO操作方法

    這篇文章主要介紹了Python實(shí)現(xiàn)IO操作的示例,是python入門必會(huì)得知識(shí)點(diǎn),將幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2022-01-01
  • 【python】matplotlib動(dòng)態(tài)顯示詳解

    【python】matplotlib動(dòng)態(tài)顯示詳解

    這篇文章主要介紹了matplotlib動(dòng)態(tài)顯示,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 解決90%的常見(jiàn)問(wèn)題的8個(gè)python NumPy函數(shù)

    解決90%的常見(jiàn)問(wèn)題的8個(gè)python NumPy函數(shù)

    這篇文章主要為大家介紹了解決90%的常見(jiàn)問(wèn)題的8個(gè)python NumPy函數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • python實(shí)現(xiàn)決策樹(shù)、隨機(jī)森林的簡(jiǎn)單原理

    python實(shí)現(xiàn)決策樹(shù)、隨機(jī)森林的簡(jiǎn)單原理

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)決策樹(shù)、隨機(jī)森林的簡(jiǎn)單原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • python如果快速判斷數(shù)字奇數(shù)偶數(shù)

    python如果快速判斷數(shù)字奇數(shù)偶數(shù)

    這篇文章主要介紹了python如果快速判斷數(shù)字奇數(shù)偶數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Python無(wú)法用requests獲取網(wǎng)頁(yè)源碼的解決方法

    Python無(wú)法用requests獲取網(wǎng)頁(yè)源碼的解決方法

    爬蟲(chóng)獲取信息,很多時(shí)候是需要從網(wǎng)頁(yè)源碼中獲取鏈接信息的,下面這篇文章主要給大家介紹了關(guān)于Python無(wú)法用requests獲取網(wǎng)頁(yè)源碼的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07

最新評(píng)論

拜城县| 集贤县| 察隅县| 昭苏县| 松潘县| 芜湖县| 太康县| 乳源| 天门市| 济阳县| 游戏| 汉寿县| 吉木乃县| 当雄县| 平乐县| 静宁县| 武宁县| 平果县| 二连浩特市| 大余县| 江山市| 呼伦贝尔市| 博爱县| 永吉县| 福州市| 义马市| 海原县| 建昌县| 长子县| 霍林郭勒市| 临澧县| 东光县| 青冈县| 行唐县| 萍乡市| 伊通| 龙山县| 长垣县| 阿勒泰市| 普洱| 洪洞县|