Python Opencv KNN英文本母識別

2021-07-01 22:32:32 字數 1691 閱讀 2416

特徵集分析

資料集為letter-recognition.data,一共為20000條資料,以逗號分隔,資料例項如下所示,第一列為字母標記,剩下的為不同的特徵。

t,2,8,3,5,1,8,13,0,6,6,10,8,0,8,0,8

學習方法

1、讀入資料,並去除分隔號

2、將資料第一列作為標記,剩下的為訓練資料

3、初始化分類器並利用訓練資料進行訓練

4、利用測試資料驗證準確率

**

import cv2

import numpy as np

import matplotlib.pyplot as plt

print 'load data'

data = np.loadtxt('letter-recognition.data',dtype = 'float32',delimiter = ',',

converters= )

print 'split as train,test'

train,test = np.vsplit(data,2)

print 'train.shape:\t',train.shape

print 'test.shape:\t',test.shape

print 'split train as the response,traindata'

response,traindata = np.hsplit(train,[1])

print 'response.shape:\t',response.shape

print 'traindata.shape:\t',traindata.shape

print 'split the test as response,traindata'

restest,testdata = np.hsplit(test,[1])

print 'init the knn'

knn = cv2.knearest()

knn.train(traindata,response)

print 'test the knn'

ret,result,neighbours,dist = knn.find_nearest(testdata,5)

print 'the rate:'

correct = np.count_nonzero(result == restest)

accuracy = correct*100.0/10000

print 'accuracy is',accuracy,'%'

結果

load data

split as train,test

train.shape:(10000, 17)

test.shape:(10000, 17)

split train as the response,traindata

response.shape:(10000, 1)

traindata.shape:(10000, 16)

split the test as response,traindata

init the knn

test the knn

the rate:

accuracy is 93.22 %

資料集

英文本母排序

116.英文本母排序 20分 c時間限制 3000 毫秒 c記憶體限制 3000 kb 題目內容 編寫乙個程式,當輸入英文本串時,計算機將這個句子中的英文本母按字典字母順序重新排列,排列後的單詞的 長度要與原始句子中的長度相同,並且要求只對 到 的字母重新排列,其它字元保持原來的狀態。輸入描述 乙個...

英文本母的排序

題目內容 編寫乙個程式,當輸入英文本串時,計算機將這個句子中的英文本母按字典字母順序重新排列,排列後的單詞的 長度要與原始句子中的長度相同,並且要求只對 到 的字母重新排列,其它字元保持原來的狀態。輸入描述 乙個字串,包括大寫英文本母,以及其他字元 輸出描述 字串的重新排列,只是大寫字母按字典順序,...

IOS 系統文字轉英文本母

nsmutablestring ms1 nsmutablestring alloc initwithstring 重力加速度 if cfstringtransform bridge cfmutablestringref ms1,0,kcfstringtransformmandarinlatin,no...