import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import ipywidgets as widgets
from IPython.display import display, clear_output
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
%matplotlib inline
# Retina display
%config InlineBackend.figure_format = 'retina'
import warnings
'ignore') warnings.filterwarnings(
from tueplots import bundles
plt.rcParams.update(bundles.beamer_moml())
# Also add despine to the bundle using rcParams
'axes.spines.right'] = False
plt.rcParams['axes.spines.top'] = False
plt.rcParams[
# Increase font size to match Beamer template
'font.size'] = 16
plt.rcParams[# Make background transparent
'figure.facecolor'] = 'none' plt.rcParams[
"font.family"] = "Arial" plt.rcParams[
0)
np.random.seed(= np.random.rand(60, 1)
X = np.where(X > 0.5, 1, 0)
y
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def compute_log_likelihood(theta0, theta1):
= sigmoid(theta0 + theta1 * X)
h = np.sum(y * np.log(h) + (1 - y) * np.log(1 - h))
log_likelihood return log_likelihood
= np.linspace(-6, 2, 100)
theta0_vals = np.linspace(2, 6, 100)
theta1_vals = np.meshgrid(theta0_vals, theta1_vals)
theta0_grid, theta1_grid = np.zeros_like(theta0_grid)
loglik_grid
for i in range(len(theta0_vals)):
for j in range(len(theta1_vals)):
= compute_log_likelihood(
loglik_grid[i, j]
theta0_vals[i], theta1_vals[j])
= widgets.FloatSlider(
theta0_slider =1, min=-6, max=2, step=0.1, description='Theta0:')
value= widgets.FloatSlider(
theta1_slider =1, min=2, max=6, step=0.1, description='Theta1:')
value
= widgets.Output(layout={'border': '1px solid black'})
figure_container
def update_figure(change):
with figure_container:
=True)
clear_output(wait
1, 2, 1)
plt.subplot(
plt.contourf(theta0_grid, theta1_grid, loglik_grid,=20, cmap='viridis')
levels='Log Likelihood')
plt.colorbar(label
= -2.0
true_theta0 = 4.0
true_theta1 ='blue',
plt.scatter([true_theta0], [true_theta1], color='o', label='True Values')
marker
plt.scatter([theta0_slider.value], [theta1_slider.value],='red', marker='x', label='Current Values')
color
'Theta0')
plt.xlabel('Theta1')
plt.ylabel('Log Likelihood Contour Plot')
plt.title(
plt.legend()
1, 2, 2)
plt.subplot(=y, cmap=ListedColormap(
plt.scatter(X, y, c'#FF0000', '#0000FF']), edgecolors='k', marker='o')
[
if theta1_slider.value != 0:
0, 1], [-theta0_slider.value/theta1_slider.value, (1-theta0_slider.value) /
plt.plot([='red', label='Decision Boundary')
theta1_slider.value], colorelse:
=theta0_slider.value, color='red',
plt.axvline(x='--', label='Decision Boundary')
linestyle
'X')
plt.xlabel('Class')
plt.ylabel('Logistic Regression Decision Boundary')
plt.title(
plt.legend()
# plt.tight_layout()
plt.show()
'value')
theta0_slider.observe(update_figure, 'value')
theta1_slider.observe(update_figure,
None)
update_figure(
display(widgets.HBox( [figure_container, widgets.VBox([theta0_slider, theta1_slider])]))
1, 2, 1)
plt.subplot(=20, cmap='viridis')
plt.contourf(theta0_grid, theta1_grid, loglik_grid, levels='Log Likelihood')
plt.colorbar(label
= -2.0
true_theta0 = 4.0
true_theta1 ='blue',
plt.scatter([true_theta0], [true_theta1], color='o', label='True Values')
marker-1], [3], color='red', marker='x', label='Current Values')
plt.scatter([
'Theta0')
plt.xlabel('Theta1')
plt.ylabel('Log Likelihood Contour Plot')
plt.title(
plt.legend()
1, 2, 2)
plt.subplot(=y, cmap=ListedColormap(
plt.scatter(X, y, c'#FF0000', '#0000FF']), edgecolors='k', marker='o')
[
if theta1_slider.value != 0:
0, 1], [1/3, (1+1)/3], color='red', label='Decision Boundary')
plt.plot([else:
=-1, color='red', linestyle='--', label='Decision Boundary')
plt.axvline(x
'X')
plt.xlabel('Class')
plt.ylabel('Logistic Regression Decision Boundary')
plt.title(
plt.legend()
# plt.tight_layout()
'figures/mle/log_reg_slider_3.pdf') plt.savefig(