<jupyter_start><jupyter_text>Lasso and Ridge Regression**Lasso regression:** It is a type of linear regression that uses shrinkage. Shrinkage is where data values are shrunk towards a central point, like the mean.**Ridge Regression:** It is a way to create a predictive and explonatory model when the number of predictor variables in a set exceeds the number of observations, or when a data set has multicollinearity (correlations between predictor variables).- With this brief knowledge of Lasso and Ridge, in this notebook we are going to predict the Height of the person given the age.**Dataset can be directly downloaded from here.**  Importing Libraries<jupyter_code>import pandas as pd
import numpy as np

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

import sklearn
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.linear_model import Ridge<jupyter_output><empty_output><jupyter_text>Importing Dataset<jupyter_code>!wget 'https://archive.org/download/ages-and-heights/AgesAndHeights.pkl'
raw_data = pd.read_pickle(
    'AgesAndHeights.pkl'
)  # Dataset Link: https://archive.org/download/ages-and-heights/AgesAndHeights.pkl
raw_data
raw_data.describe()
raw_data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 100 entries, 0 to 99
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   Age     100 non-null    float64
 1   Height  100 non-null    float64
dtypes: float64(2)
memory usage: 1.7 KB<jupyter_text>Data Visualisation<jupyter_code>sns.histplot(raw_data['Age'])
plt.show()
sns.histplot(raw_data['Height'], kde=False, bins=10)
plt.show()<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>cleaned = raw_data[raw_data['Age'] > 0]
cleaned.shape

# 7 Columns in the Dataset where the Age was less than 0, which is pretty unobvious.
sns.histplot(cleaned['Age'], kde=True)
plt.show()
cleaned.describe()
cleaned.info()
sns.scatterplot(cleaned['Age'],
                cleaned['Height'],
                label='Age, Height')
plt.title('Age VS Height', color='blue')
plt.xlabel('Age', color='green')
plt.ylabel('Height', color='green')
plt.legend()
plt.show()<jupyter_output>C:\Users\<NAME>\AppData\Local\Programs\Python\Python39\lib\site-packages\seaborn\_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  warnings.warn(<jupyter_text>**Scaling the Data in the range of (0, 1) to fit the model easily.**<jupyter_code>scaler = MinMaxScaler()
cleaned_data = pd.DataFrame(scaler.fit_transform(cleaned))
cleaned_data.columns = ['Age', 'Height']
cleaned_data<jupyter_output><empty_output><jupyter_text>Model Building<jupyter_code>age = cleaned_data['Age']
height = cleaned_data['Height']<jupyter_output><empty_output><jupyter_text>Lasso<jupyter_code>model_l = Lasso()
X = cleaned_data[['Age']]
y = cleaned_data[['Height']]
model_l.fit(X, y)<jupyter_output><empty_output><jupyter_text>Lasso - Predict<jupyter_code>np.float64(model_l.predict([[16]]) * 100)<jupyter_output><empty_output><jupyter_text>Ridge<jupyter_code>model_r = Ridge()
model_r.fit(X, y)<jupyter_output><empty_output><jupyter_text>Ridge - Predict<jupyter_code>np.float64(model_r.predict([[16]]) * 10)<jupyter_output><empty_output><jupyter_text>With and Without Regularisation Use `ML Algorithms` to build and train the model. Here, `Simple Linear Regression` is used. Before that let's create a necessary environment to build the model.Actual -> $y = \alpha + \beta  x + \epsilon$True -> $\hat{y} = \alpha + \beta  x$<jupyter_code># random parameter values

parameters = {'alpha': 40, 'beta': 4}

# y_hat using formulas mentioned above

def y_hat(age, params):
    alpha = params['alpha']
    beta = params['beta']
    return alpha + beta * age


age = int(input('Enter age: '))
y_hat(age, parameters)
# learning better parameters for optimum result (using Regularisation)

def learn_parameters(data, params):
    x, y = data['Age'], data['Height']
    x_bar, y_bar = x.mean(), y.mean()
    x, y = x.to_numpy(), y.to_numpy()
    beta = sum(((x - x_bar) * (y - y_bar)) / sum((x - x_bar)**2))
    alpha = y_bar - beta * x_bar
    params['alpha'] = alpha
    params['beta'] = beta
# new parameters derived from 'learn_parameters' function

new_parameter = {'alpha': -2, 'beta': 1000}
learn_parameters(cleaned, new_parameter)
new_parameter
# general untrained predictions

spaced_ages = list(range(19))
spaced_untrained_predictions = [y_hat(x, parameters) for x in spaced_ages]
print(spaced_untrained_predictions)
# Untrained Predictions

ages = cleaned_data[['Age']] * 17.887852
heights = cleaned_data[['Height']] * 68.170414
plt.scatter(ages, heights, label='Raw Data')
plt.plot(spaced_ages,
         spaced_untrained_predictions,
         label='Untrained Predictions',
         color='green')
plt.title('Height VS Age')
plt.xlabel('Age[Years]')
plt.ylabel('Height[Inches]')
plt.legend()
plt.show()
# Trained Predictions

spaced_trained_predictions = [y_hat(x, new_parameter) for x in spaced_ages]
print('Trained Predicted Values: ',spaced_trained_predictions)
plt.scatter(ages,heights, label='Raw Data')
plt.plot(spaced_ages, spaced_untrained_predictions, label = 'Untrained Predictions', color = 'green')
plt.plot(spaced_ages, spaced_trained_predictions, label = 'Trained Predictions', color = 'red')
plt.title('Height VS Age')
plt.xlabel('Age[Years]')
plt.ylabel('Height[Inches]')
plt.legend()
plt.show()

# We can see that the result is not optimal but has changed significantly from a normal Linear Regression Type Model<jupyter_output>Trained Predicted Values:  [29.96185761461583, 31.96327451352646, 33.96469141243709, 35.966108311347725, 37.96752521025836, 39.96894210916899, 41.97035900807962, 43.97177590699025, 45.97319280590088, 47.97460970481151, 49.97602660372215, 51.97744350263278, 53.97886040154341, 55.98027730045404, 57.98169419936467, 59.9831110982753, 61.984527997185936, 63.98594489609657, 65.98736179500719]
{'fraction_non_alphanumeric': 0.12333600641539695, 'fraction_numerical': 0.061427425821972734, 'mean_word_length': 5.748917748917749, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 47, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '28829', 'license': 'MIT', 'n_tokens_mistral': 2434, 'n_tokens_neox': 2111, 'n_words': 530, 'path': 'Datascience_With_Python/Machine Learning/Algorithms/Lasso and Ridge/lasso_and_ridge.ipynb', 'repo_name': 'vishnupriya129/winter-of-contributing', 'size': '6240'}