-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogistic_regression_learner.py
50 lines (41 loc) · 1.26 KB
/
logistic_regression_learner.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
import pandas
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.preprocessing import PolynomialFeatures
def logistic_regression_learner(
x_train, y_train, x_test, y_test
):
"""
:param x_train:
:param y_train:
:param x_test:
:param y_test:
"""
Logit = LogisticRegression()
poly_accuracy = []
polynomials = range(1, 10)
for poly_degree in polynomials:
poly = PolynomialFeatures(
degree=poly_degree, include_bias=False
)
X_poly = poly.fit_transform(x_train)
x_test_poly = poly.fit_transform(x_test)
Logit.fit(X_poly, y_train)
y_pred = Logit.predict(x_test_poly)
print(
"Polynomial Degree:",
poly_degree,
"Accuracy:",
round(Logit.score(x_test_poly, y_test), 3),
)
poly_accuracy.append(
[
poly_degree,
round(Logit.score(x_test_poly, y_test), 3),
]
)
Polynomial_Accuracy = pandas.DataFrame(poly_accuracy)
Polynomial_Accuracy.columns = ["Polynomial", "Accuracy"]
from sklearn.metrics import confusion_matrix
confusion_matrix = confusion_matrix(y_test, y_pred)
print(confusion_matrix)