-
Notifications
You must be signed in to change notification settings - Fork 0
/
KNN解决数字识别问题.py
46 lines (41 loc) · 1006 Bytes
/
KNN解决数字识别问题.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pandas as pd
from pandas import *
import numpy as np
def loadTrainData():
l=[]
with open("train.csv") as file:
lines=csv.reader(file)
for line in lines:
l.append(line)
l.remove(0)
l=np.array(l)
label=l[:,0]
data=l[:,1:]
return normalizing(toInt(data)),toInt(label)
def toInt(array):
array=mat(array)
m,n=shape(array)
newArray=zeros((m,n))
for i in xrange(m):
for j in xrange(n):
newArray[i,j]=int(array[i,j])
return newArray
def nomalizing(array):
m,n=shape(array)
for i in xrange(m):
for j in xrange(n):
if array[i,j]!=0:
array[i,j]=1
return array
def loadTestData():
l=[]
with open('test.csv') as file:
lines=csv.reader(file)
for line in lines:
l.append(line)
l.remove(l[0])
data=array(l)
return nomalizing(toInt(data))
def loadTestResult():
l=[]
with open("")