99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀機器學(xué)習(xí)python實戰(zhàn)之手寫數(shù)字識別
機器學(xué)習(xí)python實戰(zhàn)之手寫數(shù)字識別
2018-05-21
收藏

機器學(xué)習(xí)python實戰(zhàn)之手寫數(shù)字識別

看了上一篇內(nèi)容之后,相信對K近鄰算法有了一個清晰的認(rèn)識,今天的內(nèi)容——手寫數(shù)字識別是對上一篇內(nèi)容的延續(xù),這里也是為了自己能更熟練的掌握k-NN算法。

我們有大約2000個訓(xùn)練樣本和1000個左右測試樣本,訓(xùn)練樣本所在的文件夾是trainingDigits,測試樣本所在的文件夾是testDigits。文本文件中是0~9的數(shù)字,但是是用二值圖表示出來的,如圖。我們要做的就是使用訓(xùn)練樣本訓(xùn)練模型,并用測試樣本來檢測模型的性能。

首先,我們需要將文本文件中的內(nèi)容轉(zhuǎn)化為向量,因為圖片大小是32*32,所以我們可以將其轉(zhuǎn)化為1*1024的向量。具體代碼實現(xiàn)如下:

defimg2vector(filename):
  imgVec=zeros((1,1024))
  file=open(filename)
  foriinrange(32):
    lines=file.readline()
    forjinrange(32):
      imgVec[0,32*i+j]=lines[j]
  returnimgVec
實現(xiàn)了圖片到向量的轉(zhuǎn)化之后,我們就可以對測試文件中的內(nèi)容進(jìn)行識別了。這里的識別我們可以使用上一篇中的自定義函數(shù)classify0,這個函數(shù)的第一個參數(shù)是測試向量,第二個參數(shù)是訓(xùn)練數(shù)據(jù)集,第三個參數(shù)是訓(xùn)練集的標(biāo)簽。所以,我們首先需要將訓(xùn)練數(shù)據(jù)集轉(zhuǎn)化為(1934*1024)的矩陣,1934這里是訓(xùn)練集的組數(shù)即trainingDigits目錄下的文件數(shù),其對應(yīng)的標(biāo)簽轉(zhuǎn)化為(1*1934)的向量。之后要編寫的代碼就是對測試數(shù)據(jù)集中的每個文本文件進(jìn)行識別,也就是需要將每個文件都轉(zhuǎn)化成一個(1*1024)的向量,再傳入classify0函數(shù)的第一個形參。整體代碼如下:

defhandWriteNumClassTest():
  NumLabels=[]
  TrainingDirfile=listdir(r'D:\ipython\num_recognize\trainingDigits')#文件目錄
  L=len(TrainingDirfile) #該目錄中有多少文件
  TrainMat=zeros((L,1024))
  foriinrange(L):
    file_n=TrainingDirfile[i]
    fileName=file_n.split('.')[0]
    ClassName=int(file_n.split('_')[0])
    NumLabels.append(ClassName)
    TrainMat[i,:]=img2vector(r'D:\ipython\num_recognize\trainingDigits\%s'%file_n)
  TestfileDir=listdir(r'D:\ipython\num_recognize\testDigits')
  error_cnt=0.0
  M=len(TestfileDir)
  forjinrange(M):
    Testfile=TestfileDir[j]
    TestfileName=Testfile.split('.')[0]
    TestClassName=int(Testfile.split('_')[0])
    TestVector=img2vector(r'D:\ipython\num_recognize\testDigits\%s'%Testfile)
    result=classify0(TestVector,TrainMat,NumLabels,3)
    print('the result is %d,the real answer is %d\n'%(result,TestClassName))
    ifresult!=TestClassName:
      error_cnt+=1
  print('the total num of errors is %f\n'%error_cnt)
  print('the error rate is %f\n'%(error_cnt/float(M)))

這里需要首先導(dǎo)入listdir方法,from os import listdir,它可以列出給定目錄的文件名。對于測試的每個文件,如果識別的分類結(jié)果跟真實結(jié)果不一樣,則錯誤數(shù)+1,最終用錯誤數(shù)/測試總數(shù) 來表示該模型的性能。下面給出結(jié)果

這里測試的總共946個項目中,一共有10個出現(xiàn)了錯誤,出錯率為1%,這個性能還是可以接受的。有了上一篇內(nèi)容的理解,這篇就簡單多了吧!

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }