天天看點

python鸢尾花分類svm測試集_使用SVM對鸢尾花分類

使用SVM對鸢尾花分類

百度AI Studio中的一個入門項目,增加了自己在實踐時的一些注釋,對小白來說閱讀更順暢。源碼和資料在github上。

任務描述:

建構一個模型,根據鸢尾花的花萼和花瓣大小将其分為三種不同的品種。

python鸢尾花分類svm測試集_使用SVM對鸢尾花分類

資料集

總共包含150行資料

每一行資料由 4 個特征值及一個目标值組成。

4 個特征值分别為:萼片長度、萼片寬度、花瓣長度、花瓣寬度

目标值為三種不同類别的鸢尾花,分别為: Iris Setosa、Iris Versicolour、Iris Virginica

python鸢尾花分類svm測試集_使用SVM對鸢尾花分類

首先導入必要的包:

numpy:python第三方庫,用于科學計算

matplotlib:python第三方庫,主要用于進行可視化

sklearn:python的重要機器學習庫,其中封裝了大量的機器學習算法,如:分類、回歸、降維以及聚類

import numpy as np

from matplotlib import colors

from sklearn import svm

from sklearn.svm import SVC

from sklearn import model_selection

import matplotlib.pyplot as plt

import matplotlib as mpl

Step1.資料準備

(1)從指定路徑下加載資料

(2)對加載的資料進行資料分割,x_train,x_test,y_train,y_test分别表示訓練集特征、訓練集标簽、測試集特征、測試集标簽

#*************将字元串轉為整型,便于資料加載***********************

#在函數中建立一個對應字典就可以了,輸入字元串,輸出字元串對應的數字。

def iris_type(s):

# print(type(s))

#字元串加個b是指btypes 位元組串類型

it = {b'Iris-setosa':0, b'Iris-versicolor':1, b'Iris-virginica':2}

return it[s]

#加載資料

data_path='./iris.data' #資料檔案的路徑

data = np.loadtxt(data_path, #資料檔案路徑

dtype=float, #資料類型

delimiter=',', #資料分隔符

converters={4:iris_type}) #将第5列使用函數iris_type進行轉換

# print(data) #data為二維數組,data.shape=(150, 5)

# print(data.shape)

#資料分割

x, y = np.split(data, #要切分的數組

(4,), #沿軸切分的位置,第5列開始往後為y

axis=1) #1代表縱向分割,按列分割

x = x[:, 0:2]

#第一個逗号之前表示行,隻有冒号表示所有行,第二個冒号0:2表是0,1兩列

#在X中我們取前兩列作為特征,為了後面的可視化,原始的四維不好畫圖。x[:,0:4]代表第一維(行)全取,第二維(列)取0~2

# print(x)

x_train,x_test,y_train,y_test=model_selection.train_test_split(x, #所要劃分的樣本特征集

y, #所要劃分的樣本結果

random_state=1, #随機數種子確定産生的随機數組相同

test_size=0.3) #測試樣本占比

random_state=1確定了每次運作程式時用的随機數都是一樣的,也就是每次重新運作後所劃分的訓練集和測試集的樣本都是一緻的,相當于隻在第一次運作的時候進行随機劃分。如果不設定的話,每次重新運作的種子不一樣,産生的随機數也不一樣就會導緻每次随機生成的訓練集和測試集不一緻。

Step2.模型搭建

C越大,相當于懲罰松弛變量,希望松弛變量接近0,即對誤分類的懲罰增大,趨向于對訓練集全分對的情況,這樣對訓練集測試時準确率很高,但泛化能力弱。 C值小,對誤分類的懲罰減小,允許容錯,将他們當成噪聲點,泛化能力較強。

kernel='linear'時,為線性核

decision_function_shape='ovr'時,為one v rest,即一個類别與其他類别進行劃分,

decision_function_shape='ovo'時,為one v one,即将類别兩兩之間進行劃分,用二分類的方法模拟多分類的結果。 ovr是多類情況1和ovo是多類情況2,可以在我個人部落格-線性判别函數 上檢視詳細說明。

#**********************SVM分類器建構*************************

