Beispiel #1
0
def KNN(testData, testLabel, trainData, trainLabel):
    # 使用二折交叉验证
    missCount = 0
    print('开始时间:', time.strftime('%Y-%m-%d %H:%M:%S'))
    start = time.time()
    for i in range(testData.shape[0]):
        out = knn.kNNClassify(testData[i], trainData, trainLabel, 1)
        if out != testLabel[i]:
            missCount += 1
    t = testData
    testData = trainData
    trainData = t
    t = testLabel
    testLabel = trainLabel
    trainLabel = t
    for i in range(testData.shape[0]):
        out = knn.kNNClassify(testData[i], trainData, trainLabel, 1)
        if out != testLabel[i]:
            missCount += 1
    # 运行结果显示
    print('结束时间:', time.strftime('%Y-%m-%d %H:%M:%S'))
    end = time.time()
    print('test dataset total:',
          len(testLabel) + len(trainLabel), ',miss count:', missCount,
          ',correct ratio:',
          1 - missCount / (len(testLabel) + len(trainLabel)), ',miss ratio:',
          missCount / (len(testLabel) + len(trainLabel)), ',spend:',
          end - start)
def testHandWritingClass(datapath):
	print "step 1: load data..."
	train_x, train_y, test_x, test_y = loadDataSet(datapath)

	print "step 2: training..."
	pass

	print "step 3: testing..."
	numTestSamples = test_x.shape[0]
	matchCount = 0
	for i in xrange(numTestSamples):
		predict = knn.kNNClassify(test_x[i], train_x, train_y, 3)
		if predict == test_y[i]:
			matchCount += 1
	accuracy = float(matchCount) / numTestSamples

	print "step 4: show the result..."
	print 'The classify accuracy is: %.2f%%' % (accuracy * 100)
def testHandWritingClass(datapath):
    print "step 1: load data..."
    train_x, train_y, test_x, test_y = loadDataSet(datapath)

    print "step 2: training..."
    pass

    print "step 3: testing..."
    numTestSamples = test_x.shape[0]
    matchCount = 0
    for i in xrange(numTestSamples):
        predict = knn.kNNClassify(test_x[i], train_x, train_y, 3)
        if predict == test_y[i]:
            matchCount += 1
    accuracy = float(matchCount) / numTestSamples

    print "step 4: show the result..."
    print 'The classify accuracy is: %.2f%%' % (accuracy * 100)
Beispiel #4
0
import knn
from numpy import *
#生成数据集和类别标签
dataSet,labels = knn.createDataSet()
#定义一个未知类别的数据
testX = array([5.9, 3.1, 5.1, 1.8])
k=3
#调用分类函数对未知数据分类
outputLabel = knn.kNNClassify(testX, dataSet, labels, 3)
print("Your input is:", testX, " and classified to class:", outputLabel)
Beispiel #5
0
# Load training examples and tesing examples

print('hello')

X_train = loadImageSet('E:\\PY\\hand\\train-images.idx3-ubyte')

X_train = X_train.reshape(60000, 784)

y_train = loadLabelSet('E:\\PY\\hand\\train-labels.idx1-ubyte')

y_train = y_train.ravel()

X_test = loadImageSet('E:\\PY\\hand\\t10k-images.idx3-ubyte')

X_test = X_test.reshape(10000, 784)

y_test = loadLabelSet('E:\\PY\\hand\\t10k-labels.idx1-ubyte')

length = X_test.shape[0]

count = 0

for i in range(0, length):

    if (kNNClassify(X_test[i], X_train, y_train, 5) == y_test[i]):
        print(i)
        count = count + 1

print('The accuracy is %.2f%%' % (count * 1.0 * 100 / length))
Beispiel #6
0
# coding=utf-8
import knn  
from numpy import *   
import matplotlib.pyplot as plt
import numpy as np

if __name__ == "__main__" :
    
    # create the dataset
    dataSet, labels = knn.createDataSet()  
    print dataSet
    # set the K value of KNN
    k = 3  
    # classify using kNN 
    ## test1 data(1.2, 1.0)
    testX = array([1.2, 1.0])   

    outputLabel = knn.kNNClassify(testX, dataSet, labels, k)  
    print "Your input is:", testX, "and classified to class: ", outputLabel  
    ## test1  data(0.1, 0.3)
    testX = array([0.1, 0.3])  
    outputLabel = knn.kNNClassify(testX, dataSet, labels, k)  
    print "Your input is:", testX, "and classified to class: ", outputLabel