-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults.py
252 lines (199 loc) · 7.05 KB
/
results.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.axes import Axes
from seaborn.palettes import _ColorPalette
from sklearn.metrics import (
mean_absolute_error,
mean_absolute_percentage_error,
mean_squared_error,
r2_score
)
import torch
from torch import Tensor
from rich.table import Table
from rich.console import Console
from typing import (
List,
Dict,
Optional,
Union
)
from .estimator import CustomRegressor, MLP
from .optimize import median_absolute_percentage_error
def compute_metrics(
model: CustomRegressor,
X: np.ndarray,
y: np.ndarray,
to_prices: bool=True
) -> Dict:
"""Description: Compute MSE, MAE, MAPE and R² for train and test sets."""
y_pred = model.predict(X)
if to_prices:
y_pred = np.exp(y_pred)
y = np.exp(y)
results = {}
for metric in (
mean_absolute_error,
mean_absolute_percentage_error,
median_absolute_percentage_error,
mean_squared_error,
r2_score
):
metric_name = metric.__name__
results[metric_name] = metric(y, y_pred)
return results
def make_regression_report(metrics: Dict, title: Optional[str]=None) -> Table:
"""Description. Make a rich table with the results of the regression models."""
if title is None:
title = "Regression report"
table = Table(title=title)
table.add_column("Model", width=30)
for col in ("MAE", "% MAPE", "%MDAPE", "MSE", "% R²"):
table.add_column(col, width=15)
for model, records in metrics.items():
mae = round(records["mean_absolute_error"], 2)
mape = round(100*records["mean_absolute_percentage_error"], 2)
mdape = round(100*records["median_absolute_percentage_error"], 2)
mse = round(records["mean_squared_error"], 2)
r2 = round(100*records["r2_score"], 2)
table.add_row(
model,
str(mae),
str(mape),
str(mdape),
f"{mse:,.2e}",
str(r2)
)
return table
def display_regression_report(results: Dict, title: Optional[str]=None) -> None:
"""Description. Display metrics of regression models in the console.
Args:
metrics (Dict): A dictionary with the metrics of each model.
title (Optional[str], optional): The title of the table. Defaults to None.
Returns:
Table: A rich table with the results of the regression models.
Example:
>>> results = {
... "model_1": {
... "mean_absolute_error": 0.1,
... "mean_absolute_percentage_error": 0.2,
... "mean_squared_error": 0.3,
... "r2_score": 0.4
... },
... "model_2": {
... "mean_absolute_error": 0.5,
... "mean_absolute_percentage_error": 0.6,
... "mean_squared_error": 0.7,
... "r2_score": 0.8
... }
... }
>>> make_regression_report(results)
+-----------+---------------+---------------+---------------+---------------+
| Model | MAE | % MAPE | MSE | % R² |
+-----------+---------------+---------------+---------------+---------------+
| model_1 | 0.1 | 20.0 | 3.00e-01 | 40.0 |
| model_2 | 0.5 | 60.0 | 7.00e-01 | 80.0 |
+-----------+---------------+---------------+---------------+---------------+
"""
table = make_regression_report(results, title=title)
console = Console()
console.print(table)
def get_predictions(
estimator: CustomRegressor,
X: np.ndarray,
y: np.ndarray,
target_var: str="l_valeur_fonciere"
) -> Dict:
"""Description. Get predictions and true values for a given estimator and dataset.
Args:
estimator (CustomRegressor): The estimator to use.
X (np.ndarray): The feature matrix.
y (np.ndarray): The target vector
target_var (str, optional): The target variable. Defaults to "l_valeur_fonciere".
Returns:
Dict: A dictionary containing the true and predicted values."""
y_pred = estimator.predict(X)
if target_var == "l_valeur_fonciere":
results = {
"l_valeur_fonciere": {
"y_true": y,
"y_pred": y_pred
},
"valeur_fonciere": {
"y_true": np.exp(y),
"y_pred": np.exp(y_pred)
}
}
elif target_var == "valeur_fonciere":
results = {
"valeur_fonciere": {
"y_true": y,
"y_pred": y_pred
}
}
else:
raise ValueError("Target variable must be either 'l_valeur_fonciere' or 'valeur_fonciere'.")
return results
def predict_mlp(model: MLP, X: Tensor, device: torch.device, to_prices: bool=True) -> np.ndarray:
"""Predicts the target values using a trained MLP.
Args:
model: the MLP model
dataloader: the dataloader for the dataset
device: the device on which the model is located
to_prices: if True, the predicted values are exponentiated
Returns:
A numpy array containing the predicted values.
"""
model.eval()
with torch.no_grad():
X = X.to(device)
y_pred = model(X).cpu().numpy().reshape(-1)
if to_prices:
y_pred = np.exp(y_pred)
return y_pred
def absolute_percentage_error(true: float, pred: float, return_pct: bool=True) -> float:
"""Description. Compute the absolute percentage error."""
ape = np.abs((true - pred) / pred)
if return_pct:
ape = 100 * ape
return ape
def plot_predictions(
y_true: np.ndarray,
y_pred: np.ndarray,
add_line: bool=True,
title: Optional[str]=None,
ax: Optional[Axes]=None
):
"""Description. Plot observed vs actual values.
Args:
y_true (np.ndarray): The true values.
y_pred (np.ndarray): The predicted values.
add_line (bool, optional): Whether to add a line y=x. Defaults to True.
title (Optional[str], optional): The title of the plot. Defaults to None.
ax (Optional[Axes], optional): The axes to plot on. Defaults to None."""
if ax is None:
fig, ax = plt.subplots(figsize=(10, 5))
mape = 100*mean_absolute_percentage_error(y_true, y_pred)
tmp = pd.DataFrame(data={"obs": y_true, "pred": y_pred})
tmp["ape"] = tmp.apply(lambda row: absolute_percentage_error(row.obs, row.pred), axis=1)
min_x, max_x = tmp.obs.min(), tmp.obs.max()
sns.scatterplot(
x="obs",
y="pred",
data=tmp,
hue="ape",
palette="Reds",
ax=ax
)
if add_line:
sns.lineplot(
x=[min_x, max_x],
y=[min_x, max_x],
color="black",
linestyle="dotted",
ax=ax)
ax.legend(loc="best", title="Absolute error (%)")
if title is not None:
ax.set_title(f"{title} - MAPE: {mape:.2f}%")