def classifier():

#clf = svm.SVC(C=0.8,kernel='rbf', gamma=50,decision_function_shape='ovr')

clf = svm.SVC(C=0.5, #誤差項懲罰系數,預設值是1

kernel='linear', #線性核 kenrel="rbf":高斯核

decision_function_shape='ovr') #決策函數

return clf

# 2.定義模型:SVM模型定義

clf = classifier()

Step3.模型訓練

y_train.ravel()#ravel()扁平化,将原來的二維數組轉換為一維數組

array([2., 0., 0., 0., 1., 0., 0., 2., 2., 2., 2., 2., 1., 2., 1., 0., 2.,

2., 0., 0., 2., 0., 2., 2., 1., 1., 2., 2., 0., 1., 1., 2., 1., 2.,

1., 0., 0., 0., 2., 0., 1., 2., 2., 0., 0., 1., 0., 2., 1., 2., 2.,

1., 2., 2., 1., 0., 1., 0., 1., 1., 0., 1., 0., 0., 2., 2., 2., 0.,

0., 1., 0., 2., 0., 2., 2., 0., 2., 0., 1., 0., 1., 1., 0., 0., 1.,

0., 1., 1., 0., 1., 1., 1., 1., 2., 0., 0., 2., 1., 2., 1., 2., 2.,

1., 2., 0.])

#***********************訓練模型*****************************

def train(clf,x_train,y_train):

clf.fit(x_train, #訓練集特征向量,fit表示輸入資料開始拟合

y_train.ravel()) #訓練集目标值 ravel()扁平化,将原來的二維數組轉換為一維數組

# 3.訓練SVM模型

train(clf,x_train,y_train)

Step4.模型評估

#**************并判斷a b是否相等,計算acc的均值*************

def show_accuracy(a, b, tip):

acc = a.ravel() == b.ravel()

print('%sAccuracy:%.3f' %(tip, np.mean(acc)))

def print_accuracy(clf,x_train,y_train,x_test,y_test):

#分别列印訓練集和測試集的準确率 score(x_train,y_train):表示輸出x_train,y_train在模型上的準确率

print('trianing prediction:%.3f' %(clf.score(x_train, y_train)))

print('test data prediction:%.3f' %(clf.score(x_test, y_test)))

#原始結果與預測結果進行對比 predict()表示對x_train樣本進行預測,傳回樣本類别

show_accuracy(clf.predict(x_train), y_train, 'traing data')

show_accuracy(clf.predict(x_test), y_test, 'testing data')

#計算決策函數的值,表示x到各分割平面的距離,3類,是以有3個決策函數,不同的多類情況有不同的決策函數?

print('decision_function:\n', clf.decision_function(x_train))

# 4.模型評估

print_accuracy(clf,x_train,y_train,x_test,y_test)

trianing prediction:0.819

test data prediction:0.778

traing data Accuracy:0.819

testing data Accuracy:0.778

decision_function:

