import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from latexify import *
# Matplotlib retina
%config InlineBackend.figure_format = 'retina'
Logistic Regression - Cost Function
ML
= np.array([
X 1],
[2],
[3],
[4],
[5],
[6]
[
])
= np.array([1, 1, 1, 0, 0, 0]) y
from sklearn.linear_model import LogisticRegression
= LogisticRegression(penalty='none',solver='newton-cg') lr
lr.fit(X, y)
/home/nipun.batra/miniforge3/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:1183: FutureWarning: `penalty='none'`has been deprecated in 1.2 and will be removed in 1.4. To keep the past behaviour, set `penalty=None`.
warnings.warn(
LogisticRegression(penalty='none', solver='newton-cg')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', solver='newton-cg')
lr.coef_
array([[-18.33148189]])
lr.intercept_
array([64.11147504])
def sigmoid(z):
return 1/(1+np.exp(z))
= np.meshgrid(np.linspace(-10, 10, 200), np.linspace(-10, 10, 200)) theta_0_li, theta_1_li
def cost_rmse(theta_0, theta_1):
= sigmoid(theta_0 + theta_1*X)
y_hat = np.sum((y-y_hat)**2)
err return err
= np.zeros((len(theta_0_li), len(theta_0_li)))
z for i in range(len(theta_0_li)):
for j in range(len(theta_0_li)):
= cost_rmse(theta_0_li[i, j], theta_1_li[i, j]) z[i, j]
latexify()
plt.contourf(theta_0_li, theta_1_li, z)r'$\theta_0$')
plt.xlabel(r'$\theta_1$')
plt.ylabel(
plt.colorbar()'RMSE contour plot')
plt.title(
format_axes(plt.gca())"../figures/logistic-regression/logistic-sse-loss-contour.pdf", bbox_inches="tight", transparent=True) plt.savefig(
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
latexify()= plt.figure()
fig = fig.add_subplot(111, projection='3d')
ax
ax.plot_surface(theta_0_li, theta_1_li, z)'RMSE surface plot')
ax.set_title(r'$\theta_0$')
ax.set_xlabel(r'$\theta_1$')
ax.set_ylabel(
plt.tight_layout()"../figures/logistic-regression/logistic-sse-loss-3d.pdf", bbox_inches="tight", transparent=True) plt.savefig(
import pandas as pd
min().min() pd.DataFrame(z).
9.01794626038055
def cost_2(theta_0, theta_1):
= sigmoid(theta_0 + theta_1*X)
y_hat
= -np.sum((y*np.log(y_hat) + (1-y)*np.log(1-y_hat)))
err return err
= np.zeros((len(theta_0_li), len(theta_0_li)))
z2 for i in range(len(theta_0_li)):
for j in range(len(theta_0_li)):
= cost_2(theta_0_li[i, j], theta_1_li[i, j]) z2[i, j]
/tmp/ipykernel_851067/1266618369.py:4: RuntimeWarning: divide by zero encountered in log
err = -np.sum((y*np.log(y_hat) + (1-y)*np.log(1-y_hat)))
/tmp/ipykernel_851067/1266618369.py:4: RuntimeWarning: invalid value encountered in multiply
err = -np.sum((y*np.log(y_hat) + (1-y)*np.log(1-y_hat)))
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
latexify()= plt.figure()
fig = fig.add_subplot(111, projection='3d')
ax
ax.plot_surface(theta_0_li, theta_1_li, z2)'Cross-entropy surface plot')
ax.set_title(r'$\theta_0$')
ax.set_xlabel(r'$\theta_1$')
ax.set_ylabel(
plt.tight_layout()"../figures/logistic-regression/logistic-cross-loss-surface.pdf", bbox_inches="tight", transparent=True) plt.savefig(
latexify()
plt.contourf(theta_0_li, theta_1_li, z2)'Cross-entropy contour plot')
plt.title(
plt.colorbar()r'$\theta_0$')
plt.xlabel(r'$\theta_1$')
plt.ylabel(
format_axes(plt.gca())"../figures/logistic-regression/logistic-cross-loss-contour.pdf", bbox_inches="tight", transparent=True) plt.savefig(
y.shape, y_bar.shape
((6,), (10000,))
= 0
y = np.linspace(0, 1.1, 10000)
y_bar -y*np.log(y_bar) - (1-y)*np.log(1-y_bar))
plt.plot(y_bar,
format_axes(plt.gca())"Cost when y = 0")
plt.ylabel(r'$\hat{y}$')
plt.xlabel("../figures/logistic-regression/logistic-cross-cost-0.pdf", bbox_inches="tight", transparent=True) plt.savefig(
/tmp/ipykernel_851067/3960806875.py:3: RuntimeWarning: divide by zero encountered in log
plt.plot(y_bar, -y*np.log(y_bar) - (1-y)*np.log(1-y_bar))
/tmp/ipykernel_851067/3960806875.py:3: RuntimeWarning: invalid value encountered in multiply
plt.plot(y_bar, -y*np.log(y_bar) - (1-y)*np.log(1-y_bar))
/tmp/ipykernel_851067/3960806875.py:3: RuntimeWarning: invalid value encountered in log
plt.plot(y_bar, -y*np.log(y_bar) - (1-y)*np.log(1-y_bar))
= 1
y = np.linspace(0, 1.1, 10000)
y_bar -y*np.log(y_bar) - (1-y)*np.log(1-y_bar))
plt.plot(y_bar,
format_axes(plt.gca())"Cost when y = 1")
plt.ylabel(r'$\hat{y}$')
plt.xlabel("../figures/logistic-regression/logistic-cross-cost-1.pdf", bbox_inches="tight", transparent=True) plt.savefig(
/tmp/ipykernel_851067/210742206.py:3: RuntimeWarning: divide by zero encountered in log
plt.plot(y_bar, -y*np.log(y_bar) - (1-y)*np.log(1-y_bar))
/tmp/ipykernel_851067/210742206.py:3: RuntimeWarning: invalid value encountered in log
plt.plot(y_bar, -y*np.log(y_bar) - (1-y)*np.log(1-y_bar))
/tmp/ipykernel_851067/210742206.py:3: RuntimeWarning: invalid value encountered in multiply
plt.plot(y_bar, -y*np.log(y_bar) - (1-y)*np.log(1-y_bar))
Likelihood
= np.hstack((np.ones_like(X), X))
X_with_one
\[\begin{align*} P(y | X, \theta) &= \prod_{i=1}^{n} P(y_{i} | x_{i}, \theta) \\ &= \prod_{i=1}^{n} \Big\{\frac{1}{1 + e^{-x_{i}^{T}\theta}}\Big\}^{y_{i}}\Big\{1 - \frac{1}{1 + e^{-x_{i}^{T}\theta}}\Big\}^{1 - y_{i}} \\ \end{align*}\]
1] X_with_one[
array([1, 2])
def likelihood(theta_0, theta_1):
= 1
s
for i in range(len(X)):
= sigmoid(-X_with_one[i]@np.array([theta_0, theta_1]))
y_i_hat = s* ((y_i_hat**y[i])*(1-y_i_hat)**(1-y[i]))
s
return s
= np.mgrid[-5:100:0.5, -30:10:.1]
x_grid_2, y_grid_2
= np.zeros_like(x_grid_2)
li for i in range(x_grid_2.shape[0]):
for j in range(x_grid_2.shape[1]):
= likelihood(x_grid_2[i, j], y_grid_2[i, j])
li[i, j]
plt.contourf(x_grid_2, y_grid_2, li)#plt.gca().set_aspect('equal')
r"$\theta_0$")
plt.xlabel(r"$\theta_1$")
plt.ylabel(
plt.colorbar()0], lr.coef_[0], s=200, marker='*', color='r', label='MLE')
plt.scatter(lr.intercept_[r"Likelihood as a function of ($\theta_0, \theta_1$)")
plt.title(#plt.gca().set_aspect('equal')
plt.legend()"../figures/logistic-regression/logistic-likelihood.pdf", bbox_inches="tight", transparent=True) plt.savefig(