-
Notifications
You must be signed in to change notification settings - Fork 1
/
csvReader.py
94 lines (86 loc) · 2.92 KB
/
csvReader.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import csv
import numpy as np
def readClassificationFile(path):
with open(path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader, None)
X = []
Y = []
for row in csv_reader:
X.append(np.array([[row[0], row[1]]]).T.astype(np.float))
if int(row[2])==1:
Y.append(np.array([[1]]).T)
else:
Y.append(np.array([[0]]).T)
return X,Y
def readClassification3ClassesFile(path):
with open(path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader, None)
X = []
Y = []
for row in csv_reader:
X.append(np.array([[row[0], row[1]]]).T.astype(np.float))
if int(row[2])==1:
Y.append(np.array([[1, 0, 0]]).T)
elif int(row[2])==2:
Y.append(np.array([[0, 1, 0]]).T)
else:
Y.append(np.array([[0, 0, 1]]).T)
return X,Y
def readRegressionFile(path):
with open(path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader, None)
X = []
Y = []
for row in csv_reader:
X.append(np.array([[row[0]]]).T.astype(np.float))
Y.append(np.array([[float(row[1])]]).T)
return X,Y
def readmnistAsOneLineTraining(path):
with open(path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader, None)
X = []
Y = []
for row in csv_reader:
y = np.zeros((10,1))
y[int(row[0]),0] = 1
Y.append(y)
X.append((np.array([row[1:785]]).T.astype(float))/255)
return X,Y
def readmnistAsOneLineTest(path):
with open(path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader, None)
X = []
for row in csv_reader:
X.append((np.array([row[0:784]]).T.astype(float))/255)
return X
def readmnistAsOneLinePCA(path):
with open(path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
X = []
for row in csv_reader:
X.append((np.array([row[0:784]]).T.astype(float)))
return X
def saveMnistResult(path, Y):
with open(path, 'w', newline='') as csvfile:
fields = ['ImageId', 'Label']
writer = csv.DictWriter(csvfile, fieldnames=fields)
writer.writeheader()
for i in range(len(Y)):
writer.writerow({'ImageId' : i+1, 'Label' : np.argmax(Y[i])})
def readmnistAsPictureTraining(path):
with open(path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader, None)
X = []
Y = []
for row in csv_reader:
y = np.zeros((10,1))
y[int(row[0]),0] = 1
Y.append(y)
X.append(np.array([row[1:785]]).T.astype(int).reshape((28,28)))
return X,Y