11import numpy as np
22import matplotlib .pyplot as plt
33
4+ < << << << HEAD :Regularization / usingLibs / Ridge_ass1 .py
45
6+ == == == =
7+ # Set seed for reproducibility
8+ >> >> >> > dce2152 (ass5 .1 ):Linear Model Selection and Regularization / Ridge_ass1 .py
59np .random .seed (0 )
610X = np .random .rand (100 , 1 ) * 10
711y = 2 * X .squeeze () + 1 + np .random .randn (100 ) * 2
812
13+ << < << << HEAD :Regularization / usingLibs / Ridge_ass1 .py
914
15+ == == == =
16+ # Ridge Regression function
17+ >> > >> >> dce2152 (ass5 .1 ):Linear Model Selection and Regularization / Ridge_ass1 .py
1018def ridge_regression (X , y , alpha ):
1119 n = len (X )
1220 mean_X = sum (X ) / n
@@ -17,16 +25,28 @@ def ridge_regression(X, y, alpha):
1725 denominator = sum ((X [i ] - mean_X ) ** 2 for i in range (n )) + alpha
1826 b1 = numerator / denominator
1927
28+ << < << << HEAD :Regularization / usingLibs / Ridge_ass1 .py
2029 # bias (b0)
30+ == == == =
31+ # intercept (b0)
32+ >> > >> > > dce2152 (ass5 .1 ):Linear Model Selection and Regularization / Ridge_ass1 .py
2133 b0 = mean_y - b1 * mean_X
2234
2335 return b0 , b1
2436
37+ << < << << HEAD :Regularization / usingLibs / Ridge_ass1 .py
2538
2639def predict (X , b0 , b1 ):
2740 return [b0 + b1 * x for x in X ]
2841
2942# SSR + λ * slope^2
43+ == == == =
44+ # Prediction function
45+ def predict (X , b0 , b1 ):
46+ return [b0 + b1 * x for x in X ]
47+
48+ # SSR + λ * slope^2 function
49+ >> > >> >> dce2152 (ass5 .1 ):Linear Model Selection and Regularization / Ridge_ass1 .py
3050def ssr_plus_reg (X , y , slope , alpha ):
3151 n = len (X )
3252 predicted_y = [slope * x + 1 for x in X ]
@@ -35,15 +55,25 @@ def ssr_plus_reg(X, y, slope, alpha):
3555 alpha_slop2 = alpha * slope ** 2
3656 return squared_residuals + alpha_slop2
3757
58+ << < << << HEAD :Regularization / usingLibs / Ridge_ass1 .py
3859
60+ == == == =
61+ # Plotting Data
62+ >> > >> >> dce2152 (ass5 .1 ):Linear Model Selection and Regularization / Ridge_ass1 .py
3963plt .figure (figsize = (14 , 10 ))
4064plt .subplot (2 , 3 , 1 )
4165plt .scatter (X , y , color = 'blue' , edgecolor = 'k' )
4266plt .title ('Data' )
4367
68+ << < << << HEAD :Regularization / usingLibs / Ridge_ass1 .py
4469alphas = [0 , 10 , 20 , 40 , 400 ]
4570
4671# Ridge Regression for different alpha values
72+ == == == =
73+ alphas = [0 , 1 , 10 , 100 , 1000 ]
74+
75+ # Perform Ridge Regression for different alpha values and plot results
76+ >> > >> >> dce2152 (ass5 .1 ):Linear Model Selection and Regularization / Ridge_ass1 .py
4777for i , alpha in enumerate (alphas ):
4878 b0 , b1 = ridge_regression (X , y , alpha )
4979 y_pred = predict (X , b0 , b1 )
@@ -64,7 +94,11 @@ def ssr_plus_reg(X, y, slope, alpha):
6494 cost_ridge = [(slope - 0.5 ) ** 2 + alpha * slope ** 2 for slope in slope_values ]
6595 plt .plot (slope_values , cost_ridge , label = f'λ = { alpha } ' )
6696
97+ << < << << HEAD :Regularization / usingLibs / Ridge_ass1 .py
6798plt .title ('Sum of Squared Residuals + λ * Slope^2' )
99+ == == == =
100+ plt .title ('Sum of Squared Residuals + λ * Slope² (Ridge)' )
101+ >> > >> >> dce2152 (ass5 .1 ):Linear Model Selection and Regularization / Ridge_ass1 .py
68102plt .xlabel ('Slope Values' )
69103plt .ylabel ('Cost' )
70104plt .legend ()
0 commit comments