-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogistic.py
More file actions
61 lines (48 loc) · 1.62 KB
/
logistic.py
File metadata and controls
61 lines (48 loc) · 1.62 KB
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
import numpy as np
import scipy.stats as ss
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
def gen_data(n, h, sd1, sd2):
x1 = ss.norm.rvs(-h, sd1, n)
y1 = ss.norm.rvs(0, sd1, n)
x2 = ss.norm.rvs(h, sd2, n)
y2 = ss.norm.rvs(0, sd2, n)
return (x1, y1, x2, y2)
def plot_data(x1, y1, x2, y2):
plt.figure()
plt.plot(x1, y1, 'o', ms=2)
plt.plot(x2, y2, 'o', ms=2)
plt.xlabel('$X_1$')
plt.ylabel('$X_2$')
plt.savefig('gen_data.jpg')
(x1,y1,x2,y2) = gen_data(1000, 1.5, 1, 1.5)
plot_data(x1, y1, x2, y2)
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression()
X = np.vstack((np.vstack([x1,y1]).T, np.vstack([x2,y2]).T))
n = 1000
y = np.hstack((np.repeat(1,n), np.repeat(2,n)))
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.5, random_state=1)
clf.fit(X_train, y_train)
clf.score(X_test, y_test)
clf.predict_proba(np.array([-2,0]).reshape(1,-1))
clf.predict(np.array([-2,0]).reshape(1,-1))
def plot_probs(ax, clf, class_no):
xx1, xx2 = np.meshgrid(np.arange(-5, 5, 0.1), np.arange(-5, 5, 0.1))
probs = clf.predict_proba(np.stack((xx1.ravel(), xx2.ravel()), axis=1))
Z = probs[:,class_no]
Z = Z.reshape(xx1.shape)
CS = ax.contourf(xx1, xx2, Z)
plt.colorbar(CS)
plt.xlabel('$X_1$')
plt.ylabel('$X_2$')
plt.figure(figsize=(5,8))
ax = plt.subplot(211)
plot_probs(ax, clf, 0)
plt.title("Pred. prob for class 1")
ax = plt.subplot(212)
plot_probs(ax, clf, 1)
plt.title("Pred. prob for class 2")
plt.tight_layout()
plt.savefig('probabilities.jpg')