-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
94 lines (65 loc) · 2.59 KB
/
main.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
#!/usr/bin/env python
# coding: utf-8
# In[30]:
from download_data import download_data
import numpy as np
import matplotlib.pyplot as plt
from GD import gradientDescent
#from dataNormalization import rescaleMatrix1
# Starting codes for ha3 of CS596.
#NOTICE: Fill in the codes between "%PLACEHOLDER#start" and "PLACEHOLDER#end"
# There are two PLACEHODERS IN THIS SCRIPT
# parameters
################PLACEHOLDER1 #start##########################
# test multiple learning rates and report their convergence curves.
ALPHA = 0.03
MAX_ITER = 750
################PLACEHOLDER1 #end##########################
#% step-1: load data and divide it into two subsets, used for training and testing
sat = download_data('sat.csv', [1, 2, 4]).values # three columns: MATH SAT, VERB SAT, UNI. GPA # convert frame to matrix
################PLACEHOLDER2 #start##########################
# Normalize data
def rescaleMatrix1(dataMatrix):
colCount = len(dataMatrix[0])
rowCount = len(dataMatrix)
newMatrix = np.zeros(dataMatrix.shape)
#take mean, min and range of each column
for i in range(0, colCount):
mean = dataMatrix[:,i].mean()
min = dataMatrix[:,i].min()
denom = dataMatrix[:,i].max() - min
#for each observation, subtract mean and divide by rande
for k in range(0, rowCount):
newX = (dataMatrix[k,i] - mean) / denom
#store normalized data
newMatrix[k,i] = newX
return newMatrix
sat = rescaleMatrix1(sat)
################PLACEHOLDER2 #end##########################
# training data;
satTrain = sat[0:60, :]
# testing data;
satTest = sat[60:len(sat),:]
#% step-2: train a linear regression model using the Gradient Descent (GD) method
# ** theta and xValues have 3 columns since have 2 features: y = (theta * x^0) + (theta * x^1) + (theta * x^2)
theta = np.zeros(3)
xValues = np.ones((60, 3))
xValues[:, 1:3] = satTrain[:, 0:2]
yValues = satTrain[:, 2]
# call the GD algorithm, placeholders in the function gradientDescent()
[theta, arrCost] = gradientDescent(xValues, yValues, theta, ALPHA, MAX_ITER)
#visualize the convergence curve
plt.plot(range(0,len(arrCost)),arrCost);
plt.xlabel('iteration')
plt.ylabel('cost')
plt.title('alpha = {} theta = {}'.format(ALPHA, theta))
plt.show()
#% step-3: testing
testXValues = np.ones((len(satTest), 3))
testXValues[:, 1:3] = satTest[:, 0:2]
tVal = testXValues.dot(theta)
#% step-4: evaluation
# calculate average error and standard deviation
tError = np.sqrt([x**2 for x in np.subtract(tVal, satTest[:, 2])])
print('results: {} ({})'.format(np.mean(tError), np.std(tError)))
# In[ ]: