-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_data.py
More file actions
33 lines (27 loc) · 1005 Bytes
/
read_data.py
File metadata and controls
33 lines (27 loc) · 1005 Bytes
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
import csv
import numpy as np
def read_file(file_name):
# Reads the file and returns two numpy arrays, the first containing the feature-dataset and
# the second list containing the labels.
X_data = []
y_data = []
with open(file_name, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
label = int(float(row[-1]))
X = np.array([float(feature) for feature in row[:-1]])
X_data.append(X)
y_data.append(label)
return np.array(X_data), np.array(y_data)
def one_hot(labels, classes=10):
# Creates a one_hot representation of the labels.
out = []
for label in labels:
new_label = [0.0] * classes
new_label[label] = 1.0
out.append(new_label)
return np.array(out)
def get_num_of_classes(y_data):
# This should be the maximum value + 1.
max_value = np.max(y_data)
return max_value + 1