import numpy as np
import sklearn
import matplotlib.pyplot as plt
from latexify import *
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import matplotlib.patches as mpatches
Logistic Regression - I
ML
= np.array([0, 0.1, 0.2, 0.3, 0.6, 0.7, 0.9]) x
= (x>0.4).astype('int') y
y
array([0, 0, 0, 0, 1, 1, 1])
latexify()=y)
plt.scatter(x, np.zeros_like(x), c= mpatches.Patch(color='yellow', label='Oranges')
yellow_patch = mpatches.Patch(color='darkblue', label='Tomatoes')
blue_patch # legend outside the plot
=[yellow_patch, blue_patch], bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.legend(handles#plt.legend(handles=[yellow_patch, blue_patch])
'Radius')
plt.xlabel(False)
plt.gca().yaxis.set_visible(
format_axes(plt.gca())"../figures/logistic-regression/logistic-orange-tomatoes-original.pdf", bbox_inches="tight", transparent=True) plt.savefig(
=y)
plt.scatter(x, y, c= mpatches.Patch(color='yellow', label='Oranges')
yellow_patch = mpatches.Patch(color='darkblue', label='Tomatoes')
blue_patch =[yellow_patch, blue_patch], bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.legend(handles
'Radius')
plt.xlabel(
format_axes(plt.gca())"../figures/logistic-regression/logistic-orange-tomatoes.pdf", bbox_inches="tight", transparent=True) plt.savefig(
Fitting linear model
from sklearn.linear_model import LinearRegression
= LinearRegression() linr_reg
-1, 1), y) linr_reg.fit(x.reshape(
LinearRegression()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LinearRegression()
0, 1, 50), linr_reg.predict(np.linspace(0, 1, 50).reshape(-1, 1)))
plt.plot(np.linspace(=y)
plt.scatter(x, y, c= mpatches.Patch(color='yellow', label='Oranges')
yellow_patch = mpatches.Patch(color='darkblue', label='Tomatoes')
blue_patch
'Radius')
plt.xlabel(
format_axes(plt.gca())=1, color='grey', label='P(y=1)')
plt.axhline(y=0, color='grey')
plt.axhline(y=[yellow_patch, blue_patch], bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.legend(handles
"../figures/logistic-regression/linear-orange-tomatoes.pdf", bbox_inches="tight", transparent=True) plt.savefig(
0.5-linr_reg.intercept_)/linr_reg.coef_ (
array([0.44857143])
0, 1, 50), linr_reg.predict(np.linspace(0, 1, 50).reshape(-1, 1)))
plt.plot(np.linspace(=y)
plt.scatter(x, y, c= mpatches.Patch(color='yellow', label='Oranges')
yellow_patch = mpatches.Patch(color='darkblue', label='Tomatoes')
blue_patch
'Radius')
plt.xlabel(
format_axes(plt.gca())#plt.axhline(y=1, color='grey', label='P(y=1)')
#plt.axhline(y=0, color='grey')
=0.5, color='grey')
plt.axhline(y=((0.5-linr_reg.intercept_)/linr_reg.coef_)[0], color='grey')
plt.axvline(x
=[yellow_patch, blue_patch], bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.legend(handles
"../figures/logistic-regression/linear-orange-tomatoes-decision.pdf", bbox_inches="tight", transparent=True) plt.savefig(
= np.append(x, 2.5)
x_dash = np.append(y, 1)
y_dash -1, 1), y_dash) linr_reg.fit(x_dash.reshape(
LinearRegression()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LinearRegression()
0, 2.5, 50), linr_reg.predict(np.linspace(0, 2.5, 50).reshape(-1, 1)))
plt.plot(np.linspace(=y_dash)
plt.scatter(x_dash, y_dash, c= mpatches.Patch(color='yellow', label='Oranges')
yellow_patch = mpatches.Patch(color='darkblue', label='Tomatoes')
blue_patch
'Radius')
plt.xlabel(
format_axes(plt.gca())#plt.axhline(y=1, color='grey', label='P(y=1)')
#plt.axhline(y=0, color='grey')
=0.5, color='grey')
plt.axhline(y=((0.5-linr_reg.intercept_)/linr_reg.coef_)[0], color='grey')
plt.axvline(x
=[yellow_patch, blue_patch], bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.legend(handles
"../figures/logistic-regression/linear-orange-tomatoes-decision-modified.pdf", bbox_inches="tight", transparent=True) plt.savefig(
from sklearn.linear_model import LogisticRegression
= LogisticRegression(penalty=None, solver='lbfgs') clf
-1,1), y) clf.fit(x.reshape(
LogisticRegression(penalty=None)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LogisticRegression(penalty=None)
clf.coef_
array([[55.99493009]])
-clf.intercept_[0]/clf.coef_[0]
array([0.4484548])
clf.intercept_
array([-25.11119514])
def sigmoid(z):
return 1/(1+np.exp(-z))
latexify()=y)
plt.scatter(x, y, c= mpatches.Patch(color='yellow', label='Oranges')
yellow_patch = mpatches.Patch(color='darkblue', label='Tomatoes')
blue_patch = mpatches.Patch(color='black', label='Decision Boundary')
black_patch = -clf.intercept_[0]/clf.coef_[0],label='Decision Boundary',linestyle='--',color='k',lw=1)
plt.axvline(x 'Radius')
plt.xlabel(
format_axes(plt.gca())=[black_patch, yellow_patch, blue_patch], bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.legend(handles0,1, xmin=0, xmax=0.49, linestyle='--',color='darkblue',lw=1, alpha=0.2)
plt.axhspan(0,0.001, xmin=0, xmax=0.49, linestyle='--',color='k',lw=1, )
plt.axhspan(
0,1, xmax=1, xmin=0.49, linestyle='--',color='yellow',lw=1, alpha=0.2)
plt.axhspan(1,1.001, xmax=1, xmin=0.49, linestyle='--',color='k',lw=1, )
plt.axhspan("../figures/logistic-regression/linear-orange-tomatoes-decision-ideal.pdf", bbox_inches="tight", transparent=True) plt.savefig(
= np.linspace(-0.1, 1, 100)
x_dum *clf.coef_[0] + clf.intercept_[0]))
plt.plot(x_dum, sigmoid(x_dum=y)
plt.scatter(x, y, c
latexify()-clf.intercept_[0]/clf.coef_[0], lw=2, color='black')
plt.axvline(0.5, linestyle='--',color='k',lw=3, label='P(y=1) = P(y=0)')
plt.axhline("P(y=1)")
plt.ylabel('Radius')
plt.xlabel(= mpatches.Patch(color='yellow', label='Oranges')
yellow_patch = mpatches.Patch(color='darkblue', label='Tomatoes')
blue_patch = mpatches.Patch(color='black', label='Decision Boundary')
black_patch = mpatches.Patch(color='steelblue', label='Sigmoid')
sigmoid_patch =[black_patch, yellow_patch, blue_patch, sigmoid_patch], bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.legend(handles
format_axes(plt.gca())"Logistic Regression")
plt.title("../figures/logistic-regression/logistic.pdf", bbox_inches="tight", transparent=True) plt.savefig(
= np.linspace(-10, 10, 500)
z_lins =r'$\sigma(z) = \frac{1}{1+e^{-z}}$')
plt.plot(z_lins, sigmoid(z_lins), label
format_axes(plt.gca())'z')
plt.xlabel(r'$\sigma(z)$')
plt.ylabel(# vline at 0
0, color='black', lw=1, linestyle='--')
plt.axvline(# hline at 0.5
0.5, color='black', lw=1, linestyle='--')
plt.axhline("../figures/logistic-regression/logistic-function.pdf", bbox_inches="tight", transparent=True) plt.savefig(
= np.linspace(-0.1, 1, 100)
x_dum *clf.coef_[0] + clf.intercept_[0]))
plt.plot(x_dum, sigmoid(x_dum
format_axes(plt.gca())"z")
plt.xlabel(r"$\sigma(z)$")
plt.ylabel(#plt.savefig("../figures/logistic-regression/logistic-function.pdf", bbox_inches="tight", transparent=True)