<jupyter_start><jupyter_text>Regularization I : Lasso Lasso can be used for feature selection from data set.<jupyter_code># Importing Libraries 
import numpy as np
import pandas as pd
import seaborn as sns

from matplotlib import pyplot as plt
from sklearn.linear_model import Lasso
# Reading data
df = pd.read_csv('gapminder.csv')
df.head()
df.drop(['Region'], inplace=True, axis=1)
df.head()
df.info()
df.describe()
df.corr()
sns.heatmap(df.corr(), vmin=-1,vmax=1, linewidths=0.5, cmap="YlGnBu")
# features and labels
X = df.drop(['life'], axis=1).values
y = df['life'].values
# Instantiate a lasso regressor: lasso
lasso = Lasso(alpha=0.4, normalize=True)
# Fit the regressor to the data
lasso.fit(X, y)
# Compute and print the coefficients
lasso_coef = lasso.coef_
print(lasso_coef)
df_columns = df.columns.drop(['life'])
df_columns
# Plot the coefficients
sns.set()
plt.plot(range(len(df_columns)), lasso_coef)
plt.xticks(range(len(df_columns)), df_columns.values, rotation=60)
plt.show()<jupyter_output><empty_output><jupyter_text>Regularization II: Ridge Lasso is great for feature selection, but when building regression models, Ridge regression should be our first choice.<jupyter_code># Import necessary modules
from sklearn.linear_model import Ridge
from sklearn.model_selection import cross_val_score
# Setup the array of alphas and lists to store scores
alpha_space = np.logspace(-4, 0, 50)
ridge_scores = []
ridge_scores_std = []
# Create a ridge regressor: ridge
ridge = Ridge(normalize=True)
# Compute scores over range of alphas
for alpha in alpha_space:

    # Specify the alpha value to use: ridge.alpha
    ridge.alpha = alpha
    
    # Perform 10-fold CV: ridge_cv_scores
    ridge_cv_scores = cross_val_score(ridge, X, y, cv=10)
    
    # Append the mean of ridge_cv_scores to ridge_scores
    ridge_scores.append(np.mean(ridge_cv_scores))
    
    # Append the std of ridge_cv_scores to ridge_scores_std
    ridge_scores_std.append(np.std(ridge_cv_scores))
def display_plot(cv_scores, cv_scores_std):
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.plot(alpha_space, cv_scores)

    std_error = cv_scores_std / np.sqrt(10)

    ax.fill_between(alpha_space, cv_scores + std_error, cv_scores - std_error, alpha=0.2)
    ax.set_ylabel('CV Score +/- Std Error')
    ax.set_xlabel('Alpha')
    ax.axhline(np.max(cv_scores), linestyle='--', color='.5')
    ax.set_xlim([alpha_space[0], alpha_space[-1]])
    ax.set_xscale('log')
    plt.show()
# Display the plot
display_plot(ridge_scores, ridge_scores_std)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11284046692607004, 'fraction_numerical': 0.010894941634241245, 'mean_word_length': 5.078014184397163, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '526745', 'license': 'MIT', 'n_tokens_mistral': 960, 'n_tokens_neox': 897, 'n_words': 231, 'path': 'Lasso_and_Ridge_Regression.ipynb', 'repo_name': 'goutamdadhich/Linear_Regression', 'size': '2570'}