Loading…
DS · Regression · Program 13 · In the record
Ridge and Lasso Regression.
1import pandas as pd2from sklearn.model_selection import train_test_split3from sklearn.pipeline import make_pipeline4from sklearn.preprocessing import StandardScaler5from sklearn.linear_model import Ridge, Lasso6 7df = pd.DataFrame({8 'Experience': range(1, 16),9 'Education': [12, 14, 16, 16, 14, 18, 16, 18, 20, 16, 18, 20, 18, 20, 22],10 'Projects': [2, 3, 5, 4, 6, 7, 6, 8, 9, 8, 10, 11, 10, 12, 13],11 'Distance': [12, 30, 8, 25, 40, 15, 33, 6, 22, 18, 35, 10, 28, 20, 14],12 'Salary': [39000, 44500, 49500, 50500, 53000, 59500, 61000, 68500,13 71000, 69000, 76500, 81500, 81000, 85000, 91500]})14X_train, X_test, y_train, y_test = train_test_split(df.drop(columns='Salary'), df['Salary'], test_size=0.3, random_state=1)15 16for name, m in [('Ridge', Ridge(alpha=1.0)), ('Lasso', Lasso(alpha=100.0, max_iter=10000))]:17 model = make_pipeline(StandardScaler(), m).fit(X_train, y_train)18 print(f"{name} R2 Score:", model.score(X_test, y_test))19 print(f"{name} Coefs:", m.coef_.round(2))Ridge R2 Score: 0.9735442276317761 Ridge Coefs: [7769.75 3114.27 5702.76 -193.01] Lasso R2 Score: 0.9800886182117426 Lasso Coefs: [13465.47 3363.96 408.81 -0. ]

Run program 13 in your browser
Python 3.14 · Pyodide. Change the code or the input, run it, break it, and see what happens.