-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
RocCurveDisplay.ts
499 lines (405 loc) · 14.8 KB
/
RocCurveDisplay.ts
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* eslint-disable */
/* NOTE: This file is auto-generated. Do not edit it directly. */
import crypto from 'node:crypto'
import { PythonBridge, NDArray, ArrayLike, SparseMatrix } from '@/sklearn/types'
/**
ROC Curve visualization.
It is recommend to use [`from\_estimator`](#sklearn.metrics.RocCurveDisplay.from_estimator "sklearn.metrics.RocCurveDisplay.from_estimator") or [`from\_predictions`](#sklearn.metrics.RocCurveDisplay.from_predictions "sklearn.metrics.RocCurveDisplay.from_predictions") to create a [`RocCurveDisplay`](#sklearn.metrics.RocCurveDisplay "sklearn.metrics.RocCurveDisplay"). All parameters are stored as attributes.
Read more in the [User Guide](../../visualizations.html#visualizations).
[Python Reference](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.RocCurveDisplay.html)
*/
export class RocCurveDisplay {
id: string
opts: any
_py: PythonBridge
_isInitialized: boolean = false
_isDisposed: boolean = false
constructor(opts?: {
/**
False positive rate.
*/
fpr?: NDArray
/**
True positive rate.
*/
tpr?: NDArray
/**
Area under ROC curve. If `undefined`, the roc\_auc score is not shown.
*/
roc_auc?: number
/**
Name of estimator. If `undefined`, the estimator name is not shown.
*/
estimator_name?: string
/**
The class considered as the positive class when computing the roc auc metrics. By default, `estimators.classes\_\[1\]` is considered as the positive class.
*/
pos_label?: number | boolean | string
}) {
this.id = `RocCurveDisplay${crypto.randomUUID().split('-')[0]}`
this.opts = opts || {}
}
get py(): PythonBridge {
return this._py
}
set py(pythonBridge: PythonBridge) {
this._py = pythonBridge
}
/**
Initializes the underlying Python resources.
This instance is not usable until the `Promise` returned by `init()` resolves.
*/
async init(py: PythonBridge): Promise<void> {
if (this._isDisposed) {
throw new Error('This RocCurveDisplay instance has already been disposed')
}
if (this._isInitialized) {
return
}
if (!py) {
throw new Error('RocCurveDisplay.init requires a PythonBridge instance')
}
this._py = py
await this._py.ex`
import numpy as np
from sklearn.metrics import RocCurveDisplay
try: bridgeRocCurveDisplay
except NameError: bridgeRocCurveDisplay = {}
`
// set up constructor params
await this._py.ex`ctor_RocCurveDisplay = {'fpr': np.array(${
this.opts['fpr'] ?? undefined
}) if ${this.opts['fpr'] !== undefined} else None, 'tpr': np.array(${
this.opts['tpr'] ?? undefined
}) if ${this.opts['tpr'] !== undefined} else None, 'roc_auc': ${
this.opts['roc_auc'] ?? undefined
}, 'estimator_name': ${
this.opts['estimator_name'] ?? undefined
}, 'pos_label': ${this.opts['pos_label'] ?? undefined}}
ctor_RocCurveDisplay = {k: v for k, v in ctor_RocCurveDisplay.items() if v is not None}`
await this._py
.ex`bridgeRocCurveDisplay[${this.id}] = RocCurveDisplay(**ctor_RocCurveDisplay)`
this._isInitialized = true
}
/**
Disposes of the underlying Python resources.
Once `dispose()` is called, the instance is no longer usable.
*/
async dispose() {
if (this._isDisposed) {
return
}
if (!this._isInitialized) {
return
}
await this._py.ex`del bridgeRocCurveDisplay[${this.id}]`
this._isDisposed = true
}
/**
Create a ROC Curve display from an estimator.
*/
async from_estimator(opts: {
/**
Fitted classifier or a fitted [`Pipeline`](sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") in which the last estimator is a classifier.
*/
estimator?: any
/**
Input values.
*/
X?: ArrayLike | SparseMatrix[]
/**
Target values.
*/
y?: ArrayLike
/**
Sample weights.
*/
sample_weight?: ArrayLike
/**
Whether to drop some suboptimal thresholds which would not appear on a plotted ROC curve. This is useful in order to create lighter ROC curves.
@defaultValue `true`
*/
drop_intermediate?: boolean
/**
Specifies whether to use [predict\_proba](../../glossary.html#term-predict_proba) or [decision\_function](../../glossary.html#term-decision_function) as the target response. If set to ‘auto’, [predict\_proba](../../glossary.html#term-predict_proba) is tried first and if it does not exist [decision\_function](../../glossary.html#term-decision_function) is tried next.
*/
response_method?: 'decision_function' | 'auto’} default=’auto'
/**
The class considered as the positive class when computing the roc auc metrics. By default, `estimators.classes\_\[1\]` is considered as the positive class.
*/
pos_label?: number | boolean | string
/**
Name of ROC Curve for labeling. If `undefined`, use the name of the estimator.
*/
name?: string
/**
Axes object to plot on. If `undefined`, a new figure and axes is created.
*/
ax?: any
/**
Whether to plot the chance level.
@defaultValue `false`
*/
plot_chance_level?: boolean
/**
Keyword arguments to be passed to matplotlib’s `plot` for rendering the chance level line.
*/
chance_level_kw?: any
/**
Keyword arguments to be passed to matplotlib’s `plot`.
*/
kwargs?: any
}): Promise<any> {
if (this._isDisposed) {
throw new Error('This RocCurveDisplay instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'RocCurveDisplay must call init() before from_estimator()'
)
}
// set up method params
await this._py.ex`pms_RocCurveDisplay_from_estimator = {'estimator': ${
opts['estimator'] ?? undefined
}, 'X': np.array(${opts['X'] ?? undefined}) if ${
opts['X'] !== undefined
} else None, 'y': np.array(${opts['y'] ?? undefined}) if ${
opts['y'] !== undefined
} else None, 'sample_weight': np.array(${
opts['sample_weight'] ?? undefined
}) if ${
opts['sample_weight'] !== undefined
} else None, 'drop_intermediate': ${
opts['drop_intermediate'] ?? undefined
}, 'response_method': ${
opts['response_method'] ?? undefined
}, 'pos_label': ${opts['pos_label'] ?? undefined}, 'name': ${
opts['name'] ?? undefined
}, 'ax': ${opts['ax'] ?? undefined}, 'plot_chance_level': ${
opts['plot_chance_level'] ?? undefined
}, 'chance_level_kw': ${opts['chance_level_kw'] ?? undefined}, 'kwargs': ${
opts['kwargs'] ?? undefined
}}
pms_RocCurveDisplay_from_estimator = {k: v for k, v in pms_RocCurveDisplay_from_estimator.items() if v is not None}`
// invoke method
await this._py
.ex`res_RocCurveDisplay_from_estimator = bridgeRocCurveDisplay[${this.id}].from_estimator(**pms_RocCurveDisplay_from_estimator)`
// convert the result from python to node.js
return this
._py`res_RocCurveDisplay_from_estimator.tolist() if hasattr(res_RocCurveDisplay_from_estimator, 'tolist') else res_RocCurveDisplay_from_estimator`
}
/**
Plot ROC curve given the true and predicted values.
Read more in the [User Guide](../../visualizations.html#visualizations).
*/
async from_predictions(opts: {
/**
True labels.
*/
y_true?: ArrayLike
/**
Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision\_function” on some classifiers).
*/
y_pred?: ArrayLike
/**
Sample weights.
*/
sample_weight?: ArrayLike
/**
Whether to drop some suboptimal thresholds which would not appear on a plotted ROC curve. This is useful in order to create lighter ROC curves.
@defaultValue `true`
*/
drop_intermediate?: boolean
/**
The label of the positive class. When `pos\_label=None`, if `y\_true` is in {-1, 1} or {0, 1}, `pos\_label` is set to 1, otherwise an error will be raised.
*/
pos_label?: number | boolean | string
/**
Name of ROC curve for labeling. If `undefined`, name will be set to `"Classifier"`.
*/
name?: string
/**
Axes object to plot on. If `undefined`, a new figure and axes is created.
*/
ax?: any
/**
Whether to plot the chance level.
@defaultValue `false`
*/
plot_chance_level?: boolean
/**
Keyword arguments to be passed to matplotlib’s `plot` for rendering the chance level line.
*/
chance_level_kw?: any
/**
Additional keywords arguments passed to matplotlib `plot` function.
*/
kwargs?: any
}): Promise<any> {
if (this._isDisposed) {
throw new Error('This RocCurveDisplay instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'RocCurveDisplay must call init() before from_predictions()'
)
}
// set up method params
await this._py
.ex`pms_RocCurveDisplay_from_predictions = {'y_true': np.array(${
opts['y_true'] ?? undefined
}) if ${opts['y_true'] !== undefined} else None, 'y_pred': np.array(${
opts['y_pred'] ?? undefined
}) if ${
opts['y_pred'] !== undefined
} else None, 'sample_weight': np.array(${
opts['sample_weight'] ?? undefined
}) if ${
opts['sample_weight'] !== undefined
} else None, 'drop_intermediate': ${
opts['drop_intermediate'] ?? undefined
}, 'pos_label': ${opts['pos_label'] ?? undefined}, 'name': ${
opts['name'] ?? undefined
}, 'ax': ${opts['ax'] ?? undefined}, 'plot_chance_level': ${
opts['plot_chance_level'] ?? undefined
}, 'chance_level_kw': ${opts['chance_level_kw'] ?? undefined}, 'kwargs': ${
opts['kwargs'] ?? undefined
}}
pms_RocCurveDisplay_from_predictions = {k: v for k, v in pms_RocCurveDisplay_from_predictions.items() if v is not None}`
// invoke method
await this._py
.ex`res_RocCurveDisplay_from_predictions = bridgeRocCurveDisplay[${this.id}].from_predictions(**pms_RocCurveDisplay_from_predictions)`
// convert the result from python to node.js
return this
._py`res_RocCurveDisplay_from_predictions.tolist() if hasattr(res_RocCurveDisplay_from_predictions, 'tolist') else res_RocCurveDisplay_from_predictions`
}
/**
Plot visualization.
Extra keyword arguments will be passed to matplotlib’s `plot`.
*/
async plot(opts: {
/**
Axes object to plot on. If `undefined`, a new figure and axes is created.
*/
ax?: any
/**
Name of ROC Curve for labeling. If `undefined`, use `estimator\_name` if not `undefined`, otherwise no labeling is shown.
*/
name?: string
/**
Whether to plot the chance level.
@defaultValue `false`
*/
plot_chance_level?: boolean
/**
Keyword arguments to be passed to matplotlib’s `plot` for rendering the chance level line.
*/
chance_level_kw?: any
/**
Keyword arguments to be passed to matplotlib’s `plot`.
*/
kwargs?: any
}): Promise<any> {
if (this._isDisposed) {
throw new Error('This RocCurveDisplay instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('RocCurveDisplay must call init() before plot()')
}
// set up method params
await this._py.ex`pms_RocCurveDisplay_plot = {'ax': ${
opts['ax'] ?? undefined
}, 'name': ${opts['name'] ?? undefined}, 'plot_chance_level': ${
opts['plot_chance_level'] ?? undefined
}, 'chance_level_kw': ${opts['chance_level_kw'] ?? undefined}, 'kwargs': ${
opts['kwargs'] ?? undefined
}}
pms_RocCurveDisplay_plot = {k: v for k, v in pms_RocCurveDisplay_plot.items() if v is not None}`
// invoke method
await this._py
.ex`res_RocCurveDisplay_plot = bridgeRocCurveDisplay[${this.id}].plot(**pms_RocCurveDisplay_plot)`
// convert the result from python to node.js
return this
._py`res_RocCurveDisplay_plot.tolist() if hasattr(res_RocCurveDisplay_plot, 'tolist') else res_RocCurveDisplay_plot`
}
/**
ROC Curve.
*/
get line_(): Promise<any> {
if (this._isDisposed) {
throw new Error('This RocCurveDisplay instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('RocCurveDisplay must call init() before accessing line_')
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_RocCurveDisplay_line_ = bridgeRocCurveDisplay[${this.id}].line_`
// convert the result from python to node.js
return this
._py`attr_RocCurveDisplay_line_.tolist() if hasattr(attr_RocCurveDisplay_line_, 'tolist') else attr_RocCurveDisplay_line_`
})()
}
/**
The chance level line. It is `undefined` if the chance level is not plotted.
*/
get chance_level_(): Promise<any> {
if (this._isDisposed) {
throw new Error('This RocCurveDisplay instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'RocCurveDisplay must call init() before accessing chance_level_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_RocCurveDisplay_chance_level_ = bridgeRocCurveDisplay[${this.id}].chance_level_`
// convert the result from python to node.js
return this
._py`attr_RocCurveDisplay_chance_level_.tolist() if hasattr(attr_RocCurveDisplay_chance_level_, 'tolist') else attr_RocCurveDisplay_chance_level_`
})()
}
/**
Axes with ROC Curve.
*/
get ax_(): Promise<any> {
if (this._isDisposed) {
throw new Error('This RocCurveDisplay instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('RocCurveDisplay must call init() before accessing ax_')
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_RocCurveDisplay_ax_ = bridgeRocCurveDisplay[${this.id}].ax_`
// convert the result from python to node.js
return this
._py`attr_RocCurveDisplay_ax_.tolist() if hasattr(attr_RocCurveDisplay_ax_, 'tolist') else attr_RocCurveDisplay_ax_`
})()
}
/**
Figure containing the curve.
*/
get figure_(): Promise<any> {
if (this._isDisposed) {
throw new Error('This RocCurveDisplay instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'RocCurveDisplay must call init() before accessing figure_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_RocCurveDisplay_figure_ = bridgeRocCurveDisplay[${this.id}].figure_`
// convert the result from python to node.js
return this
._py`attr_RocCurveDisplay_figure_.tolist() if hasattr(attr_RocCurveDisplay_figure_, 'tolist') else attr_RocCurveDisplay_figure_`
})()
}
}