-
Notifications
You must be signed in to change notification settings - Fork 4
/
LinearRegression.py
100 lines (78 loc) · 3.35 KB
/
LinearRegression.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
import numpy as np
import logging
import json
from utility import * #custom methods for data cleaning
FILE_NAME_TRAIN = 'cleaned_train.csv' #replace this file name with the train file
FILE_NAME_TEST = 'cleaned_test.csv' #replace
ALPHA = 1e-11
EPOCHS = 5000#keep this greater than or equl to 5000 strictly otherwise you will get an error
MODEL_FILE = 'models/model1'
train_flag = True
logging.basicConfig(filename='output.log',level=logging.DEBUG)
np.set_printoptions(suppress=True)
#################################################################################################
#####################################write the functions here####################################
#################################################################################################
#this function appends 1 to the start of the input X and returns the new array
def appendIntercept(X):
#steps
#make a column vector of ones
#stack this column vector infront of the main X vector using hstack
#return the new matrix
pass#remove this line once you finish writing
#intitial guess of parameters (intialize all to zero)
#this func takes the number of parameters that is to be fitted and returns a vector of zeros
def initialGuess(n_thetas):
pass
def train(theta, X, y, model):
J = [] #this array should contain the cost for every iteration so that you can visualize it later when you plot it vs the ith iteration
#train for the number of epochs you have defined
m = len(y)
#your gradient descent code goes here
#steps
#run you gd loop for EPOCHS that you have defined
#calculate the predicted y using your current value of theta
# calculate cost with that current theta using the costFunc function
#append the above cost in J
#calculate your gradients values using calcGradients function
# update the theta using makeGradientUpdate function (don't make a new variable assign it back to theta that you received)
model['J'] = J
model['theta'] = list(theta)
return model
#this function will calculate the total cost and will return it
def costFunc(m,y,y_predicted):
#takes three parameter as the input m(#training examples), (labeled y), (predicted y)
#steps
#apply the formula learnt
pass
def calcGradients(X,y,y_predicted,m):
#apply the formula , this function will return cost with respect to the gradients
# basically an numpy array containing n_params
pass
#this function will update the theta and return it
def makeGradientUpdate(theta, grads):
pass
#this function will take two paramets as the input
def predict(X,theta):
pass
########################main function###########################################
def main():
if(train_flag):
model = {}
X_df,y_df = loadData(FILE_NAME_TRAIN)
X,y, model = normalizeData(X_df, y_df, model)
X = appendIntercept(X)
theta = initialGuess(X.shape[1])
model = train(theta, X, y, model)
with open(MODEL_FILE,'w') as f:
f.write(json.dumps(model))
else:
model = {}
with open(MODEL_FILE,'r') as f:
model = json.loads(f.read())
X_df, y_df = loadData(FILE_NAME_TEST)
X,y = normalizeTestData(X_df, y_df, model)
X = appendIntercept(X)
accuracy(X,y,model)
if __name__ == '__main__':
main()