-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Copy pathlogistic_visualize.py
52 lines (36 loc) · 1.1 KB
/
logistic_visualize.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
# visualizes the Bayes solution
#
# the notes for this class can be found at:
# https://deeplearningcourses.com/c/data-science-logistic-regression-in-python
# https://www.udemy.com/data-science-logistic-regression-in-python
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import matplotlib.pyplot as plt
N = 100
D = 2
X = np.random.randn(N,D)
# center the first 50 points at (-2,-2)
X[:50,:] = X[:50,:] - 2*np.ones((50,D))
# center the last 50 points at (2, 2)
X[50:,:] = X[50:,:] + 2*np.ones((50,D))
# labels: first 50 are 0, last 50 are 1
T = np.array([0]*50 + [1]*50)
# add a column of ones
# ones = np.array([[1]*N]).T
ones = np.ones((N, 1))
Xb = np.concatenate((ones, X), axis=1)
def sigmoid(z):
return 1/(1 + np.exp(-z))
# get the closed-form solution
w = np.array([0, 4, 4])
# calculate the model output
z = Xb.dot(w)
Y = sigmoid(z)
plt.scatter(X[:,0], X[:,1], c=T, s=100, alpha=0.5)
x_axis = np.linspace(-6, 6, 100)
y_axis = -x_axis
plt.plot(x_axis, y_axis)
plt.show()