-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmultiple_models.py
More file actions
65 lines (45 loc) · 1.96 KB
/
multiple_models.py
File metadata and controls
65 lines (45 loc) · 1.96 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import pandas as pd
from sklearn import ensemble, svm
from sklearn.linear_model import LinearRegression
from ceteris_paribus.datasets import DATASETS_DIR
from ceteris_paribus.explainer import explain
from ceteris_paribus.plots.plots import plot
from ceteris_paribus.profiles import individual_variable_profile
from ceteris_paribus.select_data import select_sample
df = pd.read_csv(os.path.join(DATASETS_DIR, 'insurance.csv'))
df = df[['age', 'bmi', 'children', 'charges']]
x = df.drop(['charges'], inplace=False, axis=1)
y = df['charges']
var_names = list(x.columns)
x = x.values
y = y.values
def linear_regression_model():
# Create linear regression object
linear_model = LinearRegression()
# Train the model using the training set
linear_model.fit(x, y)
# model, data, labels, variable_names
return linear_model, x, y, var_names
def gradient_boosting_model():
gb_model = ensemble.GradientBoostingRegressor(n_estimators=1000, random_state=42)
gb_model.fit(x, y)
return gb_model, x, y, var_names
def supported_vector_machines_model():
svm_model = svm.SVR(C=0.01, gamma='scale')
svm_model.fit(x, y)
return svm_model, x, y, var_names
if __name__ == "__main__":
(linear_model, data, labels, variable_names) = linear_regression_model()
(gb_model, _, _, _) = gradient_boosting_model()
(svm_model, _, _, _) = supported_vector_machines_model()
explainer_linear = explain(linear_model, variable_names, data, y)
explainer_gb = explain(gb_model, variable_names, data, y)
explainer_svm = explain(svm_model, variable_names, data, y)
cp_profile = individual_variable_profile(explainer_linear, x[0], y[0])
plot(cp_profile, show_residuals=True)
sample_x, sample_y = select_sample(x, y, n=10)
cp2 = individual_variable_profile(explainer_gb, sample_x, y=sample_y)
cp3 = individual_variable_profile(explainer_gb, x[0], y[0])
plot(cp3, show_residuals=True)
plot(cp_profile, cp3, show_residuals=True)