国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python機器學習之SVM支持向量機

2020-02-16 11:19:18
字體:
來源:轉載
供稿:網友

SVM支持向量機是建立于統計學習理論上的一種分類算法,適合與處理具備高維特征的數據集。
SVM算法的數學原理相對比較復雜,好在由于SVM算法的研究與應用如此火爆,CSDN博客里也有大量的好文章對此進行分析,下面給出幾個本人認為講解的相當不錯的:
支持向量機通俗導論(理解SVM的3層境界)
JULY大牛講的是如此詳細,由淺入深層層推進,以至于關于SVM的原理,我一個字都不想寫了。。強烈推薦。
還有一個比較通俗的簡單版本的:手把手教你實現SVM算法

SVN原理比較復雜,但是思想很簡單,一句話概括,就是通過某種核函數,將數據在高維空間里尋找一個最優超平面,能夠將兩類數據分開。

針對不同數據集,不同的核函數的分類效果可能完全不一樣。可選的核函數有這么幾種:
線性函數:形如K(x,y)=x*y這樣的線性函數;
多項式函數:形如K(x,y)=[(x·y)+1]^d這樣的多項式函數;
徑向基函數:形如K(x,y)=exp(-|x-y|^2/d^2)這樣的指數函數;
Sigmoid函數:就是上一篇文章中講到的Sigmoid函數。

我們就利用之前的幾個數據集,直接給出Python代碼,看看運行效果:

測試1:身高體重數據

# -*- coding: utf-8 -*- import numpy as np import scipy as sp from sklearn import svm from sklearn.cross_validation import train_test_split import matplotlib.pyplot as plt  data  = [] labels = [] with open("data//1.txt") as ifile:     for line in ifile:       tokens = line.strip().split(' ')       data.append([float(tk) for tk in tokens[:-1]])       labels.append(tokens[-1]) x = np.array(data) labels = np.array(labels) y = np.zeros(labels.shape) y[labels=='fat']=1 x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.0)  h = .02  # create a mesh to plot in x_min, x_max = x_train[:, 0].min() - 0.1, x_train[:, 0].max() + 0.1 y_min, y_max = x_train[:, 1].min() - 1, x_train[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h),            np.arange(y_min, y_max, h))  ''''' SVM ''' # title for the plots titles = ['LinearSVC (linear kernel)',      'SVC with polynomial (degree 3) kernel',      'SVC with RBF kernel',      'SVC with Sigmoid kernel'] clf_linear = svm.SVC(kernel='linear').fit(x, y) #clf_linear = svm.LinearSVC().fit(x, y) clf_poly  = svm.SVC(kernel='poly', degree=3).fit(x, y) clf_rbf   = svm.SVC().fit(x, y) clf_sigmoid = svm.SVC(kernel='sigmoid').fit(x, y)  for i, clf in enumerate((clf_linear, clf_poly, clf_rbf, clf_sigmoid)):   answer = clf.predict(np.c_[xx.ravel(), yy.ravel()])   print(clf)   print(np.mean( answer == y_train))   print(answer)   print(y_train)    plt.subplot(2, 2, i + 1)   plt.subplots_adjust(wspace=0.4, hspace=0.4)      # Put the result into a color plot   z = answer.reshape(xx.shape)   plt.contourf(xx, yy, z, cmap=plt.cm.Paired, alpha=0.8)      # Plot also the training points   plt.scatter(x_train[:, 0], x_train[:, 1], c=y_train, cmap=plt.cm.Paired)   plt.xlabel(u'身高')   plt.ylabel(u'體重')   plt.xlim(xx.min(), xx.max())   plt.ylim(yy.min(), yy.max())   plt.xticks(())   plt.yticks(())   plt.title(titles[i])    plt.show()             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天台县| 巧家县| 合川市| 墨江| 化州市| 宣城市| 岳普湖县| 东丰县| 霸州市| 德兴市| 昌都县| 汉源县| 师宗县| 六枝特区| 纳雍县| 无极县| 洛宁县| 南皮县| 余干县| 马尔康县| 茂名市| 望都县| 浠水县| 沙河市| 吉安市| 闸北区| 滕州市| 梨树县| 临漳县| 昂仁县| 汤阴县| 牙克石市| 翁牛特旗| 伊吾县| 绥德县| 余江县| 醴陵市| 浦江县| 东明县| 陆川县| 陆川县|