-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogreg.py
36 lines (30 loc) · 914 Bytes
/
logreg.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
import numpy as np
from sklearn.linear_model import LogisticRegression
import utilities as ut
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score
# Get the normalized data
X_train, y_train, X_test = ut.import_data()
C_vals = np.arange(0.01, 0.2, 0.02)
scores = []
best_score = 0
best_C = 0
for C in C_vals:
clf = LogisticRegression(C=C)
clf.fit(X_train, y_train)
score = np.mean(cross_val_score(clf, X_train, y_train, cv=5, scoring='accuracy'))
scores.append(score)
print C, score
if score > best_score:
best_score = score
best_C = C
plt.plot(C_vals, scores)
plt.title('Logistic Regression Accuracy vs. Regularization')
plt.xlabel('C')
plt.ylabel('Accuracy')
plt.savefig('logreg.png')
plt.show()
# Predictions
clf = LogisticRegression(C=best_C)
clf.fit(X_train, y_train)
ut.write_output_file(clf.predict(X_test), file_name='logreg.csv')