Skip to content

Commit d60018e

Browse files
committed
elastic net regularization code
1 parent 4a64662 commit d60018e

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

ml-concepts/elastic_net.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from sklearn.linear_model import ElasticNet
2+
from sklearn.model_selection import train_test_split
3+
from sklearn.datasets import fetch_california_housing
4+
from sklearn.metrics import mean_squared_error
5+
6+
data = fetch_california_housing()
7+
X = data.data
8+
y = data.target
9+
10+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
11+
12+
elastic_net = ElasticNet(alpha=1.0, l1_ratio=0.5) # l1_ratio=0.5 means equal balance between L1 and L2
13+
14+
15+
elastic_net.fit(X_train, y_train)
16+
17+
y_pred = elastic_net.predict(X_test)
18+
19+
mse = mean_squared_error(y_test, y_pred)
20+
print(f'Mean Squared Error: {mse}')
21+
22+
print('Coefficients:', elastic_net.coef_)

0 commit comments

Comments
 (0)