-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
70 lines (50 loc) · 1.33 KB
/
util.py
File metadata and controls
70 lines (50 loc) · 1.33 KB
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
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
def get_transformed_data():
print("Reading in and transforming data...")
df = pd.read_csv('train.csv')
data = df.to_numpy().astype(np.float32)
np.random.shuffle(data)
X = data[:, 1:]
mu = X.mean(axis=0)
X = X - mu # center the data
pca = PCA()
Z = pca.fit_transform(X)
Y = data[:, 0]
return Z, Y, pca, mu
def get_normalized_data():
print("Reading in and transforming data...")
df = pd.read_csv('train.csv')
data = df.to_numpy().astype(np.float32)
np.random.shuffle(data)
X = data[:, 1:]
mu = X.mean(axis=0)
std = X.std(axis=0)
np.place(std, std == 0, 1)
X = (X - mu) / std # normalize the data
Y = data[:, 0]
return X, Y
def forward(X, W, b):
a = X.dot(W) + b
expa = np.exp(a)
y = expa / expa.sum(axis=1, keepdims=True)
return y
def predict(p_y):
return np.argmax(p_y, axis=1)
def error_rate(p_y, t):
prediction = predict(p_y)
return np.mean(prediction != t)
def cost(p_y, t):
tot = t * np.log(p_y)
return -tot.sum()
def gradW(t, y, X):
return X.T.dot(t - y)
def gradb(t, y):
return (t - y).sum(axis=0)
def y2indicator(y):
N = len(y)
ind = np.zeros((N, 10))
for i in range(N):
ind[i, int(y[i])] = 1
return ind