<jupyter_start><jupyter_text>Regression:    Regression analysis is a set of statistical processes for estimating the relationships between a dependent variable (often called the 'outcome variable') and one or more independent variables (often called 'predictors', 'covariates', or 'features'). The most common form of regression analysis is linear regression, in which a researcher finds the line (or a more complex linear combination) that most closely fits the data according to a specific mathematical criterion.<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('Gapminder.csv')
df.head()
df.columns
# Create arrays for features and target variable
y = df['life'].values
X = df['fertility'].values
# Print the dimensions of X and y before reshaping
print("Dimensions of y before reshaping: {}".format(y.shape))
print("Dimensions of X before reshaping: {}".format(X.shape))
# Reshape X and y
y = y.reshape(-1, 1)
X = X.reshape(-1, 1)
# Print the dimensions of X and y after reshaping
print("Dimensions of y after reshaping: {}".format(y.shape))
print("Dimensions of X after reshaping: {}".format(X.shape))
import seaborn as sns
sns.heatmap(df.corr(), square=True, cmap='RdYlGn')
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state=0)
# Create the regressor: reg
reg = LinearRegression()
# Fitting the regressor to the training data
reg.fit(X_train, y_train)
# Predicting on the test data: y_pred
y_pred = reg.predict(X_test)
# Computing and printing R^2 and RMSE
print("R^2: {}".format(reg.score(X_test, y_test)))
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print("Root Mean Squared Error: {}".format(rmse))
from sklearn.model_selection import cross_val_score
# Creating a linear regression
reg = LinearRegression()
#Computing 5-fold cross-validation scores
cv_scores = cross_val_score(reg, X, y, cv=5)
print(cv_scores)
#Printing the average 5-fold cross-validation score
print("Average 5-Fold CV Score: {}".format(np.mean(cv_scores)))
#Performing 3-fold CV
cvscores_3 = cross_val_score(reg, X, y, cv = 3)
print(np.mean(cvscores_3))
#Perform 10-fold CV
cvscores_10 = cross_val_score(reg, X, y, cv = 10)
print(np.mean(cvscores_10))<jupyter_output>0.5883937741571185<jupyter_text>Regularized Regression<jupyter_code>from sklearn.linear_model import Lasso
#Instantiating a lasso regressor
lasso = Lasso(alpha=0.4, normalize=True)
#Fitting the regressor to the data
lasso.fit(X, y)
#Computing and printing the coefficients
lasso_coef = lasso.coef_
print(lasso_coef)<jupyter_output>[-1.51388042]<jupyter_text>REGULARIZATION II: Ridge<jupyter_code>from sklearn.linear_model import Ridge
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()
#Setting up the array of alphas and lists to store scores
alpha_space = np.logspace(-4, 0, 50)
ridge_scores = []
ridge_scores_std = []
#Creating a ridge regressor
ridge = Ridge(normalize=True)
#Computing the scores over range of alphas, specify the alpha value to use
for alpha in alpha_space:
    ridge.alpha = alpha
    ridge_cv_scores = cross_val_score(ridge, X, y, cv=10)
    ridge_scores.append(np.mean(ridge_cv_scores))
    ridge_scores_std.append(np.std(ridge_cv_scores))
display_plot(ridge_scores, ridge_scores_std)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10697911360163016, 'fraction_numerical': 0.017575140091696383, 'mean_word_length': 5.865384615384615, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '374440', 'license': 'MIT', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1315, 'n_words': 383, 'path': 'supervised_learning/Regression.ipynb', 'repo_name': 'mayowak/DataCamp_DataScience', 'size': '3926'}