-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
regression.py
47 lines (36 loc) · 1.17 KB
/
regression.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
"""Utilities for evaluating regression models."""
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import r2_score
def adjusted_r2(model, X, y):
"""
Calculate the adjusted R^2.
Parameters:
- model: Estimator object with a `predict()` method
- X: The values to use for prediction.
- y: The true values for scoring.
Returns:
The adjusted R^2 score.
"""
r2 = r2_score(y, model.predict(X))
n_obs, n_regressors = X.shape
adj_r2 = 1 - (1 - r2) * (n_obs - 1)/(n_obs - n_regressors - 1)
return adj_r2
def plot_residuals(y_test, preds):
"""
Plot residuals to evaluate regression.
Parameters:
- y_test: The true values for y
- preds: The predicted values for y
Returns:
Subplots of residual scatter plot and
residual KDE plot.
"""
residuals = y_test - preds
fig, axes = plt.subplots(1, 2, figsize=(15, 3))
axes[0].scatter(np.arange(residuals.shape[0]), residuals)
axes[0].set(xlabel='Observation', ylabel='Residual')
residuals.plot(kind='kde', ax=axes[1])
axes[1].set_xlabel('Residual')
plt.suptitle('Residuals')
return axes