In [28]:
from sklearn import datasets
In [29]:
X, y = datasets.make_classification(n_features=2, n_redundant=0, random_state=5)
plt.figsize(5, 5)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.prism)
plt.xlim(-3, 3)
plt.ylim(-3, 3)
plt.xticks(())
plt.yticks(())
Out[29]:
([], <a list of 0 Text yticklabel objects>)
In [30]:
from sklearn.linear_model import LogisticRegression
In [31]:
logreg = LogisticRegression()
In [32]:
logreg.fit(X, y)
Out[32]:
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, penalty='l2', random_state=None, tol=0.0001)
In [33]:
from utility import plot_decision_boundary
plt.figsize(5, 5)
plt.xlim(-3, 3)
plt.ylim(-3, 3)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.prism)
plot_decision_boundary(logreg, X)
plt.savefig("presentation/logreg-pics/synthetic_line.pdf")
In [34]:
plt.figsize(5, 5)
w = logreg.coef_.ravel()
a = -w[0] / w[1]
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.prism)
plt.xlim(-3, 3)
plt.ylim(-3, 3)
plot_decision_boundary(logreg, X)
on_line = np.array([xx[23], yy[23]])

#plt.plot([on_line[0], on_line[0] + w[0]/2.], [on_line[1], on_line[1] + w[1]/2.], c='black')

plt.arrow(on_line[0], on_line[1], w[0]/2, w[1]/2., head_width=.1, width=.02, facecolor="black")
plt.annotate("w", [on_line[0] + w[0]/4., on_line[1] + w[1]/4.], xytext=[on_line[0] + w[0]/4. + .2, on_line[1] + w[1]/4. + .1], size=20)

plt.savefig("presentation/logreg-pics/synthetic_line_w.pdf")
In [35]:
x = np.linspace(-6, 6)
y = 1. / (1 + exp(-x))
plt.figsize(6, 3)
plt.plot(x, y)
plt.ylim(-0.1, 1.1)
plt.xlim(-6, 6)
plt.tight_layout()
plt.savefig("presentation/logreg-pics/logistic_sigmoid.pdf")
In [36]:
X, y = datasets.make_moons(noise=.1)
In [75]:
plt.prism()
plt.figsize(7, 5)
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.xticks(())
plt.yticks(())
plt.xlim((-1.3184077196039725, 2.2915086337379846))
plt.ylim((-0.75489756567439814, 1.1668087283562494))
plt.tight_layout()
plt.savefig("presentation/knn-pics/simple_moons.pdf")
In [67]:
logreg.fit(X, y)
Out[67]:
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, penalty='l2', random_state=None, tol=0.0001)
In [74]:
plt.prism()
plt.figsize(7, 5)
plot_decision_boundary(logreg, X)
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.tight_layout()
plt.savefig("presentation/knn-pics/simple_moons_decision.pdf")
In [73]:
 
In [ ]: