-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
155 lines (109 loc) · 5.74 KB
/
utils.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
152
153
154
155
import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.inspection import PartialDependenceDisplay, permutation_importance
import shap
np.bool = np.bool_
np.int = np.int_
np.float = np.float_
def load_data_csv(filename, delimiter):
"""
Loading the dataset if the file exists.
"""
try:
if os.path.isfile(filename):
df = pd.read_csv(filename, delimiter=delimiter)
return df
except FileNotFoundError as e:
print("File not found", e)
def merge_and_process_data(reservations_file, vehicles_file):
# Read the reservations and vehicles files into DataFrames
df_reserve = load_data_csv(reservations_file, delimiter=',')
df_vehicles = load_data_csv(vehicles_file, delimiter=',')
# Merge the datasets on vehicle_id
data = pd.merge(df_vehicles, df_reserve, on='vehicle_id', how='outer')
# Group by vehicle_id and reservation_type, and count the number of bookings
reservation_counts = data.groupby(['vehicle_id', 'reservation_type']).size().reset_index(name='num_reservations')
# Merge the reservation counts with the original data based on 'vehicle_id' and 'reservation_type'
data = pd.merge(data, reservation_counts, on=['vehicle_id', 'reservation_type'], how='outer')
# Fill missing values with zeros
data['num_reservations'].fillna(0, inplace=True)
data['reservation_type'].fillna(0, inplace=True)
# Drop duplicates from the dataframe
data = data.drop_duplicates()
return data
def split_data(data, numerical_features, categorical_features, target_variable, random_state=42):
X = data[categorical_features + numerical_features]
y = data[target_variable]
x_train, x_test, y_train, y_test = train_test_split(X, y, random_state=random_state)
return x_train, x_test, y_train, y_test
def plot_partial_dependence(tree_pipeline, x_train, categorical_features, numerical_features, model_name):
fig, ax = plt.subplots(figsize=(12, 6))
ax.set_title(f"{model_name} (Categorical Features)")
tree_disp = PartialDependenceDisplay.from_estimator(tree_pipeline, x_train, categorical_features, ax=ax)
fig, ax = plt.subplots(figsize=(12, 6))
ax.set_title(f"{model_name} (Numerical Features)")
tree_disp = PartialDependenceDisplay.from_estimator(tree_pipeline, x_train, numerical_features, ax=ax)
plt.show()
def plot_feature_importances(tree_pipeline, model_name):
feature_names = tree_pipeline[:-1].get_feature_names_out()
mdi_importances = pd.Series(
tree_pipeline[-1].feature_importances_, index=feature_names
).sort_values(ascending=True)
ax = mdi_importances.plot.barh()
ax.set_title(f"{model_name} Feature Importances (MDI)")
ax.figure.tight_layout()
plt.show()
def plot_permutation_importances(tree_pipeline, x_train, y_train, x_test, y_test):
train_result = permutation_importance(
tree_pipeline, x_train, y_train, n_repeats=10, random_state=42, n_jobs=2
)
test_results = permutation_importance(
tree_pipeline, x_test, y_test, n_repeats=10, random_state=42, n_jobs=2
)
sorted_importances_idx = train_result.importances_mean.argsort()
train_importances = pd.DataFrame(
train_result.importances[sorted_importances_idx].T,
columns=x_train.columns[sorted_importances_idx],
)
test_importances = pd.DataFrame(
test_results.importances[sorted_importances_idx].T,
columns=x_train.columns[sorted_importances_idx],
)
for name, importances in zip(["train", "test"], [train_importances, test_importances]):
ax = importances.plot.box(vert=False, whis=10)
ax.set_title(f"Permutation Importances ({name} set)")
ax.set_xlabel("Decrease in performance")
ax.axvline(x=0, color="k", linestyle="--")
ax.figure.tight_layout()
plt.show()
def shap_analysis(tree_pipeline, x_train, x_test):
explainer = shap.TreeExplainer(tree_pipeline.named_steps["model"])
x_tr = tree_pipeline.named_steps['preprocessor'].transform(x_train)
shap_values_tr = explainer.shap_values(x_tr)
x_tes = tree_pipeline.named_steps['preprocessor'].transform(x_test)
shap_values_tes = explainer.shap_values(x_tes)
print("TREE SHAP ANALYSIS FOR TRAIN SET\n")
print('------------TREE SHAP Feature Importance Plot-------------------------------------')
shap.summary_plot(shap_values_tr, x_tr, feature_names=tree_pipeline[:-1].get_feature_names_out(),
plot_type='bar')
print('------------------TREE SHAP Summary Plot-------------------------------')
shap.summary_plot(shap_values_tr, x_tr, feature_names=tree_pipeline[:-1].get_feature_names_out())
print('-------------TREE SHAP Dependence Plots by top 5 rank------------------------------------')
for i in range(5):
shap.dependence_plot(f"rank({i})", shap_values_tr, x_tr,
feature_names=tree_pipeline[:-1].get_feature_names_out())
plt.show()
print("SHAP ANALYSIS FOR TEST SET\n")
print('------------TREE SHAP Feature Importance Plot-------------------------------------')
shap.summary_plot(shap_values_tes, x_tes, feature_names=tree_pipeline[:-1].get_feature_names_out(),
plot_type='bar')
print('------------TREE SHAP Summary Plot-------------------------------------')
shap.summary_plot(shap_values_tes, x_tes, feature_names=tree_pipeline[:-1].get_feature_names_out())
print('-------------TREE SHAP Dependence Plots by top 5 rank------------------------------------')
for i in range(5):
shap.dependence_plot(f"rank({i})", shap_values_tes, x_tes,
feature_names=tree_pipeline[:-1].get_feature_names_out())
plt.show()