[[-0.5 1.20887337 2.29112663]

[ 2.06328814 -0.0769677 1.01367956]

[ 2.16674973 0.91702835 -0.08377808]

[ 2.11427813 0.99765248 -0.11193061]

[ 0.9925538 2.06392138 -0.05647518]

[ 2.11742969 0.95255534 -0.06998503]

[ 2.05615004 -0.041847 0.98569697]

[-0.31866596 1.02685964 2.29180632]

[-0.27166251 1.09150338 2.18015913]

[-0.37827567 1.14260447 2.2356712 ]

[-0.22150749 1.11104997 2.11045752]

[-0.18331208 2.10066724 1.08264485]

[-0.05444966 0.99927764 2.05517201]

[-0.46977766 1.17853774 2.29123992]

[-0.05760122 2.04437478 1.01322644]

[ 2.1747228 0.93698124 -0.11170404]

[-0.13315707 2.12021384 1.01294323]

[-0.21752096 2.12102642 1.09649454]

[ 2.11427813 0.99765248 -0.11193061]

[ 2.16359817 0.96212549 -0.12572366]

[-0.21038286 1.08590572 2.12447714]

[ 2.21291822 0.9265985 -0.13951672]

[-0.13399204 1.06514025 2.06885179]

[-0.18016052 1.0555701 2.12459042]

[-0.2334671 1.08112064 2.15234646]

[-0.08782356 2.0747104 1.01311315]

[-0.20324476 1.05078502 2.15245974]

[-0.11489433 1.05994888 2.05494545]

[ 2.17787437 -0.1081159 0.93024154]

[-0.23578369 2.18129137 1.05449232]

[-0.20639632 1.09588216 2.11051416]

[-0.21038286 1.08590572 2.12447714]

[-0.02969547 2.11420989 0.91548558]

[-0.12685394 1.03001955 2.09683439]

[-0.09496166 2.1098311 0.98513056]

[ 2.10547008 -0.07737399 0.97190391]

[ 2.11029159 0.98767604 -0.09796763]

[ 2.20411017 -0.14842797 0.9443178 ]

[-0.20324476 1.05078502 2.15245974]

[ 2.19066895 0.97688701 -0.16755596]

[-0.16022784 2.10545232 1.05477553]

[-0.23661866 1.12621778 2.11040088]

[-0.09579663 2.05475752 1.04103911]

[ 2.11344315 -0.05742111 0.94397795]

[ 2.10231852 0.96772315 -0.07004167]

[-0.12203243 2.09506958 1.02696285]

[ 2.11029159 0.98767604 -0.09796763]

[-0.41248455 1.16296364 2.2495209 ]

[-0.16820091 1.08549943 2.08270149]

[-0.42045762 1.14301076 2.27744686]

[-0.24857827 1.09628845 2.15228982]

[-0.27796564 2.18169766 1.09626798]

[-0.09264507 1.00966038 2.08298469]

[-0.25339978 1.03123843 2.22216135]

[-0.05361468 2.05435123 0.99926346]

[ 2.15395516 -0.16797456 1.01401941]

[-0.12203243 2.09506958 1.02696285]

[ 2.06579305 1.08825305 -0.15404611]

[-0.11007283 2.12499891 0.98507392]

[-0.27166251 1.09150338 2.18015913]

[ 2.13652739 0.94736397 -0.08389137]

[-0.29789831 1.13181544 2.16608287]

[ 2.15163856 0.93219616 -0.08383473]

[ 2.1747228 0.93698124 -0.11170404]

[-0.11174277 1.01485174 2.09689103]

[-0.06872585 2.06951904 0.99920682]

[-0.23745364 1.0711442 2.16630944]

[ 2.12141623 0.96253178 -0.08394801]

[ 2.1627632 -0.09294809 0.93018489]

[-0.06557429 1.0244219 2.04115239]

[ 2.16758471 0.97210193 -0.13968664]

[-0.12203243 2.09506958 1.02696285]

[ 2.1293893 0.98248467 -0.11187396]

[-0.21038286 1.08590572 2.12447714]

[ 2.01962457 1.0786829 -0.09830747]

[ 2.18269588 0.95693412 -0.13963 ]

[-0.16106282 1.05037873 2.11068408]

[ 2.20976665 0.97169564 -0.1814623 ]

[-0.03850351 2.03918342 0.9993201 ]

[ 2.17555778 0.99205482 -0.1676126 ]

[-0.11007283 2.12499891 0.98507392]

[-0.07502898 2.15971332 0.91531566]

[ 2.13254086 0.93738753 -0.06992839]

[ 2.09518042 1.00284385 -0.09802427]

[ 1.0045134 2.09385071 -0.09836411]

[ 2.24314055 0.89626288 -0.13940344]

[-0.09579663 2.05475752 1.04103911]

[-0.14910321 1.08030806 2.06879515]

[ 2.13652739 0.94736397 -0.08389137]

[-0.2334671 1.08112064 2.15234646]

[-0.07271239 2.05954259 1.0131698 ]

[-0.2739791 2.1916741 1.082305 ]

[-0.27564905 1.08152693 2.19412211]

[-0.12203243 2.09506958 1.02696285]

[ 2.06013657 -0.03187056 0.97173399]

[ 2.07608272 1.00803521 -0.08411793]

[-0.19443672 2.12581149 1.06862523]

[-0.16421438 2.09547587 1.06873851]

[-0.3440668 1.12224529 2.22182151]

[-0.1180459 2.10504603 1.01299987]

[-0.20240979 1.10585861 2.09655118]

[-0.17617399 1.06554654 2.11062744]

[-0.2477433 2.15136204 1.09638126]

[-0.2334671 1.08112064 2.15234646]

[ 2.11029159 0.98767604 -0.09796763]]

