python機(jī)器學(xué)習(xí)sklearn實(shí)現(xiàn)識(shí)別數(shù)字_第1頁
python機(jī)器學(xué)習(xí)sklearn實(shí)現(xiàn)識(shí)別數(shù)字_第2頁
python機(jī)器學(xué)習(xí)sklearn實(shí)現(xiàn)識(shí)別數(shù)字_第3頁
python機(jī)器學(xué)習(xí)sklearn實(shí)現(xiàn)識(shí)別數(shù)字_第4頁
python機(jī)器學(xué)習(xí)sklearn實(shí)現(xiàn)識(shí)別數(shù)字_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第python機(jī)器學(xué)習(xí)sklearn實(shí)現(xiàn)識(shí)別數(shù)字目錄簡介數(shù)據(jù)集數(shù)據(jù)處理數(shù)據(jù)分離訓(xùn)練數(shù)據(jù)數(shù)據(jù)可視化完整代碼

簡介

本文主要簡述如何通過sklearn模塊來進(jìn)行預(yù)測和學(xué)習(xí),最后再以圖表這種更加直觀的方式展現(xiàn)出來

數(shù)據(jù)集

學(xué)習(xí)數(shù)據(jù)

預(yù)測數(shù)據(jù)

數(shù)據(jù)處理

數(shù)據(jù)分離

因?yàn)槲覀兇蜷_我們的的學(xué)習(xí)數(shù)據(jù)集,最后一項(xiàng)是我們的真實(shí)數(shù)值,看過小唐上一篇的人都知道,老規(guī)矩先進(jìn)行拆分,前面的特征放一塊,后面的真實(shí)值放一塊,同時(shí)由于數(shù)據(jù)沒有列名,我們選擇使用iloc[]來實(shí)現(xiàn)分離

defshuju(tr_path,ts_path,sep='\t'):

train=pd.read_csv(tr_path,sep=sep)

test=pd.read_csv(ts_path,sep=sep)

#特征和結(jié)果分離

train_features=train.iloc[:,:-1].values

train_labels=train.iloc[:,-1].values

test_features=test.iloc[:,:-1].values

test_labels=test.iloc[:,-1].values

returntrain_features,test_features,train_labels,test_labels

訓(xùn)練數(shù)據(jù)

我們在這里直接使用sklearn函數(shù),通過選擇模型,然后直接生成其識(shí)別規(guī)則

#訓(xùn)練數(shù)據(jù)

deftrain_tree(*data):

x_train,x_test,y_train,y_test=data

clf=DecisionTreeClassifier()

clf.fit(x_train,y_train)

print("學(xué)習(xí)模型預(yù)測成績:{:.4f}".format(clf.score(x_train,y_train)))

print("實(shí)際模型預(yù)測成績:{:.4f}".format(clf.score(x_test,y_test)))

#返回學(xué)習(xí)模型

returnclf

數(shù)據(jù)可視化

為了讓我們的觀察更加直觀,我們還可以使用matplotlib來進(jìn)行觀測

defplot_imafe(test,test_labels,preds):

plt.ion()

plt.show()

foriinrange(50):

label,pred=test_labels[i],preds[i]

title='實(shí)際值:{},predict{}'.format(label,pred)

img=test[i].reshape(28,28)

plt.imshow(img,cmap="binary")

plt.title(title)

plt.show()

print('done')

結(jié)果

完整代碼

importpandasaspd

fromsklearn.treeimportDecisionTreeClassifier

importmatplotlib.pyplotasplt

defshuju(tr_path,ts_path,sep='\t'):

train=pd.read_csv(tr_path,sep=sep)

test=pd.read_csv(ts_path,sep=sep)

#特征和結(jié)果分離

train_features=train.iloc[:,:-1].values

train_labels=train.iloc[:,-1].values

test_features=test.iloc[:,:-1].values

test_labels=test.iloc[:,-1].values

returntrain_features,test_features,train_labels,test_labels

#訓(xùn)練數(shù)據(jù)

deftrain_tree(*data):

x_train,x_test,y_train,y_test=data

clf=DecisionTreeClassifier()

clf.fit(x_train,y_train)

print("學(xué)習(xí)模型預(yù)測成績:{:.4f}".format(clf.score(x_train,y_train)))

print("實(shí)際模型預(yù)測成績:{:.4f}".format(clf.score(x_test,y_test)))

#返回學(xué)習(xí)模型

returnclf

defplot_imafe(test,test_labels,preds):

plt.ion()

plt.show()

foriinrange(50):

label,pred=test_labels[i],preds[i]

title='實(shí)際值:{},predict{}'.format(label,pred)

img=test[i].reshape(28,28)

plt.imshow(img,cmap="binary")

plt.title(title)

plt.show()

print('done')

train_features,test_features,train_labels,test_labels=shuju(r"C:\Users\twy\PycharmProjects\1\train_images.csv",r"C:\Users\twy\PycharmProjects\1\test_images.csv")

clf=train_tree

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論