<jupyter_start><jupyter_text>Pipeline for RegressionBuild a scalable, ElasticNet regression pipeline with Gapminder dataset. Involves tuning of l1_ratio of the ElasticNet using GridSearchCV.<jupyter_code>#Loading the requisite packages
#Scikit-Learn packages
from sklearn.preprocessing import scale, StandardScaler, Imputer
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV, train_test_split
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.linear_model import ElasticNet
#Computation and Visualization Packages
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
#Loading the requisite dataset
df = pd.read_csv('gapminder_dataset.csv')
df.info()
df.head()
df.tail()
#Specifying the feature and target variables
X = df.drop(['life'], axis=1).values
y = df.life.values
# Training and Test Data split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42)
# Establishing the Pipeline Steps
steps = [('imputation', Imputer(missing_values='NaN', strategy='mean', axis=0)),
         ('scaler', StandardScaler()),
         ('elasticnet', ElasticNet())]

pipeline = Pipeline(steps)
# Specifying the hyperparameter space
parameters = {'elasticnet__l1_ratio':np.linspace(0,1,30)}
# Creating the GridSearchCV object
gm_cv = GridSearchCV(pipeline, parameters, cv=5)
gm_cv.fit(X_train, y_train)
# Computation of the metrics
r2 = gm_cv.score(X_test, y_test)
print("Tuned ElasticNet Alpha: {}".format(gm_cv.best_params_))
print("Tuned ElasticNet R squared: {}".format(r2))<jupyter_output>Tuned ElasticNet Alpha: {'elasticnet__l1_ratio': 1.0}
Tuned ElasticNet R squared: 0.886201657089
{'fraction_non_alphanumeric': 0.09938236945536216, 'fraction_numerical': 0.017405951712521055, 'mean_word_length': 6.955357142857143, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '529406', 'license': 'Unlicense', 'n_tokens_mistral': 605, 'n_tokens_neox': 569, 'n_words': 161, 'path': 'Regression/Pipeline-Regression.ipynb', 'repo_name': 'Aditya-Codes/MachineLearning', 'size': '1781'}