-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP01.py
More file actions
190 lines (140 loc) · 4.88 KB
/
Copy pathP01.py
File metadata and controls
190 lines (140 loc) · 4.88 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# P1
def read_csv_using_pandas(csv_path='exam_scores.csv'):
data = pd.read_csv(csv_path)
print(data.shape)
print(data.head())
return data
def parse_pd_data(data, fields=['Circuit',
'DataStructure',
'MachineIntelligence']):
values = []
## Fill In Your Code Here ##
values = np.array(list(data[field] for field in fields))
############################
return values
def plot_data(values):
assert len(values) == 3
assert type(values[0]) == np.ndarray
figsize = (6, 4)
title_fontsize = 20
label_fontsize = 15
fig = plt.figure(figsize=figsize)
ax = Axes3D(fig)
## Fill In Your Code Here ##
x = values[0]
y = values[1]
z = values[2]
# scatter
ax.scatter(x,y,z)
# set title. use title_fontsize above.
plt.suptitle('Score Distributions', fontsize = title_fontsize)
# set labels for each axes. use label_fontsize above.
ax.set_xlabel('Circuit', fontsize = label_fontsize)
ax.set_ylabel('DS', fontsize = label_fontsize)
ax.set_zlabel('MI', fontsize = label_fontsize)
############################
plt.show()
return fig
# P2
def prepare_dataset_for_linear_regression(values):
bias = np.ones(len(values[0]))
X = np.array([bias, values[0], values[1]]).T
y = np.array(values[2])
return X, y
class LinearRegression:
def __init__(self, lr=0.0001, iterations=100000):
self.lr = lr
self.iterations = iterations
self.average_rss_history = []
def fit(self, X, y):
# N = number of training set
N = len(y)
## Fill In Your Code Here ##
# initialize w
self.w = np.zeros(3)
############################
for i in range(self.iterations):
## Fill In Your Code Here ##
# implement gradient descent
y_predict = self.predict(X)
average_rss = ((y - y_predict)**2).sum()/N
gradient_rss = -2 * X.T.dot(y - y_predict)/N
self.w -= self.lr * gradient_rss
############################
self.average_rss_history.append(average_rss)
def predict(self, X):
## Fill In Your Code Here ##
pred_y = X.dot(self.w)
############################
return pred_y
def plot_average_rss_history(iterations, history):
figsize = (6,4)
title_fontsize = 20
label_fontsize = 15
# plot rss_avg history over iterations
fig = plt.figure(figsize=figsize)
plt.ylim(0,100)
## Fill In Your Code Here ##
y = history
x = list(i for i in range(iterations))
# plot
plt.plot(x,y)
# set title
plt.title('Average RSS History', fontsize = title_fontsize)
# set labels for axes
plt.xlabel('Iterations', fontsize = label_fontsize)
plt.ylabel('Average_RSS', fontsize = label_fontsize)
############################
plt.show()
return fig
# P3
def plot_data_with_wireframe(values, w, wireframe_color='red'):
assert len(w) == 3
title_fontsize = 20
label_fontsize = 15
figsize = (6,4)
def make_meshgrids(x, y, num=10):
## Fill In Your Code Here ##
# make meshgrids for 3D plot.
# HINT : use np.linspace function
x_margin = (max(x) - min(x)) * 0.05
y_margin = (max(y) - min(y)) * 0.05
x_linspace = np.linspace(min(x) - x_margin, max(x) + x_margin, num)
y_linspace = np.linspace(min(y) - y_margin, max(y) + y_margin, num)
############################
x_grid, y_grid = np.meshgrid(x_linspace, y_linspace)
return x_grid, y_grid
x_grid, y_grid = make_meshgrids(values[0], values[1])
# For one fig, one figure of plot (either in 2D plane or in 3D space)
fig = plt.figure(figsize=figsize)
# ax of axis
ax = Axes3D(fig)
## Fill In Your Code Here ##
# X, Y = np.meshgrid(values[0], values[1])
# Z = np.array([1, X, Y]).dot(w)
Z = np.array([1, x_grid, y_grid]).dot(w)
# scatter
# scatter takes arrays for inputs
ax.scatter(values[0], values[1], values[2])
# set title. use title_fontsize above.
plt.suptitle('Score Distributions', fontsize = title_fontsize)
# set labels for each axes. use label_fontsize above.
ax.set_xlabel('Circuit', fontsize = label_fontsize)
ax.set_ylabel('DS', fontsize = label_fontsize)
ax.set_zlabel('MI', fontsize = label_fontsize)
# plot wireframe
# plot_wireframe takes (2 arrays and one 2D matrix)
ax.plot_wireframe(x_grid, y_grid, Z, color = wireframe_color)
############################
plt.show()
return fig
def get_closed_form_solution(X, y):
w = np.zeros(X.shape[1])
## Fill In Your Code Here ##
w = np.linalg.inv(X.T.dot(X)).dot(X.T.dot(y))
############################
return w