forked from campusx-official/100-days-of-machine-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88e322e
commit 8c8e5fe
Showing
1 changed file
with
166 additions
and
0 deletions.
There are no files selected for viewing
166 changes: 166 additions & 0 deletions
166
day57-elasticnet-regression/elastic-net-regression.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 70, | ||
"id": "71e28874", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from sklearn.datasets import load_diabetes\n", | ||
"from sklearn.linear_model import LinearRegression,Ridge,Lasso,ElasticNet\n", | ||
"from sklearn.model_selection import train_test_split\n", | ||
"from sklearn.metrics import r2_score" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 71, | ||
"id": "284ccf3c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"X,y = load_diabetes(return_X_y=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 72, | ||
"id": "799d9080", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=2)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 73, | ||
"id": "8a242b9f", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"0.4399387660024645" | ||
] | ||
}, | ||
"execution_count": 73, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Linear Regression\n", | ||
"reg = LinearRegression()\n", | ||
"reg.fit(X_train,y_train)\n", | ||
"y_pred = reg.predict(X_test)\n", | ||
"r2_score(y_test,y_pred)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 74, | ||
"id": "a41eb10c", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"0.4519973816947852" | ||
] | ||
}, | ||
"execution_count": 74, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Ridge \n", | ||
"reg = Ridge(alpha=0.1)\n", | ||
"reg.fit(X_train,y_train)\n", | ||
"y_pred = reg.predict(X_test)\n", | ||
"r2_score(y_test,y_pred)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 75, | ||
"id": "b6b53e7c", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"0.4411227990495632" | ||
] | ||
}, | ||
"execution_count": 75, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Lasso\n", | ||
"reg = Lasso(alpha=0.01)\n", | ||
"reg.fit(X_train,y_train)\n", | ||
"y_pred = reg.predict(X_test)\n", | ||
"r2_score(y_test,y_pred)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 76, | ||
"id": "5fc65b12", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"0.4531493801165679" | ||
] | ||
}, | ||
"execution_count": 76, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# ElasticNet\n", | ||
"reg = ElasticNet(alpha=0.005,l1_ratio=0.9)\n", | ||
"reg.fit(X_train,y_train)\n", | ||
"y_pred = reg.predict(X_test)\n", | ||
"r2_score(y_test,y_pred)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "0cb67b0c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.8" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |