<jupyter_start><jupyter_text>Regularization<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import scale
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import LassoCV
import warnings
warnings.filterwarnings("ignore")
from sklearn.cross_validation import train_test_split 
data_credit = pd.read_csv("C:\\Users\\apoghosyan\\Desktop\\YSU_ML\\ISLR_Data\\Hitters.csv")
data_credit = data_credit.dropna()
print(data_credit.shape)
print(data_credit.columns)
data_credit = data_credit.drop(['Unnamed: 0'], axis = 1)
print(data_credit.columns)
data_credit.head(5)
cat_list = ["League","Division","NewLeague"]
df_dummies= pd.get_dummies(data_credit[cat_list], drop_first = True)
print(df_dummies.head())
add = pd.concat([data_credit, df_dummies], axis  = 1)
add.drop(cat_list, inplace = True, axis = 1)
print(add.head())


X = add.drop("Salary", axis = 1)
names = X.columns
y = add.Salary<jupyter_output>League_N  Division_W  NewLeague_N
1         1           1            1
2         0           1            0
3         1           0            1
4         1           0            1
5         0           1            0
   AtBat  Hits  HmRun  Runs  RBI  Walks  Years  CAtBat  CHits  CHmRun  CRuns  \
1    315    81      7    24   38     39     14    3449    835      69    321   
2    479   130     18    66   72     76      3    1624    457      63    224   
3    496   141     20    65   78     37     11    5628   1575     225    828   
4    321    87     10    39   42     30      2     396    101      12     48   
5    594   169      4    74   51     35     11    4408   1133      19    501   

   CRBI  CWalks  PutOuts  Assists  Errors  Salary  League_N  Division_W  \
1   414     375      632       43      10   475.0         1           1   
2   266     263      880       82      14   480.0         0           1   
3   838     354      200       11       3   500.0         1           0 [...]<jupyter_text>Ridge Regression<jupyter_code>names
ridge = Ridge()
ridge.set_params(alpha = 0)
ridge.fit(scale(X),y)
print("Ridge Coefficients -> ", ridge.coef_)
coef_list = ridge.coef_ 
print(np.sum(coef_list**2))
print("score->", ridge.score(scale(X),y))

lin = LinearRegression()
lin.fit(scale(X),y)
print("Linear Regression Coefficients ->", lin.coef_)
print(np.sum(coef_list**2))
print(lin.score(scale(X),y))
ridge = Ridge()
ridge.set_params(alpha = 0.400170056022409)
ridge.fit(scale(X),y)
print(ridge.coef_)
coef_list = ridge.coef_ 
print(np.sum(coef_list**2))
print("score ->", ridge.score(scale(X),y))
ridge = Ridge()
ridge.set_params(alpha = 100)
ridge.fit(scale(X),y)
print(ridge.coef_)
coef_list = ridge.coef_ 
print(np.sum(coef_list**2))
print("score -> ", ridge.score(scale(X),y))
ridge = Ridge()
ridge.set_params(alpha = 1000000)
ridge.fit(scale(X),y)
print(ridge.coef_)
coef_list = ridge.coef_ 
print(np.sum(coef_list**2))
print("score ->", ridge.score(scale(X),y))
alphas = np.linspace(0,200,1000)
ridge = Ridge()
coef_list = []
for a in alphas:
    ridge.set_params(alpha = a)
    ridge.fit(scale(X),y)
    coef_list.append(ridge.coef_)
coef_list

plt.figure(figsize = (15,12))
plt.plot(alphas, coef_list)
plt.legend(names)
plt.show()<jupyter_output><empty_output><jupyter_text>Lasso<jupyter_code>alphas = np.linspace(0.0001,200,1000)
lasso = Lasso( max_iter=100000)
coef_list = []
for a in alphas:
    lasso.set_params(alpha = a)
    lasso.fit(scale(X),y)
    coef_list.append(lasso.coef_)
coef_list

plt.figure(figsize = (15,12))
plt.plot(alphas, coef_list)
plt.legend(names)
plt.show()<jupyter_output><empty_output><jupyter_text>Ridge CV<jupyter_code>alphas = np.linspace(0.00001,500,2500)
ridgecv = RidgeCV(alphas = alphas, scoring = 'neg_mean_squared_error', cv = 10)
ridgecv.fit(scale(X),y)
ridgecv.alpha_
ridgecv.coef_
ridgecv.score(scale(X),y)
pd.Series(ridgecv.coef_.flatten(), index = names)
plt.figure(figsize = (15,8))
plt.plot(ridgecv.predict(scale(X)),color = "red")
plt.xlim(0,100)
plt.plot(y)<jupyter_output><empty_output><jupyter_text>Lasso CV<jupyter_code>alphas = np.linspace(0.001,100,2500)
lassocv = LassoCV(alphas = alphas, cv = 10, max_iter = 100000, verbose = True)
lassocv.fit(scale(X),y)
lassocv.alpha_
lassocv.coef_
lassocv.score(scale(X),y)
pd.Series(lassocv.coef_.flatten(), index = names)
plt.figure(figsize = (15,8))
plt.plot(lassocv.predict(scale(X)),color = "red")
plt.xlim(0,100)
plt.plot(y)
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.2, random_state = 42)  
X_train.shape
X_test.shape
X.shape
X_test.shape[0]/X.shape[0]<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup