|
4 | 4 | '''
|
5 | 5 | import platform
|
6 | 6 |
|
7 |
| -from sklearn.metrics import roc_curve, confusion_matrix, precision_score, accuracy_score, recall_score, f1_score, make_scorer |
| 7 | +from sklearn.metrics import roc_curve, confusion_matrix, precision_score, accuracy_score, recall_score, f1_score, make_scorer, mean_absolute_error, mean_squared_error, r2_score, mean_squared_log_error |
8 | 8 | from sklearn.model_selection import KFold, cross_val_score
|
9 | 9 | import numpy as np
|
10 | 10 | import pandas as pd
|
@@ -232,6 +232,55 @@ def get_classification_report(y_train=None, prediction=None, show_roc_plot=True,
|
232 | 232 | plt.savefig("roc_plot.png")
|
233 | 233 |
|
234 | 234 |
|
| 235 | +def get_regression_report(y_true=None, prediction=None, show_r2_plot=True, save_plot=False): |
| 236 | + ''' |
| 237 | + Generates performance report for a regression problem. |
| 238 | +
|
| 239 | + Parameters: |
| 240 | + ------------------ |
| 241 | + y_true: Array, series, list. |
| 242 | +
|
| 243 | + The truth/ground value from the train data set. |
| 244 | + |
| 245 | + prediction: Array, series, list. |
| 246 | +
|
| 247 | + The predicted value by a trained model. |
| 248 | +
|
| 249 | + show_r2_plot: Bool, default True. |
| 250 | +
|
| 251 | + Show the r-squared curve. |
| 252 | +
|
| 253 | + save_plot: Bool, default True. |
| 254 | +
|
| 255 | + Save the plot to the current working directory. |
| 256 | +
|
| 257 | + ''' |
| 258 | + mae = mean_absolute_error(y_true, prediction) |
| 259 | + mse = mean_squared_error(y_true, prediction) |
| 260 | + msle = precision_score(y_true, prediction) |
| 261 | + r2 = r2_score(y_true, prediction) |
| 262 | + |
| 263 | + print("Mean Absolute Error: ", round(mae, 5)) |
| 264 | + print("Mean Squared Error: ", round(mse, 5)) |
| 265 | + print("Mean Squared Log Error: ", round(msle, 5)) |
| 266 | + print("R-squared Error: ", round(r2, 5)) |
| 267 | + print("*" * 100) |
| 268 | + |
| 269 | + if show_r2_plot: |
| 270 | + plt.scatter(y_true,prediction) |
| 271 | + plt.xlabel('Truth values') |
| 272 | + plt.ylabel('Predicted values') |
| 273 | + plt.plot(np.unique(y_true), np.poly1d(np.polyfit(y_true, y_true, 1))(np.unique(y_true))) |
| 274 | + plt.text(0.7, 0.2, 'R-squared = %0.2f' % r2) |
| 275 | + plt.show() |
| 276 | + |
| 277 | + if save_plot: |
| 278 | + plt.savefig("r2_plot.png") |
| 279 | + |
| 280 | + |
| 281 | + |
| 282 | + |
| 283 | + |
235 | 284 | def compare_model(models_list=None, x_train=None, y_train=None, scoring_metric=None, scoring_cv=3, silenced=True, plot=True):
|
236 | 285 | """
|
237 | 286 | Train multiple user-defined model and display report based on defined metric. Enables user to pick the best base model for a problem.
|
|
0 commit comments