-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcustom_deep_regression.py
More file actions
127 lines (91 loc) · 3.74 KB
/
Copy pathcustom_deep_regression.py
File metadata and controls
127 lines (91 loc) · 3.74 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
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
import matplotlib.pyplot as plt
import os
import nnetsauce as ns
import numpy as np
import sklearn.metrics as skm2
import warnings
from sklearn.datasets import load_diabetes, fetch_california_housing
from sklearn.linear_model import LassoCV, RidgeCV, ElasticNetCV, HuberRegressor
from sklearn.model_selection import train_test_split
from time import time
print(f"\n ----- Running: {os.path.basename(__file__)}... ----- \n")
load_models = [LassoCV, RidgeCV, ElasticNetCV, HuberRegressor]
load_datasets = [load_diabetes(), fetch_california_housing()]
warnings.filterwarnings('ignore')
split_color = 'green'
split_color2 = 'orange'
local_color = 'gray'
def plot_func(x,
y,
y_u=None,
y_l=None,
pred=None,
shade_color="lightblue",
method_name="",
title=""):
fig = plt.figure()
plt.plot(x, y, 'k.', alpha=.3, markersize=10,
fillstyle='full', label=u'Test set observations')
if (y_u is not None) and (y_l is not None):
plt.fill(np.concatenate([x, x[::-1]]),
np.concatenate([y_u, y_l[::-1]]),
alpha=.3, fc=shade_color, ec='None',
label = method_name + ' Prediction interval')
if pred is not None:
plt.plot(x, pred, 'k--', lw=2, alpha=0.9,
label=u'Predicted value')
#plt.ylim([-2.5, 7])
plt.xlabel('$X$')
plt.ylabel('$Y$')
plt.legend(loc='upper right')
plt.title(title)
plt.show()
for data in load_datasets:
X = data.data
y= data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2, random_state = 13)
print("\n\n 1 - not conformal \n\n")
for model in load_models:
obj = model()
regr = ns.DeepRegressor(obj, n_layers=3, verbose=1, n_clusters=3, n_hidden_features=2)
start = time()
regr.fit(X_train, y_train)
print(f"\nElapsed: {time() - start} seconds\n")
print(f"{type(obj).__name__} test RMSE: {regr.score(X_test, y_test)} \n")
print("\n\n 2 - conformal \n\n")
print("\n\n 2 - 1 split conformal \n\n")
for model in load_models:
obj = model()
regr = ns.DeepRegressor(obj, n_layers=3,
verbose=1, n_clusters=2,
n_hidden_features=5,
)
start = time()
regr.fit(X_train, y_train)
print(f"\nElapsed: {time() - start} seconds\n")
preds = regr.predict(X_test, return_pi=True, level=95,
method="splitconformal")
#print(f"preds: {preds}")
coverage = np.mean((y_test >= preds.lower) & (y_test <= preds.upper))
print(f"test coverage: {coverage} \n")
plot_func(range(len(y_test))[0:30], y_test[0:30],
preds.upper[0:30], preds.lower[0:30],
preds.mean[0:30], method_name="Split Conformal")
# prediction interval average width
width = np.mean(preds.upper - preds.lower)
print(f"prediction interval average width: {width} \n")
print("\n\n 2 - 2 local conformal \n\n")
for model in load_models:
obj = model()
regr = ns.DeepRegressor(obj, n_layers=3,
verbose=1, n_clusters=2,
n_hidden_features=5,
)
start = time()
regr.fit(X_train, y_train)
print(f"\nElapsed: {time() - start} seconds\n")
preds = regr.predict(X_test, return_pi=True, level=95,
method="splitconformal")
#print(f"preds: {preds}")
coverage = np.mean((y_test >= preds.lower) & (y_test <= preds.upper))
print(f"test coverage: {coverage} \n")