-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRdata_model.py
82 lines (62 loc) · 2.49 KB
/
Rdata_model.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
import pandas as pd
import pickle
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn.linear_model import Ridge
from sklearn import linear_model
# save trained model as a pkl file, pass FULL desired path to file
def save_model_pkl(model, name):
model_pkl_file = str(name)
with open(model_pkl_file, 'wb') as file:
pickle.dump(model, file)
# not necessary but optional visualization of predictions as dataframe
def show_pkl_as_df(filename):
with open(filename, 'rb') as f:
data = pickle.load(f)
df_data = pd.DataFrame(data)
return df_data
# model evaluation
def model_eval(y_test, predictions):
squared_error_val = mean_squared_error(y_test, predictions)
absolute_error_val = mean_absolute_error(y_test, predictions)
squared_error = "{:.2%}".format(squared_error_val)
absolute_error = "{:.2%}".format(absolute_error_val)
print(
'mean_squared_error : ', squared_error)
print(
'mean_absolute_error : ', absolute_error)
# create regression model
def mult_regression_model(x_train, y_train, x_test, y_test):
model = LinearRegression()
#print("x_train shape:", x_train.shape)
#print("x_test shape:", x_test.shape)
#print("\ny_train shape:", y_train.shape)
#print("y_test shape:", y_test.shape)
#fit
model = model.fit(x_train, y_train)
# Use the model for prediction on the selected features of the test set
predictions = model.predict(x_test)
#print("\ny_test shape:", y_test.shape)
#print("predictions shape:", predictions.shape)
return predictions
def ridge_reg(x_train, y_train, x_test, y_test):
rdg = Ridge(alpha = 0.5)
rdg = rdg.fit(x_train, y_train)
rdg_pred = rdg.predict(x_test)
score = "{:.2%}".format(rdg.score(x_test,y_test))
print('RGD score:',score)
model_eval(y_test,rdg_pred)
return rdg_pred
def lasso_reg(x_train, y_train, x_test, y_test):
# Build lasso model
lasso_model = linear_model.Lasso(alpha=0.01)
lasso_model = lasso_model.fit(x_train, y_train)
# predict on test data and return predictions
lasso_prediction = lasso_model.predict(x_test)
score = "{:.2%}".format(lasso_model.score(x_test,y_test))
print('Lasso score:',score)
# evaluate and print results
model_eval(y_test, lasso_prediction)
return lasso_prediction