<jupyter_start><jupyter_text>Outliers Impact<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns 
sns.set_style('whitegrid')
%matplotlib inline
import pandas as pd<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code>from sklearn.linear_model import LinearRegression 
np.random.seed(42)
n_samples = 100
rng = np.random.randn(n_samples) * 10
print("Feeature shape: ", rng.shape)
y_gen = 0.5 * rng + 2 * np.random.randn(n_samples)
print("\nTarget shape: ", y_gen.shape)

lr = LinearRegression()
lr.fit(rng.reshape(-1, 1), y_gen)
model_pred = lr.predict(rng.reshape(-1, 1))

# plotting 
plt.figure(figsize= (10, 8));
plt.scatter(rng, y_gen);
plt.plot(rng, model_pred);
print("Coefficient Estimate: ", lr.coef_);
idx= rng.argmax()
y_gen[idx] = 200
plt.figure(figsize=(10, 8));
plt.scatter(rng, y_gen);

o_lr = LinearRegression(normalize= True)
o_lr.fit(rng.reshape(-1, 1), y_gen)
o_model_pred = o_lr.predict(rng.reshape(-1, 1))

plt.scatter(rng, y_gen);
plt.plot(rng, o_model_pred)
print("Coefficient Estimate: ", o_lr.coef_)<jupyter_output>Coefficient Estimate:  [0.92796845]<jupyter_text>Ridge Regression<jupyter_code>from sklearn.linear_model import Ridge
ridge_mod = Ridge(alpha= 1, normalize= True)
ridge_mod.fit(rng.reshape(-1, 1), y_gen)
ridge_mod_pred = ridge_mod.predict(rng.reshape(-1,1))

plt.figure(figsize=(10,8))
plt.scatter(rng, y_gen);
plt.plot(rng, ridge_mod_pred);
print("Coefficient of Estimation: ", ridge_mod.coef_)
# ridge_mod_pred<jupyter_output><empty_output><jupyter_text>Lasso Regression<jupyter_code>from sklearn.linear_model import Lasso
# define model 
lasso_mod = Lasso(alpha= 0.4, normalize= True)
lasso_mod.fit(rng.reshape(-1, 1), y_gen)  # (features, target)
lasso_mod_pred = lasso_mod.predict(rng.reshape(-1,1))   # (features)

# plotting
plt.figure(figsize=(10, 8));
plt.scatter(rng, y_gen); # (features, target)
plt.plot(rng, lasso_mod_pred); # (features, prediction)
print("Coefficient Estimation: ", lasso_mod.coef_)  # coefficent change by the rate of alpha<jupyter_output>Coefficient Estimation:  [0.48530263]<jupyter_text>Elastic Net Regression<jupyter_code>from sklearn.linear_model import ElasticNet
# defining model and prediction 
elnet_mod = ElasticNet(alpha= 0.02, normalize= True)
elnet_mod.fit(rng.reshape(-1, 1), y_gen)
elnet_pred = elnet_mod.predict(rng.reshape(-1,1))

# plotting 
plt.figure(figsize=(10, 8));
plt.scatter(rng, y_gen);
plt.plot(rng, elnet_pred);
print("Coefficent Estimation: ", elnet_mod.coef_)<jupyter_output>Coefficent Estimation:  [0.4584509]
{'fraction_non_alphanumeric': 0.14385964912280702, 'fraction_numerical': 0.031189083820662766, 'mean_word_length': 7.2243589743589745, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '338', 'license': 'MIT', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1046, 'n_words': 183, 'path': 'bonston_housing_project/Regularized Regression.ipynb', 'repo_name': 'taareek/machine_learning', 'size': '2565'}