-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryclassification.py
72 lines (54 loc) · 1.75 KB
/
binaryclassification.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
# -*- coding: utf-8 -*-
"""BinaryClassification.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ccMuEwHQSz09Lt6woUmImGJH8kB-JWag
"""
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
def sigmoid(x):
return 1/1+np.exp(-x)
def d_sigmoid(x):
return sigmoid(x)(1-sigmoid(x))
# create variable used for prediction
hours_learning = np.linspace(1, 100, 100)
# probability of passing
weights = hours_learning / len(hours_learning)
# create variable we want to predict from hours_learning
pass_test = np.random.binomial(1, weights)
print('First five values')
print('\thours_learning:\t\t', hours_learning[:5])
print('\tweights:\t\t', weights[:5])
print('\tpass_test:\t\t', pass_test[:5])
x = hours_learning.reshape(-1,1)
y = pass_test
model = LogisticRegression()
model.fit(x,y)
# use the model coefficients to draw the plot
pred = sigmoid(x * model.coef_[0] + model.intercept_[0])
fig = plt.figure(figsize=(16, 9))
plt.plot(x, pred, c="lightblue", linewidth=3.0)
plt.scatter(
x[(y == 1).ravel()],
y[(y == 1).ravel()],
marker=".",
c="lightgreen",
linewidth=1.0,
label="passed",
)
plt.scatter(
x[(y == 0).ravel()],
y[(y == 0).ravel()],
marker=".",
c="red",
linewidth=1.0,
label="failed",
)
plt.axhline(y=0.5, color="orange", linestyle="--", label="boundary")
plt.xlabel("Hours spent learning")
plt.ylabel('p("passing the test")')
plt.legend(frameon=False, loc="best", bbox_to_anchor=(0.5, 0.0, 0.5, 0.5), prop={'size': 20})
plt.show()
print(model.predict([[30]])) # 30 hours spent learning will lead to a fail prediction
print(model.predict([[60]])) # 60 hours spent learning will lead to a pass prediction