Step5.模型使用

np.mgrid的作用是用前兩個特征生成其對應最大最小範圍所能組合出的所有200*200的樣本,也就是周遊了這兩個特征所能組合出的所有可能性,隻是粒度是1/200

def draw(clf, x):

iris_feature = 'sepal length', 'sepal width', 'petal lenght', 'petal width'

# 開始畫圖

x1_min, x1_max = x[:, 0].min(), x[:, 0].max() #第0列的範圍

x2_min, x2_max = x[:, 1].min(), x[:, 1].max() #第1列的範圍

x1, x2 = np.mgrid[x1_min:x1_max:200j, x2_min:x2_max:200j] #生成網格采樣點 開始坐标:結束坐标(不包括):步長

#flat将二維數組轉換成1個1維的疊代器,然後把x1和x2的所有可能值給比對成為樣本點

grid_test = np.stack((x1.flat, x2.flat), axis=1) #stack():沿着新的軸加入一系列數組,豎着(按列)增加兩個數組,grid_test的shape:(40000, 2)

print('grid_test:\n', grid_test)

# 輸出樣本到決策面的距離

z = clf.decision_function(grid_test)

print('the distance to decision plane:\n', z)

grid_hat = clf.predict(grid_test) # 預測分類值 得到【0,0.。。。2,2,2】

print('grid_hat:\n', grid_hat)

grid_hat = grid_hat.reshape(x1.shape) # reshape grid_hat和x1形狀一緻

#若3*3矩陣e,則e.shape()為3*3,表示3行3列

#light是網格測試點的配色,相當于背景

#dark是樣本點的配色

cm_light = mpl.colors.ListedColormap(['#A0FFA0', '#FFA0A0', '#A0A0FF'])

cm_dark = mpl.colors.ListedColormap(['g', 'b', 'r'])

#畫出所有網格樣本點被判斷為的分類,作為背景

plt.pcolormesh(x1, x2, grid_hat, cmap=cm_light) # pcolormesh(x,y,z,cmap)這裡參數代入

# x1,x2,grid_hat,cmap=cm_light繪制的是背景。

#squeeze()把y的個數為1的次元去掉,也就是變成一維。

plt.scatter(x[:, 0], x[:, 1], c=np.squeeze(y), edgecolor='k', s=50, cmap=cm_dark) # 樣本點

plt.scatter(x_test[:, 0], x_test[:, 1], s=200, facecolor='yellow', zorder=10, marker='+') # 測試點

plt.xlabel(iris_feature[0], fontsize=20)

plt.ylabel(iris_feature[1], fontsize=20)

plt.xlim(x1_min, x1_max)

plt.ylim(x2_min, x2_max)

plt.title('svm in iris data classification', fontsize=30)

plt.grid()

plt.show()

# 5.模型使用

draw(clf,x)

grid_test:

[[4.3 2. ]

[4.3 2.0120603]

[4.3 2.0241206]

...

[7.9 4.3758794]

[7.9 4.3879397]

[7.9 4.4 ]]

the distance to decision plane:

[[ 2.04663576 1.0980928 -0.14472856]

[ 2.04808477 1.09663836 -0.14472313]

[ 2.04953377 1.09518392 -0.1447177 ]

...

[-0.21454554 0.96016146 2.25438408]

[-0.21309653 0.95870702 2.25438951]

[-0.21164753 0.95725258 2.25439495]]

grid_hat:

[0. 0. 0. ... 2. 2. 2.]

python鸢尾花分類svm測試集_使用SVM對鸢尾花分類

參考

個人部落格原文:使用SVM對鸢尾花分類 閱讀體驗更佳。