forked from jjfiv/cs451-practicals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.py
288 lines (254 loc) · 8.07 KB
/
shared.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# This 'shared.py' will be imported from in our practicals going forward (sometimes, anyway).
import os
import urllib.request
import sys
import zipfile
from typing import List, Dict, Optional, Any
from sklearn.base import ClassifierMixin, RegressorMixin
from sklearn.utils import resample
from sklearn.metrics import accuracy_score, mean_squared_error, roc_auc_score, r2_score
import random
def bootstrap_auc(
f: Any, # sklearn classifier
X, # numpy array
y, # numpy array
num_samples: int = 100,
random_state: int = random.randint(0, 2 ** 32 - 1),
truth_label: int = 1,
) -> List[float]:
"""
Take the classifier ``f``, and compute it's bootstrapped AUC over the dataset ``X``,``y``.
Generate ``num_samples`` samples; and seed the resampler with ``random_state``.
"""
(N, D) = X.shape
dist: List[float] = []
if hasattr(f, "decision_function"):
y_scores = f.decision_function(X)
# type:ignore (predict not on ClassifierMixin)
else:
y_scores = f.predict_proba(X)[:, truth_label].ravel()
# do the bootstrap:
for trial in range(num_samples):
sample_pred, sample_truth = resample(
y_scores, y, random_state=trial + random_state
) # type:ignore
score = roc_auc_score(y_true=sample_truth, y_score=sample_pred)
dist.append(score)
return dist
def bootstrap_measure(
f,
X, # numpy array
y, # numpy array
num_samples: int = 100,
random_state: int = random.randint(0, 2 ** 32 - 1),
predict=lambda m, X: m.predict(X),
measure=lambda y_true, y_pred: accuracy_score(y_true, y_pred),
) -> List[float]:
"""
Take the classifier ``f``, and compute it's bootstrapped accuracy over the dataset ``X``,``y``.
Generate ``num_samples`` samples; and seed the resampler with ``random_state``.
"""
dist: List[float] = []
y_pred = predict(f, X)
# do the bootstrap:
for trial in range(num_samples):
sample_pred, sample_truth = resample(
y_pred, y, random_state=trial + random_state
) # type:ignore
score = measure(y_true=sample_truth, y_pred=sample_pred)
dist.append(score)
return dist
def bootstrap_regressor(
f: RegressorMixin,
X, # numpy array
y, # numpy array
num_samples: int = 100,
random_state: int = random.randint(0, 2 ** 32 - 1),
) -> List[float]:
"""
Take the regressor f, and compute it's bootstrapped accuracy over the dataset `X`,`y`.
Generate `num_samples` samples; and seed the resampler with `random_state`.
"""
dist: List[float] = []
y_pred = f.predict(X) # type:ignore
# do the bootstrap:
for trial in range(num_samples):
sample_pred, sample_truth = resample(
y_pred, y, random_state=trial + random_state
) # type:ignore
score = mean_squared_error(y_true=sample_truth, y_pred=sample_pred) # type:ignore
dist.append(score)
return dist
def bootstrap_r2(
f: Any,
X: Any,
y: Any,
num_samples: int = 100,
random_state: int = random.randint(0, 2 ** 32 - 1),
) -> List[float]:
def regress_eval(y_true, y_pred) -> float:
return r2_score(y_true=y_true, y_pred=y_pred)
return bootstrap_measure(
f,
X,
y,
num_samples=num_samples,
random_state=random_state,
measure=regress_eval,
)
def bootstrap_accuracy(
f: ClassifierMixin,
X, # numpy array
y, # numpy array
num_samples: int = 100,
random_state: int = random.randint(0, 2 ** 32 - 1),
) -> List[float]:
"""
Take the classifier ``f``, and compute it's bootstrapped accuracy over the dataset ``X``,``y``.
Generate ``num_samples`` samples; and seed the resampler with ``random_state``.
"""
return bootstrap_measure(
f,
X,
y,
num_samples=num_samples,
random_state=random_state,
predict=lambda f, X: f.predict(X),
measure=accuracy_score,
)
def TODO(for_what: str) -> None:
"""Because crashing should be legible."""
print("=" * 80)
print("TODO:", for_what, file=sys.stderr)
print("=" * 80)
sys.exit(-1)
def __create_data_directory():
os.makedirs("data", exist_ok=True)
assert os.path.exists("data") and os.path.isdir("data")
def __download_file(url: str, path: str):
# empty data files were mis-downloaded...
if os.path.exists(path) and os.path.getsize(path) > 0:
# don't download multiple times.
return
# try connecting before creating output file...
with urllib.request.urlopen(url) as f:
# create output file and download the rest.
with open(path, "wb") as out:
out.write(f.read())
def dataset_local_path(name: str) -> str:
__create_data_directory()
destination = os.path.join("data", name)
if name == "AirQualityUCI.csv":
zip_path = os.path.join("data", "AirQualityUCI.zip")
__download_file(
"http://archive.ics.uci.edu/ml/machine-learning-databases/00360/AirQualityUCI.zip",
zip_path,
)
with zipfile.ZipFile(zip_path) as zf:
zf.extract(name, "data")
return destination
elif name == "clickbait.csv.gz":
__download_file(
"https://drive.google.com/uc?export=download&id=10NKt-ct-TaP_cXwVsZpSH_XrdVCXXaqY",
destination,
)
elif name == "forest-fires.csv":
__download_file(
"http://archive.ics.uci.edu/ml/machine-learning-databases/forest-fires/forestfires.csv",
destination,
)
elif name == "poetry_id.jsonl":
__download_file(
"http://ciir.cs.umass.edu/downloads/poetry/id_datasets.jsonl", destination
)
elif name in [
"lit-wiki-2020.jsonl.gz",
"tiny-wiki.jsonl.gz",
"tiny-wiki-labels.jsonl",
]:
__download_file("http://static.jjfoley.me/{}".format(name), destination)
else:
raise ValueError("No such dataset... {}; should you git pull?".format(name))
assert os.path.exists(destination)
return destination
def simple_violins(
data: Dict[str, List[float]],
title: Optional[str] = None,
xlabel: Optional[str] = None,
ylabel: Optional[str] = None,
show: bool = True,
save: Optional[str] = None,
) -> Any:
""" Create a simple set of named boxplots. """
import matplotlib.pyplot as plt
box_names = []
box_dists = []
for (k, v) in data.items():
box_names.append(k)
box_dists.append(v)
plt.violinplot(box_dists)
plt.xticks(
rotation=30,
horizontalalignment="right",
ticks=range(1, len(box_names) + 1),
labels=box_names,
)
if title:
plt.title(title)
if xlabel:
plt.xlabel(xlabel)
if ylabel:
plt.ylabel(ylabel)
plt.tight_layout()
if save:
plt.savefig(save)
if show:
plt.show()
return plt
def simple_boxplot(
data: Dict[str, List[float]],
title: Optional[str] = None,
xlabel: Optional[str] = None,
ylabel: Optional[str] = None,
show: bool = True,
save: Optional[str] = None,
) -> Any:
""" Create a simple set of named boxplots. """
import matplotlib.pyplot as plt
box_names = []
box_dists = []
for (k, v) in data.items():
box_names.append(k)
box_dists.append(v)
plt.boxplot(box_dists)
plt.xticks(
rotation=30,
horizontalalignment="right",
ticks=range(1, len(box_names) + 1),
labels=box_names,
)
if title:
plt.title(title)
if xlabel:
plt.xlabel(xlabel)
if ylabel:
plt.ylabel(ylabel)
plt.tight_layout()
if save:
plt.savefig(save)
if show:
plt.show()
return plt
# TESTS:
def test_download_poetry():
import json
lpath = dataset_local_path("poetry_id.jsonl")
with open(lpath) as fp:
first = json.loads(next(fp))
assert first["book"] == "aceptadaoficialmente00gubirich"
def test_download_wiki():
import json
lpath = dataset_local_path("tiny-wiki-labels.jsonl")
with open(lpath) as fp:
first = json.loads(next(fp))
print(first)