Elastic Net Regression

#Elastic Net regression is a type of linear regression technique that combines
#the features of both L1 Lasso regression and L2 Ridge regression.

#Help prevent overfittting

#best used data set many features with some correlated

#The combined penalty term is controlled by two hyperparameters:alpha, L1 Ratio


  import seaborn as sns
  import pandas as pd
  sns.get_dataset_names()
  tips = sns.load_dataset("tips")
  tips
  tips = pd.get_dummies(tips)
  X = tips.drop('tip', axis=1)
  y = tips['tip']
  from sklearn.model_selection import train_test_split
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=19)
  from sklearn.preprocessing import StandardScaler
  scaler = StandardScaler()
  X_train = scaler.fit_transform(X_train)
  X_test = scaler.transform(X_test)
  from sklearn.linear_model import ElasticNet
  elastic_net = ElasticNet()
  elastic_net.fit(X_train, y_train)
  y_pred = elastic_net.predict(X_test)
  from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
  mean_absolute_error(y_test, y_pred)
  mean_squared_error(y_test, y_pred)
  r2_score(y_test, y_pred)
#l1_ratio: Another way to specify the balance between L1 and L2 regularization.
#Try different values of alpha
  param_grid = { "alpha": [0.1, 0.3, 0.5, 0.7, 0.9], 'l1_ratio': [0.1, 0.3, 0.5, 0.7, 0.9],}
  from sklearn.model_selection import GridSearchCV
  elastic_cv = GridSearchCV(estimator=elastic_net, param_grid=param_grid, cv=3, scoring='neg_mean_squared_error', n_jobs=-1)
  elastic_cv.fit(X_train, y_train)
  y_pred = elastic_cv.predict(X_test)
  mean_absolute_error(y_test, y_pred)
  mean_squared_error(y_test, y_pred)
  r2_score(y_test, y_pred)

Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Leave a Reply

Your email address will not be published. Required fields are marked *