forked from DanielKetterer/MachineLearningWright
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
138 lines (112 loc) · 4.1 KB
/
main.py
File metadata and controls
138 lines (112 loc) · 4.1 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
print("CS6840 HW 1")
#import os
#os.chdir('c:\\Users') # Replace with path
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn import preprocessing
import matplotlib.pyplot as plt
#load data - skip first row
reData = np.loadtxt('reDataUCI.csv', delimiter = ",", skiprows = 1)
numRows = np.size(reData,0)
numCols = np.size(reData,1)
#Columns 0-6 are features
#X0= id number
#X1=the transaction date (for example, 2013.250=2013 March, 2013.500=2013 June, etc.)
#X2=the house age (unit: year)
#X3=the distance to the nearest MRT station (unit: meter)
#X4=the number of convenience stores in the living circle on foot (integer)
#X5=the geographic coordinate, latitude. (unit: degree)
#X6=the geographic coordinate, longitude. (unit: degree)
xFeatures = reData[:,0:numCols-1]
#Last column are the labels
yLabels = reData[:,7]
"""
OLS Derivation
# Ax = b
# A'Ax = A'b
# x = inverse(A'A)*A'b
# Here x is a feature vector, A is xFeatures, and b is Ylabels.
"""
def ordinaryLeastSquares(xFeatures, Ylabels):
XTX = np.dot(xFeatures.T,xFeatures)
XTY = np.dot(xFeatures.T, Ylabels)
OLS_params = np.dot(np.linalg.inv(XTX),XTY)
print(OLS_params)
"""
Calculate the hypothesis = X * theta
Calculate the cost = (h - y)^2
Calculate the gradient = sum(X' * loss )/ m #this is the part that makes it batch
Update the parameters theta = theta - alpha * gradient of cost
Check if cost is less than epsilon
"""
def bgd(xFeatures, yLabels, alpha, epsilon, epochs):
i = 0
theta = np.array([[0 for x in range(numCols-1)]], ndmin=2)
# print(theta)
# theta.shape = (numCols-1,1)
theta = np.transpose(theta)
print(theta)
Cost = epsilon + 1
while i < epochs or Cost < epsilon:
Hypo = np.dot(xFeatures, theta)
Diff = Hypo - yLabels
# print(Diff)
# print(Hypo)
# print(yLabels)
Cost = (1/2*numRows) * np.sum(np.square(Diff) )
# print(Cost)
theta = theta - alpha * (1.0/numRows) * np.dot(np.transpose(xFeatures), Diff)
#print(theta)
i += 1
#print(i)
return theta
"""
TODO
3. Test each algorithm on the reDataUCI dataset from Task 1. Report the best one
4. Report the affects of trying a variety of learning rates and number of
epochs.
Plot the cost of the bgd function after each epoch for
a variety of number of epochs and learning rate.
"""
test = bgd(xFeatures, yLabels, .0000001, .0000001, 10000)
test2 = ordinaryLeastSquares(xFeatures, yLabels)
CostHistory = []
#Here is where a variety of alpha, epochs are tested
for i in range(2):
alpha = 1000**(-i-1)
for j in range(5):
epochs =10**(j)
theta = bgd(xFeatures, yLabels, alpha, .0000001, epochs)
Hypo = np.dot(xFeatures, theta)
sse = np.sum(np.square(np.subtract(yLabels,Hypo)))
mse = np.mean(np.square(np.subtract(yLabels,Hypo)))
print('SSE and MME: alpha and epochs ' + str(sse) + str(', ') + str(mse) + str(', ') + str(alpha) + str(', ') + str(epochs))
Diff = Hypo - yLabels
Cost = (1/2*numRows) * np.sum(np.square(Diff) )
CostHistory.append(Cost)
fig = plt.figure()
plt.plot(epochs, Cost, color = 'r')
fig.suptitle("alpha = " + str(alpha))
plt.xlabel("Epoch #")
plt.ylabel("Cost")
plt.show()
"""
5. Repeat tasks 1.3, 2.3 and 2.4 for another dataset of your choosing. You
may use any dataset you wish from any public repository (UCI, Kaggle,
etc.). Give a brief description of the dataset (features, labels).
"""
#this is using the new dataset
reData = np.loadtxt('Admission_Predict.csv', delimiter = ",", skiprows = 1)
numRows = np.size(reData,0)
numCols = np.size(reData,1)
xFeatures = reData[:,0:numCols-1]
yLabels = reData[:,numRows-1]
newtest = bgd(xFeatures, yLabels, .0000001, .0000001, 10000)
newtest2 = ordinaryLeastSquares(xFeatures, yLabels)
"""
#References
#https://the-tarzan.com/2012/10/27/calculate-ols-regression-manually-in-python-using-numpy/
#https://machinelearningmastery.com/gradient-descent-for-machine-learning/
#https://towardsdatascience.com/difference-between-batch-gradient-descent-and-stochastic-gradient-descent-1187f1291aa1
#https://www.kaggle.com/mohansacharya/graduate-admissions/version/2 ---Dataset
"""