xxxx18一60岁hd中国/日韩女同互慰一区二区/西西人体扒开双腿无遮挡/日韩欧美黄色一级片 - 色护士精品影院www

資源簡(jiǎn)介

該文件包含鳶尾花分類的Python源程序和數(shù)據(jù)集,數(shù)據(jù)集內(nèi)包含 3 類鳶尾花,分別為山鳶尾(setosa)、變色鳶尾(versicolor)和維吉尼亞鳶尾(virginica)。每類各 50 個(gè)數(shù)據(jù),每條記錄有 4 項(xiàng)特征:花萼長(zhǎng)度、花萼寬度、花瓣長(zhǎng)度、花瓣寬度。

資源截圖

代碼片段和文件信息

from?sklearn?import?svm
import?numpy?as?np
from?sklearn?import?model_selection
import?matplotlib.pyplot?as?plt
import?matplotlib?as?mpl
from?matplotlib?import?colors
#?#?當(dāng)使用numpy中的loadtxt函數(shù)導(dǎo)入該數(shù)據(jù)集時(shí),假設(shè)數(shù)據(jù)類型dtype為浮點(diǎn)型,但是很明顯數(shù)據(jù)集的第五列的數(shù)據(jù)類型是字符串并不是浮點(diǎn)型。
#?#?因此需要額外做一個(gè)工作,即通過(guò)loadtxt()函數(shù)中的converters參數(shù)將第五列通過(guò)轉(zhuǎn)換函數(shù)映射成浮點(diǎn)類型的數(shù)據(jù)。
#?#?首先,我們要寫(xiě)出一個(gè)轉(zhuǎn)換函數(shù):
#?#?定義一個(gè)函數(shù),將不同類別標(biāo)簽與數(shù)字相對(duì)應(yīng)
def?iris_type(s):
????class_label={b‘setosa‘:0b‘versicolor‘:1b‘virginica‘:2}
????return?class_label[s]

def?train(model?x_train?y_train):
????model.fit(x_trainy_train.ravel())

def?show_accuracy(a?b?tip):
????acc?=?(a.ravel()?==?b.ravel())
????print(“%s?Accuracy:%.3f“%(tip?np.mean(acc)))

def?print_accuracy(model?x_train?y_trainx_test?y_test?):
????print(‘SVM-輸出訓(xùn)練集的準(zhǔn)確率為:‘?model.score(x_train?y_train))
????print(“SVM-輸出測(cè)試集的準(zhǔn)確率為:“?model.score(x_test?y_test))
????#原始結(jié)果與預(yù)測(cè)結(jié)果進(jìn)行對(duì)比
????show_accuracy(model.predict(x_train)?y_train?‘traing?data‘)
????show_accuracy(model.predict(x_test)?y_test?‘testing?data‘)
????#計(jì)算決策函數(shù)的值
????print(‘decision_function:\n‘?model.decision_function(x_train))

def?draw(model?x):
????x1_min?x1_max?=?x[:?0].min()?x[:?0].max()??#?第0列的范圍??x[:?0]?“:“表示所有行,0表示第1列
????x2_min?x2_max?=?x[:?1].min()?x[:?1].max()??#?第1列的范圍??x[:?0]?“:“表示所有行,1表示第2列
????x1?x2?=?np.mgrid[x1_min:x1_max:200j?x2_min:x2_max:200j]??#?生成網(wǎng)格采樣點(diǎn)(用meshgrid函數(shù)生成兩個(gè)網(wǎng)格矩陣X1和X2)
????grid_test?=?np.stack((x1.flat?x2.flat)?axis=1)??#?測(cè)試點(diǎn),再通過(guò)stack()函數(shù),axis=1,生成測(cè)試點(diǎn)
????#?.flat?將矩陣轉(zhuǎn)變成一維數(shù)組?(與ravel()的區(qū)別:flatten:返回的是拷貝

????grid_hat?=?model.predict(grid_test)??#?預(yù)測(cè)分類值
????grid_hat?=?grid_hat.reshape(x1.shape)??#?使之與輸入的形狀相同

????#?#?2.指定默認(rèn)字體
????#?mpl.rcParams[‘font.sans-serif‘]?=?[u‘SimHei‘]
????#?mpl.rcParams[‘a(chǎn)xes.unicode_minus‘]?=?False

????#?3.繪制
????cm_light?=?mpl.colors.ListedColormap([‘#A0FFA0‘?‘#FFA0A0‘?‘#A0A0FF‘])
????cm_dark?=?mpl.colors.ListedColormap([‘g‘?‘r‘?‘b‘])

????#?alpha?=?0.5
????plt.pcolormesh(x1?x2?grid_hat?cmap=cm_light)??#?預(yù)測(cè)值的顯示
????#?plt.plot(x[:?0]?x[:?1]?‘o‘?alpha=alpha?color=‘blue‘?markeredgecolor=‘k‘)
????plt.scatter(x[:?0]?x[:?1]?c=np.squeeze(y)?edgecolor=‘k‘?s=50?cmap?=?cm_dark?)??#?圈中測(cè)試集樣本
????plt.scatter(x_test[:?0]?x_test[:?1]?s=120?facecolors=‘none‘?zorder=10)??#?圈中測(cè)試集樣本
????plt.xlabel(‘sepal?length‘?fontsize=13)
????plt.ylabel(‘sepal?width‘?fontsize=13)
????plt.xlim(x1_min?x1_max)
????plt.ylim(x2_min?x2_max)
????plt.title(‘SVM?feature‘?fontsize=15)
????#?plt.grid()
????plt.show()

#1.數(shù)據(jù)準(zhǔn)備
#1.1加載數(shù)據(jù)
#使用numpy中的loadtxt讀入數(shù)據(jù)文件
filepath=‘./iris.txt‘??#?數(shù)據(jù)文件路徑
#注意數(shù)據(jù)的讀取,delimiter參數(shù)是根據(jù)txt文件中的分隔符來(lái)設(shè)置的!
data=np.loadtxt(filepathdtype=floatdelimiter=Noneconverters={4:iris_type})

#1.2數(shù)據(jù)分割
X?y?=?np.split(data?(4)axis=1)??#?np.split?按照列(axis=1)進(jìn)行分割,從第四列開(kāi)始往后的作為y?數(shù)據(jù),之前的作為X?數(shù)據(jù)。函數(shù)?split(數(shù)據(jù),分割位置,軸=1(水平分割)?or?0(垂直分割))。
x?=?X[:0:2]??#?在?X中取前兩列作為特征(為了后期的可視化畫(huà)圖更加直觀,故只取前兩列特征值向量進(jìn)行訓(xùn)練)

x_trainx_testy_trainy_test=model_selection.train_test_split(xyrandom_state=1test_size=0.3)
#

?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----

?????文件????????408??2019-08-17?11:27??SVM鳶尾花分類Python實(shí)現(xiàn)\.idea\01-鳶尾花分類SVM.iml

?????文件????????138??2019-08-17?11:26??SVM鳶尾花分類Python實(shí)現(xiàn)\.idea\encodings.xml

?????文件????????294??2019-08-17?11:27??SVM鳶尾花分類Python實(shí)現(xiàn)\.idea\misc.xml

?????文件????????301??2019-08-17?11:26??SVM鳶尾花分類Python實(shí)現(xiàn)\.idea\modules.xml

?????文件???????9622??2019-08-17?18:26??SVM鳶尾花分類Python實(shí)現(xiàn)\.idea\workspace.xml

?????文件???????3808??2019-08-17?13:45??SVM鳶尾花分類Python實(shí)現(xiàn)\iris.txt

?????文件???????5920??2019-08-17?18:26??SVM鳶尾花分類Python實(shí)現(xiàn)\iris_svm.py

?????目錄??????????0??2019-08-17?18:27??SVM鳶尾花分類Python實(shí)現(xiàn)\.idea

?????目錄??????????0??2019-08-17?18:27??SVM鳶尾花分類Python實(shí)現(xiàn)

-----------?---------??----------?-----??----

????????????????20491????????????????????9


評(píng)論

共有 條評(píng)論