-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglm.py
153 lines (127 loc) · 4.29 KB
/
glm.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
150
151
import pickle
import random
from models.GeneralizedLinearRegressionModel import *
import pandas as pd
from models.SimpleGeneralizedLinearRegressionModel import *
def linear_prediction_function(xs: [float], b_params: [float]):
return sum([(bn * (xs[i - 1] ** i) if i != 0 else bn) for (i, bn) in enumerate(b_params)])
def first_derivative_of_linear_prediction_function(respect_to_bn: int, xs: List[float]):
return (xs[respect_to_bn - 1] ** respect_to_bn) if respect_to_bn != 0 else 1
def first_derivative_of_linear_prediction_function_as_exponent_LP(respect_to_bn: int, xs: List[float], b_params: [float]):
return first_derivative_of_linear_prediction_function(
respect_to_bn=respect_to_bn, xs=xs
) * math.exp(linear_prediction_function(
xs=xs, b_params=b_params
))
def custom_x_y_dataset_data_test():
# %% load data
dataset = pd.read_csv('datasets/custom_x_y_dataset.csv')
x_train_1 = list(dataset.iloc[:, 0].values)
# x_train_2 = [x ** 2 for x in x_train_1]
y_train = list(dataset.iloc[:, 1].values)
# %% define data
LRM = SimpleGeneralizedLinearRegressionModel(
start_b0=0,
start_b1=1
)
LRM.update_data(
training_input={
"x_train_1": x_train_1
},
training_output=y_train
)
# # %% test learning
loss_history = LRM.learn(
epochs=1000,
data_necessity_type=DataNecessityType.TRAINING,
epoch_history_save_interval=1
)
LRM.plot_loss_history(loss_history, model_description_str="_glm_custom_x_y_dataset_data_test_poisson_exp")
LRM.plot_performance(
x_data=x_train_1,
y_data=y_train,
b0=LRM.b0,
b1=LRM.b1,
name="performance_glm_custom_x_y_dataset_data_test_poisson_exp",
padding_interval=3
)
n = 0.5
LRM.plot_b0_dependency_from_loss(
r=float_range(LRM.b0 - n, LRM.b0 + n, 0.01),
name="b0_loss_dependency_derivative_glm_custom_x_y_dataset_data_test_poisson_exp"
)
def custom_x_y_dataset_data_test_2():
# %% load data
dataset = pd.read_csv('datasets/custom_x_y_dataset_2.csv')
x_train_1 = list(dataset.iloc[:, 0].values)
# x_train_2 = [x ** 2 for x in x_train_1]
y_train = list(dataset.iloc[:, 1].values)
# %% define data
LRM = SimpleGeneralizedLinearRegressionModel(
start_b0=0,
start_b1=1
)
LRM.update_data(
training_input={
"x_train_1": x_train_1
},
training_output=y_train
)
# # %% test learning
loss_history = LRM.learn(
epochs=1050,
data_necessity_type=DataNecessityType.TRAINING,
epoch_history_save_interval=1
)
LRM.plot_loss_history(loss_history, model_description_str="_glm_custom_x_y_dataset_data_test_2_poisson_exp")
LRM.plot_performance(
x_data=x_train_1,
y_data=y_train,
b0=LRM.b0,
b1=LRM.b1,
name="performance_glm_custom_x_y_dataset_data_test_2_poisson_exp",
padding_interval=3,
step_quality=0.01
)
n = 0.5
LRM.plot_b0_dependency_from_loss(
r=float_range(LRM.b0 - n, LRM.b0 + n, 0.01),
name="b0_loss_dependency_derivative_glm_custom_x_y_dataset_data_test_2_poisson_exp"
)
def sales_data():
# %% load data
dataset = pd.read_csv('datasets/Salary_Data.csv')
x_train_1 = list(dataset.iloc[:, 0].values)
# x_train_2 = [x ** 2 for x in x_train_1]
y_train = list(dataset.iloc[:, 1].values)
# %% define data
LRM = SimpleGeneralizedLinearRegressionModel(
start_b0=0,
start_b1=1
)
LRM.update_data(
training_input={
"x_train_1": x_train_1
},
training_output=y_train
)
# # %% test learning
loss_history = LRM.learn(
epochs=1000,
data_necessity_type=DataNecessityType.TRAINING,
epoch_history_save_interval=1
)
LRM.plot_loss_history(loss_history, model_description_str="_glm_sales_data_x_y_test_poisson_exp")
LRM.plot_performance(
x_data=x_train_1,
y_data=y_train,
b0=LRM.b0,
b1=LRM.b1,
name="performance_glm_sales_data_x_y_test_poisson_exp",
padding_interval=0,
step_quality=0.01
)
if __name__ == '__main__':
sales_data()
custom_x_y_dataset_data_test()
custom_x_y_dataset_data_test_2()