Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
C0mRD
GitHub Repository: C0mRD/Machine-Learning-Specialization-Coursera
Path: blob/main/C1 - Supervised Machine Learning: Regression and Classification/week2/Optional Labs/lab_utils_multi.py
3283 views
1
import numpy as np
2
import copy
3
import math
4
from scipy.stats import norm
5
import matplotlib.pyplot as plt
6
from mpl_toolkits.mplot3d import axes3d
7
from matplotlib.ticker import MaxNLocator
8
dlblue = '#0096ff'; dlorange = '#FF9300'; dldarkred='#C00000'; dlmagenta='#FF40FF'; dlpurple='#7030A0';
9
plt.style.use('./deeplearning.mplstyle')
10
11
def load_data_multi():
12
data = np.loadtxt("data/ex1data2.txt", delimiter=',')
13
X = data[:,:2]
14
y = data[:,2]
15
return X, y
16
17
##########################################################
18
# Plotting Routines
19
##########################################################
20
21
def plt_house_x(X, y,f_wb=None, ax=None):
22
''' plot house with aXis '''
23
if not ax:
24
fig, ax = plt.subplots(1,1)
25
ax.scatter(X, y, marker='x', c='r', label="Actual Value")
26
27
ax.set_title("Housing Prices")
28
ax.set_ylabel('Price (in 1000s of dollars)')
29
ax.set_xlabel(f'Size (1000 sqft)')
30
if f_wb is not None:
31
ax.plot(X, f_wb, c=dlblue, label="Our Prediction")
32
ax.legend()
33
34
35
def mk_cost_lines(x,y,w,b, ax):
36
''' makes vertical cost lines'''
37
cstr = "cost = (1/2m)*1000*("
38
ctot = 0
39
label = 'cost for point'
40
for p in zip(x,y):
41
f_wb_p = w*p[0]+b
42
c_p = ((f_wb_p - p[1])**2)/2
43
c_p_txt = c_p/1000
44
ax.vlines(p[0], p[1],f_wb_p, lw=3, color=dlpurple, ls='dotted', label=label)
45
label='' #just one
46
cxy = [p[0], p[1] + (f_wb_p-p[1])/2]
47
ax.annotate(f'{c_p_txt:0.0f}', xy=cxy, xycoords='data',color=dlpurple,
48
xytext=(5, 0), textcoords='offset points')
49
cstr += f"{c_p_txt:0.0f} +"
50
ctot += c_p
51
ctot = ctot/(len(x))
52
cstr = cstr[:-1] + f") = {ctot:0.0f}"
53
ax.text(0.15,0.02,cstr, transform=ax.transAxes, color=dlpurple)
54
55
56
def inbounds(a,b,xlim,ylim):
57
xlow,xhigh = xlim
58
ylow,yhigh = ylim
59
ax, ay = a
60
bx, by = b
61
if (ax > xlow and ax < xhigh) and (bx > xlow and bx < xhigh) \
62
and (ay > ylow and ay < yhigh) and (by > ylow and by < yhigh):
63
return(True)
64
else:
65
return(False)
66
67
from mpl_toolkits.mplot3d import axes3d
68
def plt_contour_wgrad(x, y, hist, ax, w_range=[-100, 500, 5], b_range=[-500, 500, 5],
69
contours = [0.1,50,1000,5000,10000,25000,50000],
70
resolution=5, w_final=200, b_final=100,step=10 ):
71
b0,w0 = np.meshgrid(np.arange(*b_range),np.arange(*w_range))
72
z=np.zeros_like(b0)
73
n,_ = w0.shape
74
for i in range(w0.shape[0]):
75
for j in range(w0.shape[1]):
76
z[i][j] = compute_cost(x, y, w0[i][j], b0[i][j] )
77
78
CS = ax.contour(w0, b0, z, contours, linewidths=2,
79
colors=[dlblue, dlorange, dldarkred, dlmagenta, dlpurple])
80
ax.clabel(CS, inline=1, fmt='%1.0f', fontsize=10)
81
ax.set_xlabel("w"); ax.set_ylabel("b")
82
ax.set_title('Contour plot of cost J(w,b), vs b,w with path of gradient descent')
83
w = w_final; b=b_final
84
ax.hlines(b, ax.get_xlim()[0],w, lw=2, color=dlpurple, ls='dotted')
85
ax.vlines(w, ax.get_ylim()[0],b, lw=2, color=dlpurple, ls='dotted')
86
87
base = hist[0]
88
for point in hist[0::step]:
89
edist = np.sqrt((base[0] - point[0])**2 + (base[1] - point[1])**2)
90
if(edist > resolution or point==hist[-1]):
91
if inbounds(point,base, ax.get_xlim(),ax.get_ylim()):
92
plt.annotate('', xy=point, xytext=base,xycoords='data',
93
arrowprops={'arrowstyle': '->', 'color': 'r', 'lw': 3},
94
va='center', ha='center')
95
base=point
96
return
97
98
99
# plots p1 vs p2. Prange is an array of entries [min, max, steps]. In feature scaling lab.
100
def plt_contour_multi(x, y, w, b, ax, prange, p1, p2, title="", xlabel="", ylabel=""):
101
contours = [1e2, 2e2,3e2,4e2, 5e2, 6e2, 7e2,8e2,1e3, 1.25e3,1.5e3, 1e4, 1e5, 1e6, 1e7]
102
px,py = np.meshgrid(np.linspace(*(prange[p1])),np.linspace(*(prange[p2])))
103
z=np.zeros_like(px)
104
n,_ = px.shape
105
for i in range(px.shape[0]):
106
for j in range(px.shape[1]):
107
w_ij = w
108
b_ij = b
109
if p1 <= 3: w_ij[p1] = px[i,j]
110
if p1 == 4: b_ij = px[i,j]
111
if p2 <= 3: w_ij[p2] = py[i,j]
112
if p2 == 4: b_ij = py[i,j]
113
114
z[i][j] = compute_cost(x, y, w_ij, b_ij )
115
CS = ax.contour(px, py, z, contours, linewidths=2,
116
colors=[dlblue, dlorange, dldarkred, dlmagenta, dlpurple])
117
ax.clabel(CS, inline=1, fmt='%1.2e', fontsize=10)
118
ax.set_xlabel(xlabel); ax.set_ylabel(ylabel)
119
ax.set_title(title, fontsize=14)
120
121
122
def plt_equal_scale(X_train, X_norm, y_train):
123
fig,ax = plt.subplots(1,2,figsize=(12,5))
124
prange = [
125
[ 0.238-0.045, 0.238+0.045, 50],
126
[-25.77326319-0.045, -25.77326319+0.045, 50],
127
[-50000, 0, 50],
128
[-1500, 0, 50],
129
[0, 200000, 50]]
130
w_best = np.array([0.23844318, -25.77326319, -58.11084634, -1.57727192])
131
b_best = 235
132
plt_contour_multi(X_train, y_train, w_best, b_best, ax[0], prange, 0, 1,
133
title='Unnormalized, J(w,b), vs w[0],w[1]',
134
xlabel= "w[0] (size(sqft))", ylabel="w[1] (# bedrooms)")
135
#
136
w_best = np.array([111.1972, -16.75480051, -28.51530411, -37.17305735])
137
b_best = 376.949151515151
138
prange = [[ 111-50, 111+50, 75],
139
[-16.75-50,-16.75+50, 75],
140
[-28.5-8, -28.5+8, 50],
141
[-37.1-16,-37.1+16, 50],
142
[376-150, 376+150, 50]]
143
plt_contour_multi(X_norm, y_train, w_best, b_best, ax[1], prange, 0, 1,
144
title='Normalized, J(w,b), vs w[0],w[1]',
145
xlabel= "w[0] (normalized size(sqft))", ylabel="w[1] (normalized # bedrooms)")
146
fig.suptitle("Cost contour with equal scale", fontsize=18)
147
#plt.tight_layout(rect=(0,0,1.05,1.05))
148
fig.tight_layout(rect=(0,0,1,0.95))
149
plt.show()
150
151
def plt_divergence(p_hist, J_hist, x_train,y_train):
152
153
x=np.zeros(len(p_hist))
154
y=np.zeros(len(p_hist))
155
v=np.zeros(len(p_hist))
156
for i in range(len(p_hist)):
157
x[i] = p_hist[i][0]
158
y[i] = p_hist[i][1]
159
v[i] = J_hist[i]
160
161
fig = plt.figure(figsize=(12,5))
162
plt.subplots_adjust( wspace=0 )
163
gs = fig.add_gridspec(1, 5)
164
fig.suptitle(f"Cost escalates when learning rate is too large")
165
#===============
166
# First subplot
167
#===============
168
ax = fig.add_subplot(gs[:2], )
169
170
# Print w vs cost to see minimum
171
fix_b = 100
172
w_array = np.arange(-70000, 70000, 1000)
173
cost = np.zeros_like(w_array)
174
175
for i in range(len(w_array)):
176
tmp_w = w_array[i]
177
cost[i] = compute_cost(x_train, y_train, tmp_w, fix_b)
178
179
ax.plot(w_array, cost)
180
ax.plot(x,v, c=dlmagenta)
181
ax.set_title("Cost vs w, b set to 100")
182
ax.set_ylabel('Cost')
183
ax.set_xlabel('w')
184
ax.xaxis.set_major_locator(MaxNLocator(2))
185
186
#===============
187
# Second Subplot
188
#===============
189
190
tmp_b,tmp_w = np.meshgrid(np.arange(-35000, 35000, 500),np.arange(-70000, 70000, 500))
191
z=np.zeros_like(tmp_b)
192
for i in range(tmp_w.shape[0]):
193
for j in range(tmp_w.shape[1]):
194
z[i][j] = compute_cost(x_train, y_train, tmp_w[i][j], tmp_b[i][j] )
195
196
ax = fig.add_subplot(gs[2:], projection='3d')
197
ax.plot_surface(tmp_w, tmp_b, z, alpha=0.3, color=dlblue)
198
ax.xaxis.set_major_locator(MaxNLocator(2))
199
ax.yaxis.set_major_locator(MaxNLocator(2))
200
201
ax.set_xlabel('w', fontsize=16)
202
ax.set_ylabel('b', fontsize=16)
203
ax.set_zlabel('\ncost', fontsize=16)
204
plt.title('Cost vs (b, w)')
205
# Customize the view angle
206
ax.view_init(elev=20., azim=-65)
207
ax.plot(x, y, v,c=dlmagenta)
208
209
return
210
211
# draw derivative line
212
# y = m*(x - x1) + y1
213
def add_line(dj_dx, x1, y1, d, ax):
214
x = np.linspace(x1-d, x1+d,50)
215
y = dj_dx*(x - x1) + y1
216
ax.scatter(x1, y1, color=dlblue, s=50)
217
ax.plot(x, y, '--', c=dldarkred,zorder=10, linewidth = 1)
218
xoff = 30 if x1 == 200 else 10
219
ax.annotate(r"$\frac{\partial J}{\partial w}$ =%d" % dj_dx, fontsize=14,
220
xy=(x1, y1), xycoords='data',
221
xytext=(xoff, 10), textcoords='offset points',
222
arrowprops=dict(arrowstyle="->"),
223
horizontalalignment='left', verticalalignment='top')
224
225
def plt_gradients(x_train,y_train, f_compute_cost, f_compute_gradient):
226
#===============
227
# First subplot
228
#===============
229
fig,ax = plt.subplots(1,2,figsize=(12,4))
230
231
# Print w vs cost to see minimum
232
fix_b = 100
233
w_array = np.linspace(-100, 500, 50)
234
w_array = np.linspace(0, 400, 50)
235
cost = np.zeros_like(w_array)
236
237
for i in range(len(w_array)):
238
tmp_w = w_array[i]
239
cost[i] = f_compute_cost(x_train, y_train, tmp_w, fix_b)
240
ax[0].plot(w_array, cost,linewidth=1)
241
ax[0].set_title("Cost vs w, with gradient; b set to 100")
242
ax[0].set_ylabel('Cost')
243
ax[0].set_xlabel('w')
244
245
# plot lines for fixed b=100
246
for tmp_w in [100,200,300]:
247
fix_b = 100
248
dj_dw,dj_db = f_compute_gradient(x_train, y_train, tmp_w, fix_b )
249
j = f_compute_cost(x_train, y_train, tmp_w, fix_b)
250
add_line(dj_dw, tmp_w, j, 30, ax[0])
251
252
#===============
253
# Second Subplot
254
#===============
255
256
tmp_b,tmp_w = np.meshgrid(np.linspace(-200, 200, 10), np.linspace(-100, 600, 10))
257
U = np.zeros_like(tmp_w)
258
V = np.zeros_like(tmp_b)
259
for i in range(tmp_w.shape[0]):
260
for j in range(tmp_w.shape[1]):
261
U[i][j], V[i][j] = f_compute_gradient(x_train, y_train, tmp_w[i][j], tmp_b[i][j] )
262
X = tmp_w
263
Y = tmp_b
264
n=-2
265
color_array = np.sqrt(((V-n)/2)**2 + ((U-n)/2)**2)
266
267
ax[1].set_title('Gradient shown in quiver plot')
268
Q = ax[1].quiver(X, Y, U, V, color_array, units='width', )
269
qk = ax[1].quiverkey(Q, 0.9, 0.9, 2, r'$2 \frac{m}{s}$', labelpos='E',coordinates='figure')
270
ax[1].set_xlabel("w"); ax[1].set_ylabel("b")
271
272
def norm_plot(ax, data):
273
scale = (np.max(data) - np.min(data))*0.2
274
x = np.linspace(np.min(data)-scale,np.max(data)+scale,50)
275
_,bins, _ = ax.hist(data, x, color="xkcd:azure")
276
#ax.set_ylabel("Count")
277
278
mu = np.mean(data);
279
std = np.std(data);
280
dist = norm.pdf(bins, loc=mu, scale = std)
281
282
axr = ax.twinx()
283
axr.plot(bins,dist, color = "orangered", lw=2)
284
axr.set_ylim(bottom=0)
285
axr.axis('off')
286
287
def plot_cost_i_w(X,y,hist):
288
ws = np.array([ p[0] for p in hist["params"]])
289
rng = max(abs(ws[:,0].min()),abs(ws[:,0].max()))
290
wr = np.linspace(-rng+0.27,rng+0.27,20)
291
cst = [compute_cost(X,y,np.array([wr[i],-32, -67, -1.46]), 221) for i in range(len(wr))]
292
293
fig,ax = plt.subplots(1,2,figsize=(12,3))
294
ax[0].plot(hist["iter"], (hist["cost"])); ax[0].set_title("Cost vs Iteration")
295
ax[0].set_xlabel("iteration"); ax[0].set_ylabel("Cost")
296
ax[1].plot(wr, cst); ax[1].set_title("Cost vs w[0]")
297
ax[1].set_xlabel("w[0]"); ax[1].set_ylabel("Cost")
298
ax[1].plot(ws[:,0],hist["cost"])
299
plt.show()
300
301
302
##########################################################
303
# Regression Routines
304
##########################################################
305
306
def compute_gradient_matrix(X, y, w, b):
307
"""
308
Computes the gradient for linear regression
309
310
Args:
311
X : (array_like Shape (m,n)) variable such as house size
312
y : (array_like Shape (m,1)) actual value
313
w : (array_like Shape (n,1)) Values of parameters of the model
314
b : (scalar ) Values of parameter of the model
315
Returns
316
dj_dw: (array_like Shape (n,1)) The gradient of the cost w.r.t. the parameters w.
317
dj_db: (scalar) The gradient of the cost w.r.t. the parameter b.
318
319
"""
320
m,n = X.shape
321
f_wb = X @ w + b
322
e = f_wb - y
323
dj_dw = (1/m) * (X.T @ e)
324
dj_db = (1/m) * np.sum(e)
325
326
return dj_db,dj_dw
327
328
#Function to calculate the cost
329
def compute_cost_matrix(X, y, w, b, verbose=False):
330
"""
331
Computes the gradient for linear regression
332
Args:
333
X : (array_like Shape (m,n)) variable such as house size
334
y : (array_like Shape (m,)) actual value
335
w : (array_like Shape (n,)) parameters of the model
336
b : (scalar ) parameter of the model
337
verbose : (Boolean) If true, print out intermediate value f_wb
338
Returns
339
cost: (scalar)
340
"""
341
m,n = X.shape
342
343
# calculate f_wb for all examples.
344
f_wb = X @ w + b
345
# calculate cost
346
total_cost = (1/(2*m)) * np.sum((f_wb-y)**2)
347
348
if verbose: print("f_wb:")
349
if verbose: print(f_wb)
350
351
return total_cost
352
353
# Loop version of multi-variable compute_cost
354
def compute_cost(X, y, w, b):
355
"""
356
compute cost
357
Args:
358
X : (ndarray): Shape (m,n) matrix of examples with multiple features
359
w : (ndarray): Shape (n) parameters for prediction
360
b : (scalar): parameter for prediction
361
Returns
362
cost: (scalar) cost
363
"""
364
m = X.shape[0]
365
cost = 0.0
366
for i in range(m):
367
f_wb_i = np.dot(X[i],w) + b
368
cost = cost + (f_wb_i - y[i])**2
369
cost = cost/(2*m)
370
return(np.squeeze(cost))
371
372
def compute_gradient(X, y, w, b):
373
"""
374
Computes the gradient for linear regression
375
Args:
376
X : (ndarray Shape (m,n)) matrix of examples
377
y : (ndarray Shape (m,)) target value of each example
378
w : (ndarray Shape (n,)) parameters of the model
379
b : (scalar) parameter of the model
380
Returns
381
dj_dw : (ndarray Shape (n,)) The gradient of the cost w.r.t. the parameters w.
382
dj_db : (scalar) The gradient of the cost w.r.t. the parameter b.
383
"""
384
m,n = X.shape #(number of examples, number of features)
385
dj_dw = np.zeros((n,))
386
dj_db = 0.
387
388
for i in range(m):
389
err = (np.dot(X[i], w) + b) - y[i]
390
for j in range(n):
391
dj_dw[j] = dj_dw[j] + err * X[i,j]
392
dj_db = dj_db + err
393
dj_dw = dj_dw/m
394
dj_db = dj_db/m
395
396
return dj_db,dj_dw
397
398
#This version saves more values and is more verbose than the assigment versons
399
def gradient_descent_houses(X, y, w_in, b_in, cost_function, gradient_function, alpha, num_iters):
400
"""
401
Performs batch gradient descent to learn theta. Updates theta by taking
402
num_iters gradient steps with learning rate alpha
403
404
Args:
405
X : (array_like Shape (m,n) matrix of examples
406
y : (array_like Shape (m,)) target value of each example
407
w_in : (array_like Shape (n,)) Initial values of parameters of the model
408
b_in : (scalar) Initial value of parameter of the model
409
cost_function: function to compute cost
410
gradient_function: function to compute the gradient
411
alpha : (float) Learning rate
412
num_iters : (int) number of iterations to run gradient descent
413
Returns
414
w : (array_like Shape (n,)) Updated values of parameters of the model after
415
running gradient descent
416
b : (scalar) Updated value of parameter of the model after
417
running gradient descent
418
"""
419
420
# number of training examples
421
m = len(X)
422
423
# An array to store values at each iteration primarily for graphing later
424
hist={}
425
hist["cost"] = []; hist["params"] = []; hist["grads"]=[]; hist["iter"]=[];
426
427
w = copy.deepcopy(w_in) #avoid modifying global w within function
428
b = b_in
429
save_interval = np.ceil(num_iters/10000) # prevent resource exhaustion for long runs
430
431
print(f"Iteration Cost w0 w1 w2 w3 b djdw0 djdw1 djdw2 djdw3 djdb ")
432
print(f"---------------------|--------|--------|--------|--------|--------|--------|--------|--------|--------|--------|")
433
434
for i in range(num_iters):
435
436
# Calculate the gradient and update the parameters
437
dj_db,dj_dw = gradient_function(X, y, w, b)
438
439
# Update Parameters using w, b, alpha and gradient
440
w = w - alpha * dj_dw
441
b = b - alpha * dj_db
442
443
# Save cost J,w,b at each save interval for graphing
444
if i == 0 or i % save_interval == 0:
445
hist["cost"].append(cost_function(X, y, w, b))
446
hist["params"].append([w,b])
447
hist["grads"].append([dj_dw,dj_db])
448
hist["iter"].append(i)
449
450
# Print cost every at intervals 10 times or as many iterations if < 10
451
if i% math.ceil(num_iters/10) == 0:
452
#print(f"Iteration {i:4d}: Cost {cost_function(X, y, w, b):8.2f} ")
453
cst = cost_function(X, y, w, b)
454
print(f"{i:9d} {cst:0.5e} {w[0]: 0.1e} {w[1]: 0.1e} {w[2]: 0.1e} {w[3]: 0.1e} {b: 0.1e} {dj_dw[0]: 0.1e} {dj_dw[1]: 0.1e} {dj_dw[2]: 0.1e} {dj_dw[3]: 0.1e} {dj_db: 0.1e}")
455
456
return w, b, hist #return w,b and history for graphing
457
458
def run_gradient_descent(X,y,iterations=1000, alpha = 1e-6):
459
460
m,n = X.shape
461
# initialize parameters
462
initial_w = np.zeros(n)
463
initial_b = 0
464
# run gradient descent
465
w_out, b_out, hist_out = gradient_descent_houses(X ,y, initial_w, initial_b,
466
compute_cost, compute_gradient_matrix, alpha, iterations)
467
print(f"w,b found by gradient descent: w: {w_out}, b: {b_out:0.2f}")
468
469
return(w_out, b_out, hist_out)
470
471
# compact extaction of hist data
472
#x = hist["iter"]
473
#J = np.array([ p for p in hist["cost"]])
474
#ws = np.array([ p[0] for p in hist["params"]])
475
#dj_ws = np.array([ p[0] for p in hist["grads"]])
476
477
#bs = np.array([ p[1] for p in hist["params"]])
478
479
def run_gradient_descent_feng(X,y,iterations=1000, alpha = 1e-6):
480
m,n = X.shape
481
# initialize parameters
482
initial_w = np.zeros(n)
483
initial_b = 0
484
# run gradient descent
485
w_out, b_out, hist_out = gradient_descent(X ,y, initial_w, initial_b,
486
compute_cost, compute_gradient_matrix, alpha, iterations)
487
print(f"w,b found by gradient descent: w: {w_out}, b: {b_out:0.4f}")
488
489
return(w_out, b_out)
490
491
def gradient_descent(X, y, w_in, b_in, cost_function, gradient_function, alpha, num_iters):
492
"""
493
Performs batch gradient descent to learn theta. Updates theta by taking
494
num_iters gradient steps with learning rate alpha
495
496
Args:
497
X : (array_like Shape (m,n) matrix of examples
498
y : (array_like Shape (m,)) target value of each example
499
w_in : (array_like Shape (n,)) Initial values of parameters of the model
500
b_in : (scalar) Initial value of parameter of the model
501
cost_function: function to compute cost
502
gradient_function: function to compute the gradient
503
alpha : (float) Learning rate
504
num_iters : (int) number of iterations to run gradient descent
505
Returns
506
w : (array_like Shape (n,)) Updated values of parameters of the model after
507
running gradient descent
508
b : (scalar) Updated value of parameter of the model after
509
running gradient descent
510
"""
511
512
# number of training examples
513
m = len(X)
514
515
# An array to store values at each iteration primarily for graphing later
516
hist={}
517
hist["cost"] = []; hist["params"] = []; hist["grads"]=[]; hist["iter"]=[];
518
519
w = copy.deepcopy(w_in) #avoid modifying global w within function
520
b = b_in
521
save_interval = np.ceil(num_iters/10000) # prevent resource exhaustion for long runs
522
523
for i in range(num_iters):
524
525
# Calculate the gradient and update the parameters
526
dj_db,dj_dw = gradient_function(X, y, w, b)
527
528
# Update Parameters using w, b, alpha and gradient
529
w = w - alpha * dj_dw
530
b = b - alpha * dj_db
531
532
# Save cost J,w,b at each save interval for graphing
533
if i == 0 or i % save_interval == 0:
534
hist["cost"].append(cost_function(X, y, w, b))
535
hist["params"].append([w,b])
536
hist["grads"].append([dj_dw,dj_db])
537
hist["iter"].append(i)
538
539
# Print cost every at intervals 10 times or as many iterations if < 10
540
if i% math.ceil(num_iters/10) == 0:
541
#print(f"Iteration {i:4d}: Cost {cost_function(X, y, w, b):8.2f} ")
542
cst = cost_function(X, y, w, b)
543
print(f"Iteration {i:9d}, Cost: {cst:0.5e}")
544
return w, b, hist #return w,b and history for graphing
545
546
def load_house_data():
547
data = np.loadtxt("./data/houses.txt", delimiter=',', skiprows=1)
548
X = data[:,:4]
549
y = data[:,4]
550
return X, y
551
552
def zscore_normalize_features(X,rtn_ms=False):
553
"""
554
returns z-score normalized X by column
555
Args:
556
X : (numpy array (m,n))
557
Returns
558
X_norm: (numpy array (m,n)) input normalized by column
559
"""
560
mu = np.mean(X,axis=0)
561
sigma = np.std(X,axis=0)
562
X_norm = (X - mu)/sigma
563
564
if rtn_ms:
565
return(X_norm, mu, sigma)
566
else:
567
return(X_norm)
568
569
570
571