题目链接 吃了个饭就跑完了^--^
#-*- coding=utf-8 -*-
import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics
import time
s = time.clock()
knn = KNeighborsClassifier(n_neighbors=20)
digit_data = pd.read_csv("C:/Users\hanghang/Desktop/Digit Recognizer/train.csv")
y = digit_data['label']
x_col = [x for x in digit_data.columns if x != 'label']
x = digit_data[x_col]
knn.fit(x,y)
digit_test = pd.read_csv("C:/Users\hanghang/Desktop/Digit Recognizer/test.csv")
predicted = knn.predict(digit_test)
# print(metrics.classification_report(expected,predicted))
# print(metrics.confusion_matrix(expected,predicted))
res = pd.DataFrame({'ImageId':range(1,len(predicted)+1), 'Label':predicted})
res.to_csv("C:/Users\hanghang/Desktop/Digit Recognizer/result.csv", index=False)
spend = (time.clock() - s)
# 总共花费时间
print("Spend time ",int(spend/60))