-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_reg.py
executable file
·103 lines (89 loc) · 3.68 KB
/
log_reg.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
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from my_logistic_regression import MyLogisticRegression as MyLR
ALPHA = 1e-4
MAX_ITER = 2 ** 7
if __name__ == "__main__":
# Read dataset
train_df = pd.read_csv("Dataset/mnist_train.csv")
test_df = pd.read_csv("Dataset/mnist_test.csv")
y_train = train_df.pop('label')
y_test = test_df.pop('label')
# Convert data to numpy arrays
x_train = train_df.to_numpy()
y_train = y_train.to_numpy().reshape(-1, 1)
x_test = test_df.to_numpy()
y_test = y_test.to_numpy().reshape(-1, 1)
# Visualize 10 random images each from the training and test sets
rng = np.random.default_rng(1337)
(m_train, n) = x_train.shape
rand_idx = rng.choice(m_train, 10, replace=False)
imgs = x_train[rand_idx, :]
labels = y_train[rand_idx, :]
fig, axs = plt.subplots(2, 10)
for i, ax in enumerate(axs.flat[:10]):
img = imgs[i].reshape(28, 28)
ax.imshow(img, cmap='gray', vmin=0, vmax=255)
ax.set_title(f'Label = {labels[i, 0]}', fontsize=6)
ax.set_axis_off()
m_test = x_test.shape[0]
rand_idx = rng.choice(m_test, 10, replace=False)
imgs = x_test[rand_idx, :]
labels = y_test[rand_idx, :]
for i, ax in enumerate(axs.flat[10:]):
img = imgs[i].reshape(28, 28)
ax.imshow(img, cmap='gray', vmin=0, vmax=255)
ax.set_title(f'Label = {labels[i, 0]}', fontsize=6)
ax.set_axis_off()
plt.show()
# Train a logistic regression model using the one-vs-all method
thetas = []
for d in range(10):
# Train a logistic regression model to determine
# whether an image is of digit d or not
print(f'Training logistic regression model number {d + 1}')
mylr = MyLR(np.zeros((n + 1, 1)), alpha=ALPHA,
max_iter=MAX_ITER, lambda_=0.1)
new_y_train = (y_train == d).astype(float)
mylr.fit_(x_train, new_y_train)
thetas.append(mylr.thetas)
all_thetas = np.hstack(thetas)
# Calculate model predictions on the training set
X_train = np.hstack([np.ones((m_train, 1)), x_train])
all_predictions = X_train @ all_thetas
predictions_train = np.argmax(all_predictions, axis=1).reshape(-1, 1)
# Calculate accuracy
acc = np.mean(predictions_train == y_train)
print(f'Training set accuracy = {acc}')
# Calculate model predictions on the test set
X_test = np.hstack([np.ones((m_test, 1)), x_test])
all_predictions = X_test @ all_thetas
predictions_test = np.argmax(all_predictions, axis=1).reshape(-1, 1)
# Calculate accuracy
acc = np.mean(predictions_test == y_test)
print(f'Test set accuracy = {acc}')
# Visualize 10 random images each from the training and test sets
# along with our model's predictions
rand_idx = rng.choice(m_train, 10, replace=False)
imgs = x_train[rand_idx, :]
labels = y_train[rand_idx, :]
predictions = predictions_train[rand_idx, :]
fig, axs = plt.subplots(2, 10)
for i, ax in enumerate(axs.flat[:10]):
img = imgs[i].reshape(28, 28)
ax.imshow(img, cmap='gray', vmin=0, vmax=255)
ax.set_title(f'L = {labels[i, 0]} P = {predictions[i, 0]}', fontsize=6)
ax.set_axis_off()
rand_idx = rng.choice(m_test, 10, replace=False)
imgs = x_test[rand_idx, :]
labels = y_test[rand_idx, :]
predictions = predictions_test[rand_idx, :]
for i, ax in enumerate(axs.flat[10:]):
img = imgs[i].reshape(28, 28)
ax.imshow(img, cmap='gray', vmin=0, vmax=255)
ax.set_title(f'L = {labels[i, 0]} P = {predictions[i, 0]}', fontsize=6)
ax.set_axis_off()
fig.suptitle('(L)abels vs (P)redictions')
plt.show()