import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
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[
= np.array([1, 2, 3, 4, 5])
X = np.array([2, 3, 4, 4.5, 5])
y
def compute_cost(theta0, theta1):
= theta0 + theta1 * X
y_pred = y_pred - y
error = -0.5 * np.sum(error ** 2)
cost return cost
= np.linspace(-2, 4, 100)
theta0_vals = np.linspace(-2, 4, 100)
theta1_vals = np.meshgrid(theta0_vals, theta1_vals)
theta0_grid, theta1_grid = np.zeros_like(theta0_grid)
cost_grid
for i in range(len(theta0_vals)):
for j in range(len(theta1_vals)):
= compute_cost(theta0_vals[i], theta1_vals[j])
cost_grid[i, j]
= widgets.FloatSlider(
theta0_slider =0, min=-2, max=4, step=0.1, description='Theta0:')
value= widgets.FloatSlider(
theta1_slider =0, min=-2, max=4, step=0.1, description='Theta1:')
value
= widgets.Output()
figure_container
def update_figure(change):
with figure_container:
=True)
clear_output(wait=(12, 5))
plt.figure(figsize
1, 2, 1)
plt.subplot(
plt.contourf(theta0_grid, theta1_grid, cost_grid,=20, cmap='viridis')
levels='Cost')
plt.colorbar(label
= 1.0
true_theta0 = 1.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(='Data')
plt.scatter(X, y, label+ theta1_slider.value *
plt.plot(X, theta0_slider.value ='red', label='Line Fit')
X, color'X')
plt.xlabel('y')
plt.ylabel('Linear Regression Line Fit')
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])]))
# plt.figure(figsize=(12, 5))
1, 2, 1)
plt.subplot(=20, cmap='viridis')
plt.contourf(theta0_grid, theta1_grid, cost_grid, levels='Cost')
plt.colorbar(label
= 1.0
true_theta0 = 1.0
true_theta1 ='blue',
plt.scatter([true_theta0], [true_theta1], color='o', label='True Values')
marker0.5], [0.2], 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(='Data')
plt.scatter(X, y, label0.5 + 0.2 * X, color='red', label='Line Fit')
plt.plot(X, 'X')
plt.xlabel('y')
plt.ylabel('Linear Regression Line Fit')
plt.title(
plt.legend()
# plt.tight_layout()
'figures/mle/lin_reg_slider_1.pdf') plt.savefig(