-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ2_plots.py
155 lines (112 loc) · 3.86 KB
/
Q2_plots.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
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
from scratch import MyLogisticRegression,MyPreProcessor
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression,SGDClassifier
def resultsUsingSKLogistic():
"""
Perform Logistic Regression on dataset-2 using scikit learn's built-in methods
Reports Train and test accuracy
Parameters: None
Returns: None
"""
print("Using SkLearn Logistic...")
# Obtain the data
proc = MyPreProcessor()
X,y = proc.pre_process(2)
k = X.shape[0]//10
# Train split
Xtrain = X[:7*k, :]
ytrain = y[:7*k]
# Validation split
Xvalid = X[7*k:8*k, :]
yvalid = y[7*k:8*k]
# Test split
Xtest = X[8*k:, :]
ytest = y[8*k:]
# Initialize the model
logistic = LogisticRegression()
# Train the model
logistic.fit(Xtrain,ytrain)
# Calculate accuracy
train_acc = logistic.score(Xtrain,ytrain)
test_acc = logistic.score(Xtest,ytest)
# Report the results
print("Train Accuracy:",train_acc)
print("Test Accuracy:",test_acc)
def resultsUsingSGDClassifier():
"""
Perform SGD Logistic Regression on dataset-2 using scikit learn's built-in methods
Reports Train and test accuracy
Parameters: None
Returns: None
"""
print("Using SkLearn SGD Classifier...")
# Obtain the data
proc = MyPreProcessor()
X,y = proc.pre_process(2)
k = X.shape[0]//10
# Train split
Xtrain = X[:7*k, :]
ytrain = y[:7*k]
# Validation split
Xvalid = X[7*k:8*k, :]
yvalid = y[7*k:8*k]
# Test split
Xtest = X[8*k:, :]
ytest = y[8*k:]
# Initialize the model
model = SGDClassifier(loss='log', max_iter=100000, learning_rate='constant', eta0=0.1)
# Train the model
model.fit(Xtrain,ytrain)
# Calculate accuracy
train_acc = model.score(Xtrain,ytrain)
test_acc = model.score(Xtest,ytest)
# Report the results
print("Train Accuracy:",train_acc)
print("Test Accuracy:",test_acc)
def resultsUsingMyLogistic(alg,alpha,epochs):
"""
Performing logistic regression on dataset-2 using alg type gradient descent
Parameters
----------
alpha : learning rate
epochs : number of iterations in gradient descend
alg: "SGD" or "BGD"
----------
Returns: None
"""
print("Using MyLogistic...")
# Importing the data
proc = MyPreProcessor()
X,y = proc.pre_process(2)
k = X.shape[0]//10
# Train split
Xtrain = X[:7*k, :]
ytrain = y[:7*k]
# Valid split
Xvalid = X[7*k:8*k, :]
yvalid = y[7*k:8*k]
# Test split
Xtest = X[8*k:, :]
ytest = y[8*k:]
# Initialize my own implementation of Logistic Regression
logistic = MyLogisticRegression()
logistic.fit(Xtrain, ytrain, plot=True, X_valid=Xvalid, y_valid=yvalid, alpha=alpha, epochs=epochs, alg=alg)
# Print the results
print("Final Train Loss:",logistic.train_loss[-1])
print("Final Validation Loss:",logistic.valid_loss[-1])
print("Theta:",logistic.theta)
train_acc = np.sum((logistic.predict(Xtrain)==ytrain)*1)/Xtrain.shape[0]
test_acc = np.sum((logistic.predict(Xtest)==ytest)*1)/Xtest.shape[0]
print("Train Accuracy:",train_acc)
print("Test Accuracy:",test_acc)
# Plot results
proc.PlotLossVsEpochs(logistic.train_loss, "Training Loss", logistic.valid_loss, "Validation Loss", text=alg+"; dataset-2"+"\nTrain Acc:"+str(train_acc)+"\nTest Acc:"+str(test_acc))
if __name__=="__main__":
resultsUsingMyLogistic("SGD",0.1,100000)
resultsUsingMyLogistic("BGD",3,10000)
resultsUsingSKLogistic() #c | default logistic regression implementation
resultsUsingSGDClassifier() #d | sgd classifier with attributes to make it behave as logistic regression with our hyper parameters
# resultsUsingMyLogistic("SGD",10,100000)
# resultsUsingMyLogistic("BGD",10,10000)