import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib
Ridge
Interactive tutorial on ridge with practical implementations and visualizations
from latexify import latexify, format_axes
latexify()
#Define input array with angles from 60deg to 300deg converted to radians
= np.array([i*np.pi/180 for i in range(60,300,4)])
x 10) #Setting seed for reproducability
np.random.seed(= 4*x + 7 + np.random.normal(0,3,len(x))
y = 4*x + 7
y_true = 20
max_deg = [x**(i+1) for i in range(max_deg)] + [y]
data_x = ['x'] + ['x_{}'.format(i+1) for i in range(1,max_deg)] + ['y']
data_c = pd.DataFrame(np.column_stack(data_x),columns=data_c)
data "ones"] = 1
data['x'],data['y'],'.', label='Data Points')
plt.plot(data['x'], y_true,'g', label='True Function')
plt.plot(data["x")
plt.xlabel("y")
plt.ylabel(
plt.legend()
format_axes(plt.gca())'lin_1.pdf', transparent=True, bbox_inches="tight") plt.savefig(
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[2], line 16 14 plt.ylabel("y") 15 plt.legend() ---> 16 format_axes(plt.gca()) 17 plt.savefig('lin_1.pdf', transparent=True, bbox_inches="tight") NameError: name 'format_axes' is not defined
from sklearn.preprocessing import PolynomialFeatures
for i, deg in enumerate([1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 15, 20, 25]):
= PolynomialFeatures(degree=deg, include_bias=False)
poly = poly.fit_transform(x.reshape(-1, 1))
X_ from sklearn.linear_model import LinearRegression
= LinearRegression()
clf 'y'])
clf.fit(X_, data[print(X_.shape)
= clf.predict(X_)
y_pred
plt.figure()'x'], y_pred, label='Degree: {}'.format(deg))
plt.plot(data["x")
plt.xlabel("y")
plt.ylabel(
plt.scatter(x, y)
plt.legend()= np.sqrt(np.mean((y_pred - y)**2))
train_rmse = np.max([np.max(clf.coef_), clf.intercept_])
highest_coef = np.sqrt(np.sum(clf.coef_**2) + clf.intercept_**2)
sqrt_sum_squares_coef 'RMSE: {:.2f}\n Highest Coef: {:.2f}\n Sqrt Sum of Squares of Coef: {:.2f}'.format(train_rmse, highest_coef, sqrt_sum_squares_coef))
plt.title(#format_axes(plt.gca())
#plt.savefig('lin_{}.pdf'.format(i+2), transparent=True, bbox_inches="tight")
(60, 1)
(60, 2)
(60, 3)
(60, 4)
(60, 5)
(60, 6)
(60, 7)
(60, 8)
(60, 10)
(60, 12)
(60, 15)
(60, 20)
(60, 25)
from sklearn.linear_model import LinearRegression
= []
coef_list for i,deg in enumerate([1,3,6,11]):
= ['ones','x']
predictors if deg >= 2:
'x_%d'%i for i in range(2,deg+1)])
predictors.extend([
= LinearRegression(normalize=False, fit_intercept=False)
regressor 'y'])
regressor.fit(data[predictors],data[= regressor.predict(data[predictors])
y_pred abs(max(regressor.coef_, key=abs)))
coef_list.append(
'x'],data['y'], label='Train')
plt.scatter(data['x'], y_pred,'k', label='Prediction')
plt.plot(data['x'], y_true,'g.', label='True Function')
plt.plot(data[
format_axes(plt.gca())
plt.legend() f"Degree: {deg} | Max Coeff: {max(regressor.coef_, key=abs):.2f}")
plt.title('lin_plot_{}.pdf'.format(deg), transparent=True, bbox_inches="tight")
plt.savefig( plt.clf()
<Figure size 244.08x150.85 with 0 Axes>
1,3,6,11],coef_list,'o-k')
plt.semilogy([1,3,6,11])
plt.xticks(['Degree')
plt.xlabel('Max Coef')
plt.ylabel(
format_axes(plt.gca())'lin_plot_coef.pdf', transparent=True, bbox_inches="tight") plt.savefig(
def cost(theta_0, theta_1, x, y):
= 0
s for i in range(len(x)):
= x[i]*theta_1 + theta_0
y_i_hat += (y[i]-y_i_hat)**2
s return s/len(x)
= np.mgrid[-4:15:.2, -4:15:.2]
x_grid, y_grid
= np.zeros_like(x_grid)
cost_matrix for i in range(x_grid.shape[0]):
for j in range(x_grid.shape[1]):
= cost(x_grid[i, j], y_grid[i, j], data['x'], data['y']) cost_matrix[i, j]
from matplotlib.patches import Circle
= np.sort(np.array([2,10,20,50,100,150,200,400,600,800,1000]))
levels
=.4)
plt.contourf(x_grid, y_grid, cost_matrix, levels,alpha
plt.colorbar()0, color='black', alpha=.5, dashes=[2, 4],linewidth=1)
plt.axhline(0, color='black', alpha=0.5, dashes=[2, 4],linewidth=1)
plt.axvline(
= plt.contour(x_grid, y_grid, cost_matrix, levels, linewidths=1,colors='black')
CS =1, fontsize=8)
plt.clabel(CS, inline"Least squares objective function")
plt.title(r"$\theta_0$")
plt.xlabel(r"$\theta_1$")
plt.ylabel(= Circle((0, 0), 3, color='g', label=r'$\theta_0^2+\theta_1^2=3$')
p1 7], [4],marker='*', color='r',s=25,label='Actual Solution')
plt.scatter([
plt.gca().add_patch(p1)
plt.legend()'equal')
plt.gca().set_aspect(
format_axes(plt.gca())'ridge_base_contour.pdf', transparent=True, bbox_inches="tight")
plt.savefig( plt.show()
= plt.figure(figsize=(7,7))
fig = plt.axes(projection='3d')
ax
= plt.axes(projection='3d')
ax ='viridis', edgecolor='none')
ax.plot_surface(x_grid, y_grid, cost_matrix,cmap'Least squares objective function');
ax.set_title(r"$\theta_0$")
ax.set_xlabel(r"$\theta_1$")
ax.set_ylabel(-4,15])
ax.set_xlim([-4,15])
ax.set_ylim([
= np.linspace(0, np.pi, 30)
u = np.linspace(0, 2 * np.pi, 30)
v
# x = np.outer(500*np.sin(u), np.sin(v))
# y = np.outer(500*np.sin(u), np.cos(v))
# z = np.outer(500*np.cos(u), np.ones_like(v))
# ax.plot_wireframe(x, y, z)
45, 120)
ax.view_init('ridge_base_surface.pdf', transparent=True, bbox_inches="tight") plt.savefig(
=5, fig_height=2.5)
latexify(fig_widthfrom sklearn.linear_model import Ridge
for alpha in [1, 10, 1000]:
= plt.subplots(nrows=1, ncols=2)
fig, ax
= 1
deg = ['ones','x']
predictors if deg >= 2:
'x_%d'%i for i in range(2,deg+1)])
predictors.extend([
= Ridge(alpha=alpha,normalize=True, fit_intercept=False)
regressor 'y'])
regressor.fit(data[predictors],data[= regressor.predict(data[predictors])
y_pred 0])
format_axes(ax[1])
format_axes(ax[# Plot
0].scatter(data['x'],data['y'], label='Train')
ax[0].plot(data['x'], y_pred,'k', label='Prediction')
ax[0].plot(data['x'], y_true,'g.', label='True Function')
ax[0].legend()
ax[0].set_title(f"Degree: {deg} | $\mu$: {alpha} | Max Coef: {max(regressor.coef_, key=abs):.2f}")
ax[
# Circle
= Circle((0, 0), np.sqrt(regressor.coef_.T@regressor.coef_), alpha=0.6, color='g', label=r'$\theta_0^2+\theta_1^2={:.2f}$'.format(np.sqrt(regressor.coef_.T@regressor.coef_)))
p1 1].add_patch(p1)
ax[
# Contour
= np.sort(np.array([2,10,20,50,100,150,200,400,600,800,1000]))
levels 1].contourf(x_grid, y_grid, cost_matrix, levels,alpha=.3)
ax[#ax[1].colorbar()
1].axhline(0, color='black', alpha=.5, dashes=[2, 4],linewidth=1)
ax[1].axvline(0, color='black', alpha=0.5, dashes=[2, 4],linewidth=1)
ax[
= plt.contour(x_grid, y_grid, cost_matrix, levels, linewidths=1,colors='black')
CS 1].clabel(CS, inline=1, fontsize=8)
ax[1].set_title("Least squares objective function")
ax[1].set_xlabel(r"$\theta_0$")
ax[1].set_ylabel(r"$\theta_1$")
ax[1].scatter(regressor.coef_[0],regressor.coef_[1] ,marker='x', color='r',s=25,label='Ridge Solution')
ax[1].scatter([7], [4],marker='*', color='r',s=25,label='Actual Solution')
ax[1].set_xlim([-4,15])
ax[1].set_ylim([-4,15])
ax[1].legend()
ax[1].set_aspect('equal')
ax['ridge_{}.pdf'.format(alpha), transparent=True, bbox_inches="tight")
plt.savefig(
plt.show() plt.clf()
<Figure size 360x180 with 0 Axes>
<Figure size 360x180 with 0 Axes>
<Figure size 360x180 with 0 Axes>
latexify()from sklearn.linear_model import Ridge
for i,deg in enumerate([19]):
= ['ones', 'x']
predictors if deg >= 2:
'x_%d'%i for i in range(2,deg+1)])
predictors.extend([
for i,alpha in enumerate([1, 1e7]):
= Ridge(alpha=alpha,normalize=False, fit_intercept=False)
regressor 'y'])
regressor.fit(data[predictors],data[= regressor.predict(data[predictors])
y_pred 'x'],data['y'], label='Train')
plt.scatter(data['x'], y_pred,'k', label='Prediction')
plt.plot(data['x'], y_true,'g.', label='True Function')
plt.plot(data[
plt.legend()
format_axes(plt.gca())f"Degree: {deg} | $\mu$: {alpha} | Max Coeff: {max(regressor.coef_, key=abs):.2f}")
plt.title('ridge_{}_{}.pdf'.format(alpha, deg), transparent=True, bbox_inches="tight")
plt.savefig(
plt.show() plt.clf()
<Figure size 244.08x150.85 with 0 Axes>
# from sklearn.linear_model import Ridge
# for i,deg in enumerate([2,4,8,16]):
# predictors = ['x']
# if deg >= 2:
# predictors.extend(['x_%d'%i for i in range(2,deg+1)])
# fig, ax = plt.subplots(nrows=1, ncols=4, sharey=True, figsize=(20, 4))
# for i,alpha in enumerate([1e-15,1e-4,1,20]):
# regressor = Ridge(alpha=alpha,normalize=True)
# regressor.fit(data[predictors],data['y'])
# y_pred = regressor.predict(data[predictors])
# ax[i].scatter(data['x'],data['y'], label='Train')
# ax[i].plot(data['x'], y_pred,'k', label='Prediction')
# ax[i].plot(data['x'], y_true,'g.', label='True Function')
# ax[i].legend()
# ax[i].set_title(f"Degree: {deg} | Alpha: {alpha} | Max Coeff: {max(regressor.coef_, key=abs):.2f}")
import pandas as pd
= pd.read_excel("data.xlsx")
data = data.columns
cols = np.logspace(-2,10,num=20, endpoint=False)
alph_list = []
coef_list
for i,alpha in enumerate(alph_list):
= Ridge(alpha=alpha,normalize=True)
regressor 1:-1]],data[cols[-1]])
regressor.fit(data[cols[
coef_list.append(regressor.coef_)
= np.abs(np.array(coef_list).T)
coef_list for i in range(len(cols[1:-1])):
=r"$\theta_{}$".format(i))
plt.loglog(alph_list, coef_list[i] , label'$\mu$ value')
plt.xlabel('Coefficient Value')
plt.ylabel(
plt.legend()
format_axes(plt.gca())
= True
lim if lim:
10e-20, 100))
plt.ylim(('rid_reg-without-lim.pdf', transparent=True, bbox_inches="tight")
plt.savefig(else:
'rid_reg-with-lim.pdf', transparent=True, bbox_inches="tight")
plt.savefig(
# plt.set_title(f"Degree: {deg} | Alpha: {alpha} | Max Coeff: {max(regressor.coef_, key=abs):.2f}")
Question
'seaborn-whitegrid')
plt.style.use(
= [1,2,3,4]
x = [1,2,3,0]
y = [(2-i/5) for i in x]
y_1 = [(0.5+0.4*i) for i in x]
y_2 -0.2,3.3)
plt.ylim('.')
plt.plot(x,y,for i in range(len(x)):
-0.1, y[i]+0.1, "({},{})".format(x[i], y[i]))
plt.text(x[i]#plt.plot(x,y_1, label="unreg")
#plt.plot(x,y_2, label="reg")
#plt.legend()
#format_axes(plt.gca())
'temp.pdf', transparent=True) plt.savefig(
from sklearn.linear_model import LinearRegression
= np.vstack([np.ones_like(x), x]).T
x_new = LinearRegression(normalize=False, fit_intercept=False)
regressor
regressor.fit(x_new,y)= regressor.predict(x_new)
y_pred
for i in range(len(x)):
-0.1, y[i]+0.1, "({},{})".format(x[i], y[i]))
plt.text(x[i]'.', label='Data Points')
plt.plot(x,y,'-g', label='Unregularized Fit')
plt.plot(x,y_pred,='lower left')
plt.legend(loc#format_axes(plt.gca())
'q_unreg.pdf', transparent=True) plt.savefig(
from sklearn.linear_model import Ridge
= Ridge(alpha=4, normalize=False, fit_intercept=False)
regressor
regressor.fit(x_new,y)= regressor.predict(x_new)
y_pred_r
for i in range(len(x)):
-0.1, y[i]+0.1, "({},{})".format(x[i], y[i]))
plt.text(x[i]def ridge_func(x):
return 0.56 + 0.26*np.array(x)
'.', label='Data Points')
plt.plot(x,y,'-g', label='Unregularized Fit')
plt.plot(x,y_pred,'-b', label='Regularized Fit')
plt.plot(x,ridge_func(x),='lower left')
plt.legend(loc#format_axes(plt.gca())
'q_reg.pdf', transparent=True) plt.savefig(
regressor.coef_
array([0.37209302, 0.30232558])
Retrying with a better example
4, 10], [10, 34]])@np.array([6, 14]) np.linalg.inv([[
array([ 1.77777778, -0.11111111])
8, 10], [10, 34]])@np.array([6, 14]) np.linalg.inv([[
array([0.37209302, 0.30232558])
#Define input array with angles from 60deg to 300deg converted to radians
= np.array([i*np.pi/180 for i in range(60,600,10)])
x 10) #Setting seed for reproducability
np.random.seed(= 4*x + 7 + np.random.normal(0,5,len(x))
y = 4*x + 7
y_true = 20
max_deg = [x**(i+1) for i in range(max_deg)] + [y]
data_x = ['x'] + ['x_{}'.format(i+1) for i in range(1,max_deg)] + ['y']
data_c = pd.DataFrame(np.column_stack(data_x),columns=data_c)
data "ones"] = 1
data['x'],data['y'],'.', label='Data Points')
plt.plot(data['x'], y_true,'g', label='True Function')
plt.plot(data["x")
plt.xlabel("y")
plt.ylabel(
plt.legend()#format_axes(plt.gca())
'lin_1.pdf', transparent=True, bbox_inches="tight") plt.savefig(
from sklearn.linear_model import Ridge
for i,deg in enumerate([17]):
= ['ones', 'x']
predictors if deg >= 2:
'x_%d'%i for i in range(2,deg+1)])
predictors.extend([
for i,alpha in enumerate([1e-3, 1e7]):
= Ridge(alpha=alpha,normalize=False)
regressor 'y'])
regressor.fit(data[predictors],data[= regressor.predict(data[predictors])
y_pred 'x'],data['y'], label='Train')
plt.scatter(data['x'], y_pred,'k', label='Prediction')
plt.plot(data['x'], y_true,'g.', label='True Function')
plt.plot(data[
plt.legend() #format_axes(plt.gca())
#print(regressor.coef_)
0,60])
plt.ylim([f"Degree: {deg} | $\mu$: {alpha}")
plt.title('ridge_new_{}_{}.pdf'.format(i, deg), transparent=True, bbox_inches="tight")
plt.savefig(
plt.show() plt.clf()
<Figure size 244.08x150.85 with 0 Axes>