基于knn算法识别手写数字

#coding:gb2312
import numpy as np
from sklearn import neighbors
import csv


def showimg(line):
    #创建一个一维数组
    returnVect = np.zeros((1, 784))
    list=line.split(',')
    num=list[0]
    list.pop(0)
    new_image = np.zeros((28, 28))
    count=0
    for i in range(28):
        for j in range(28):
            returnVect[0, i * 28 + j] = int(list[i * 28 + j])
            count=count+1
    return num,returnVect

def showtestimg(line):
    #创建一个一维数组
    returnVect = np.zeros((1, 784))
    list=line.split(',')
    new_image = np.zeros((28, 28))
    count=0
    for i in range(28):
        for j in range(28):
            returnVect[0, i * 28 + j] = int(list[i * 28 + j])
            count = count + 1
    return returnVect

def readtest():
    testingMat = np.zeros((28000, 784))
    filePath = 'D:\\data\\test.csv'
    df = open(filePath)
    lines = df.readlines()
    count = 0;
    for line in lines:
        count = count + 1
        if (count != 1):
            print("第" + str(count) + "行")
            vect = showtestimg(line)
            testingMat[count - 2, :] = vect
    return testingMat

if __name__ == "__main__":
    knn = neighbors.KNeighborsClassifier()
    #初始化样本数据矩阵(M*784)
    trainingMat = np.zeros((42000, 784))
    filePath = 'D:\\data\\train.csv'
    df = open(filePath)
    lines = df.readlines()
    count=0;
    label=[]
    for line in lines:
        count=count+1
        if(count!=1):
            print("第"+str(count)+"行")
            num,vect=showimg(line)
            label.append(num)
            trainingMat[count-2, :]=vect
    print("开始训练模型")
    knn.fit(trainingMat, label)
    print("读入测试数据")
    testingMat=readtest()
    print("开始预测")
    results = knn.predict(testingMat)
    print(len(results))
    print("写入文件")
    with open("D:/data/result.csv", "w",newline='') as csvfile:
        writer = csv.writer(csvfile)
        # 先写入columns_name
        writer.writerow(["ImageId", "Label"])
        for i in range(len(results)):
            writer.writerow([i+1, results[i]])


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值