-
Notifications
You must be signed in to change notification settings - Fork 0
/
regularized_linear_regression.py
executable file
·149 lines (120 loc) · 5.1 KB
/
regularized_linear_regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 1 21:48:35 2020
@author: daniel
"""
# https://www.datacamp.com/community/tutorials/tutorial-ridge-lasso-elastic-net
# https://www.r-bloggers.com/ridge-regression-and-the-lasso/
# https://www.pluralsight.com/guides/linear-lasso-and-ridge-regression-with-r
# https://towardsdatascience.com/ridge-and-lasso-regression-a-complete-guide-with-python-scikit-learn-e20e34bcbf0b
# Initialization
from sklearn.linear_model import ElasticNet
from sklearn.linear_model import Lasso
from yellowbrick.regressor import AlphaSelection
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import Ridge
from sklearn.linear_model import LinearRegression
import statsmodels.api as sm
import math
import statistics
import random
import pandas as pd
import numpy as np
def eval_results(y, y_pred):
SSE = sum((y_pred-y)**2)
RMSE = math.sqrt(SSE/len(y))
SST = sum((y-statistics.mean(y))**2)
R_square = 1 - (SSE / SST)
print("RMSE = {:.06f}, R_square = {:.06f}".format(RMSE, R_square))
random.seed(123)
data = pd.read_csv(
'/Users/daniel/Data-Science/Data/from_R_datasets/swiss/swiss.csv')
X = data.drop(["Fertility"], axis=1)
y = data["Fertility"]
train_idx = [31, 15, 14, 3, 42, 37, 45, 25, 26, 27,
5, 38, 28, 9, 29, 44, 8, 39, 7, 10, 34, 19, 4]
train_idx = [x - 1 for x in train_idx]
X_train = X.loc[train_idx]
y_train = y.loc[train_idx]
X_test = X.drop(train_idx, axis=0)
y_test = y.drop(train_idx, axis=0)
X_train_sm = sm.add_constant(X_train)
X_test_sm = sm.add_constant(X_test)
# 1. Linear Regression - OLS
print('\n--------------------------\n2. Linear Regression\n--------------------------')
OLS_model = sm.OLS(y_train, X_train_sm)
linear_reg_model_1 = OLS_model.fit()
print('\nstatsmodels')
print(linear_reg_model_1.params)
y_train_pred_linearreg_1 = linear_reg_model_1.predict(X_train_sm)
y_test_pred_linearreg_1 = linear_reg_model_1.predict(X_test_sm)
eval_results(y_train, y_train_pred_linearreg_1)
eval_results(y_test, y_test_pred_linearreg_1)
linear_reg_model_2 = LinearRegression()
linear_reg_model_2.fit(X_train, y_train)
print('\nscikit-learn')
print(linear_reg_model_2.intercept_, linear_reg_model_2.coef_)
y_train_pred_linearreg_2 = linear_reg_model_2.predict(X_train)
y_test_pred_linearreg_2 = linear_reg_model_2.predict(X_test)
eval_results(y_train, y_train_pred_linearreg_2)
eval_results(y_test, y_test_pred_linearreg_2)
# 2. Ridge Regression
print('\n--------------------------\n2. Ridge Regression\n--------------------------')
OLS_model = sm.OLS(y_train, X_train_sm)
# to tune alpha: https://stackoverflow.com/questions/41045752/using-statsmodel-estimations-with-scikit-learn-cross-validation-is-it-possible
# need to use some GridSearch to find out the best alpha
ridge_reg_model_1 = OLS_model.fit_regularized(L1_wt=0.0, alpha=0.008)
print('\nstatsmodels')
print(ridge_reg_model_1.params)
y_train_pred_ridgereg_1 = ridge_reg_model_1.predict(X_train_sm)
y_test_pred_ridgereg_1 = ridge_reg_model_1.predict(X_test_sm)
eval_results(y_train, y_train_pred_ridgereg_1)
eval_results(y_test, y_test_pred_ridgereg_1)
alphas = 10 ** np.linspace(start=1.3, stop=2, num=10000)
clf = RidgeCV(alphas=alphas).fit(X_train, y_train)
best_alpha = clf.alpha_
print('\nbest alpha (lambda) = {:f}'.format(best_alpha))
# https://www.scikit-yb.org/en/latest/api/regressor/alphas.html
alphas = 10 ** np.linspace(start=1.3, stop=2, num=10000)
model = RidgeCV(alphas=alphas)
visualizer = AlphaSelection(model)
visualizer.fit(X_train, y_train)
visualizer.show()
#
ridge_reg_model_2 = Ridge(alpha=best_alpha, tol=1e-12, random_state=123)
ridge_reg_model_2.fit(X_train, y_train)
print('\nscikit-learn')
print(ridge_reg_model_2.intercept_, ridge_reg_model_2.coef_)
y_train_pred_ridgereg_2 = ridge_reg_model_2.predict(X_train)
y_test_pred_ridgereg_2 = ridge_reg_model_2.predict(X_test)
eval_results(y_train, y_train_pred_ridgereg_2)
eval_results(y_test, y_test_pred_ridgereg_2)
##########################################################################################
# Not completed (below)
##########################################################################################
# 3. Lasso Regression
print('\n\n--------------------------\n3. Lasso Regression\n--------------------------\n')
OLS_model = sm.OLS(y_train, X_train_sm)
lasso_reg_model_1 = OLS_model.fit_regularized(L1_wt=1.0)
print(lasso_reg_model_1.params)
print('\nstatsmodels')
y_train_pred_lassoreg_1 = lasso_reg_model_1.predict(X_train_sm)
eval_results(y_train, y_train_pred_lassoreg_1)
y_test_pred_lassoreg_1 = lasso_reg_model_1.predict(X_test_sm)
eval_results(y_test, y_test_pred_lassoreg_1)
lasso_reg_model_2 = Lasso(alpha=1, normalize=False, max_iter=1e5)
lasso_reg_model_2.fit(X, y)
print('\nscikit-learn')
y_train_pred_lassoreg_2 = lasso_reg_model_2.predict(X_train)
eval_results(y_train, y_train_pred_lassoreg_2)
y_test_pred_lassoreg_2 = lasso_reg_model_2.predict(X_test)
eval_results(y_test, y_test_pred_lassoreg_2)
# 4. Elastic Net
elasticnet = ElasticNet(normalize=False)
elasticnet.fit(X, y)
y_pred = elasticnet.predict(X)
rss = sum((y_pred-y)**2)
print(rss)
print([elasticnet.intercept_])
print(elasticnet.coef_)