-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregularization.py
More file actions
49 lines (34 loc) · 1.38 KB
/
regularization.py
File metadata and controls
49 lines (34 loc) · 1.38 KB
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
import pandas as pd
import sklearn
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.linear_model import Ridge
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
if __name__ == "__main__":
dataset = pd.read_csv('./data/whr2017.csv')
print(dataset.describe())
X = dataset[['gdp', 'family', 'lifexp', 'freedom' , 'corruption' , 'generosity', 'dystopia']]
y = dataset[['score']]
print(X.shape)
print(y.shape)
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.25)
modelLinear = LinearRegression().fit(X_train, y_train)
y_predict_linear = modelLinear.predict(X_test)
modelLasso = Lasso(alpha=0.02).fit(X_train, y_train)
y_predict_lasso = modelLasso.predict(X_test)
modelRidge = Ridge(alpha=1).fit(X_train, y_train)
y_predict_ridge = modelRidge.predict(X_test)
linear_loss = mean_squared_error(y_test, y_predict_linear)
print("Linear Loss:", linear_loss)
lasso_loss = mean_squared_error(y_test, y_predict_lasso)
print("Lasso Loss: ", lasso_loss)
ridge_loss = mean_squared_error(y_test, y_predict_ridge)
print("Ridge Loss: ", ridge_loss)
print("="*32)
print("Coef LASSO")
print(modelLasso.coef_)
print("="*32)
print("Coef RIDGE")
print(modelRidge.coef_)
#implementacion_lasso_ridge