-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
PrecisionRecallDisplay.ts
531 lines (436 loc) · 16.9 KB
/
PrecisionRecallDisplay.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/* 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'
/**
Precision Recall visualization.
It is recommend to use [`from\_estimator`](#sklearn.metrics.PrecisionRecallDisplay.from_estimator "sklearn.metrics.PrecisionRecallDisplay.from_estimator") or [`from\_predictions`](#sklearn.metrics.PrecisionRecallDisplay.from_predictions "sklearn.metrics.PrecisionRecallDisplay.from_predictions") to create a [`PrecisionRecallDisplay`](#sklearn.metrics.PrecisionRecallDisplay "sklearn.metrics.PrecisionRecallDisplay"). 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.PrecisionRecallDisplay.html)
*/
export class PrecisionRecallDisplay {
id: string
opts: any
_py: PythonBridge
_isInitialized: boolean = false
_isDisposed: boolean = false
constructor(opts?: {
/**
Precision values.
*/
precision?: NDArray
/**
Recall values.
*/
recall?: NDArray
/**
Average precision. If `undefined`, the average precision is not shown.
*/
average_precision?: number
/**
Name of estimator. If `undefined`, then the estimator name is not shown.
*/
estimator_name?: string
/**
The class considered as the positive class. If `undefined`, the class will not be shown in the legend.
*/
pos_label?: number | boolean | string
/**
The prevalence of the positive label. It is used for plotting the chance level line. If `undefined`, the chance level line will not be plotted even if `plot\_chance\_level` is set to `true` when plotting.
*/
prevalence_pos_label?: number
}) {
this.id = `PrecisionRecallDisplay${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 PrecisionRecallDisplay instance has already been disposed'
)
}
if (this._isInitialized) {
return
}
if (!py) {
throw new Error(
'PrecisionRecallDisplay.init requires a PythonBridge instance'
)
}
this._py = py
await this._py.ex`
import numpy as np
from sklearn.metrics import PrecisionRecallDisplay
try: bridgePrecisionRecallDisplay
except NameError: bridgePrecisionRecallDisplay = {}
`
// set up constructor params
await this._py.ex`ctor_PrecisionRecallDisplay = {'precision': np.array(${
this.opts['precision'] ?? undefined
}) if ${
this.opts['precision'] !== undefined
} else None, 'recall': np.array(${this.opts['recall'] ?? undefined}) if ${
this.opts['recall'] !== undefined
} else None, 'average_precision': ${
this.opts['average_precision'] ?? undefined
}, 'estimator_name': ${
this.opts['estimator_name'] ?? undefined
}, 'pos_label': ${
this.opts['pos_label'] ?? undefined
}, 'prevalence_pos_label': ${
this.opts['prevalence_pos_label'] ?? undefined
}}
ctor_PrecisionRecallDisplay = {k: v for k, v in ctor_PrecisionRecallDisplay.items() if v is not None}`
await this._py
.ex`bridgePrecisionRecallDisplay[${this.id}] = PrecisionRecallDisplay(**ctor_PrecisionRecallDisplay)`
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 bridgePrecisionRecallDisplay[${this.id}]`
this._isDisposed = true
}
/**
Plot precision-recall curve given an estimator and some data.
*/
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
/**
The class considered as the positive class when computing the precision and recall metrics. By default, `estimators.classes\_\[1\]` is considered as the positive class.
*/
pos_label?: number | boolean | string
/**
Whether to drop some suboptimal thresholds which would not appear on a plotted precision-recall curve. This is useful in order to create lighter precision-recall curves.
@defaultValue `false`
*/
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.
@defaultValue `'auto'`
*/
response_method?: 'predict_proba' | 'decision_function' | 'auto'
/**
Name for labeling curve. If `undefined`, no name is used.
*/
name?: string
/**
Axes object to plot on. If `undefined`, a new figure and axes is created.
*/
ax?: any
/**
Whether to plot the chance level. The chance level is the prevalence of the positive label computed from the data passed during [`from\_estimator`](#sklearn.metrics.PrecisionRecallDisplay.from_estimator "sklearn.metrics.PrecisionRecallDisplay.from_estimator") or [`from\_predictions`](#sklearn.metrics.PrecisionRecallDisplay.from_predictions "sklearn.metrics.PrecisionRecallDisplay.from_predictions") call.
@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 PrecisionRecallDisplay instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'PrecisionRecallDisplay must call init() before from_estimator()'
)
}
// set up method params
await this._py
.ex`pms_PrecisionRecallDisplay_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, 'pos_label': ${
opts['pos_label'] ?? undefined
}, 'drop_intermediate': ${
opts['drop_intermediate'] ?? undefined
}, 'response_method': ${opts['response_method'] ?? 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_PrecisionRecallDisplay_from_estimator = {k: v for k, v in pms_PrecisionRecallDisplay_from_estimator.items() if v is not None}`
// invoke method
await this._py
.ex`res_PrecisionRecallDisplay_from_estimator = bridgePrecisionRecallDisplay[${this.id}].from_estimator(**pms_PrecisionRecallDisplay_from_estimator)`
// convert the result from python to node.js
return this
._py`res_PrecisionRecallDisplay_from_estimator.tolist() if hasattr(res_PrecisionRecallDisplay_from_estimator, 'tolist') else res_PrecisionRecallDisplay_from_estimator`
}
/**
Plot precision-recall curve given binary class predictions.
*/
async from_predictions(opts: {
/**
True binary labels.
*/
y_true?: ArrayLike
/**
Estimated probabilities or output of decision function.
*/
y_pred?: ArrayLike
/**
Sample weights.
*/
sample_weight?: ArrayLike
/**
The class considered as the positive class when computing the precision and recall metrics.
*/
pos_label?: number | boolean | string
/**
Whether to drop some suboptimal thresholds which would not appear on a plotted precision-recall curve. This is useful in order to create lighter precision-recall curves.
@defaultValue `false`
*/
drop_intermediate?: boolean
/**
Name for labeling curve. 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. The chance level is the prevalence of the positive label computed from the data passed during [`from\_estimator`](#sklearn.metrics.PrecisionRecallDisplay.from_estimator "sklearn.metrics.PrecisionRecallDisplay.from_estimator") or [`from\_predictions`](#sklearn.metrics.PrecisionRecallDisplay.from_predictions "sklearn.metrics.PrecisionRecallDisplay.from_predictions") call.
@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 PrecisionRecallDisplay instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'PrecisionRecallDisplay must call init() before from_predictions()'
)
}
// set up method params
await this._py
.ex`pms_PrecisionRecallDisplay_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, 'pos_label': ${
opts['pos_label'] ?? undefined
}, 'drop_intermediate': ${
opts['drop_intermediate'] ?? 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_PrecisionRecallDisplay_from_predictions = {k: v for k, v in pms_PrecisionRecallDisplay_from_predictions.items() if v is not None}`
// invoke method
await this._py
.ex`res_PrecisionRecallDisplay_from_predictions = bridgePrecisionRecallDisplay[${this.id}].from_predictions(**pms_PrecisionRecallDisplay_from_predictions)`
// convert the result from python to node.js
return this
._py`res_PrecisionRecallDisplay_from_predictions.tolist() if hasattr(res_PrecisionRecallDisplay_from_predictions, 'tolist') else res_PrecisionRecallDisplay_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 precision recall curve for labeling. If `undefined`, use `estimator\_name` if not `undefined`, otherwise no labeling is shown.
*/
name?: string
/**
Whether to plot the chance level. The chance level is the prevalence of the positive label computed from the data passed during [`from\_estimator`](#sklearn.metrics.PrecisionRecallDisplay.from_estimator "sklearn.metrics.PrecisionRecallDisplay.from_estimator") or [`from\_predictions`](#sklearn.metrics.PrecisionRecallDisplay.from_predictions "sklearn.metrics.PrecisionRecallDisplay.from_predictions") call.
@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 PrecisionRecallDisplay instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error('PrecisionRecallDisplay must call init() before plot()')
}
// set up method params
await this._py.ex`pms_PrecisionRecallDisplay_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_PrecisionRecallDisplay_plot = {k: v for k, v in pms_PrecisionRecallDisplay_plot.items() if v is not None}`
// invoke method
await this._py
.ex`res_PrecisionRecallDisplay_plot = bridgePrecisionRecallDisplay[${this.id}].plot(**pms_PrecisionRecallDisplay_plot)`
// convert the result from python to node.js
return this
._py`res_PrecisionRecallDisplay_plot.tolist() if hasattr(res_PrecisionRecallDisplay_plot, 'tolist') else res_PrecisionRecallDisplay_plot`
}
/**
Precision recall curve.
*/
get line_(): Promise<any> {
if (this._isDisposed) {
throw new Error(
'This PrecisionRecallDisplay instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'PrecisionRecallDisplay must call init() before accessing line_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_PrecisionRecallDisplay_line_ = bridgePrecisionRecallDisplay[${this.id}].line_`
// convert the result from python to node.js
return this
._py`attr_PrecisionRecallDisplay_line_.tolist() if hasattr(attr_PrecisionRecallDisplay_line_, 'tolist') else attr_PrecisionRecallDisplay_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 PrecisionRecallDisplay instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'PrecisionRecallDisplay must call init() before accessing chance_level_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_PrecisionRecallDisplay_chance_level_ = bridgePrecisionRecallDisplay[${this.id}].chance_level_`
// convert the result from python to node.js
return this
._py`attr_PrecisionRecallDisplay_chance_level_.tolist() if hasattr(attr_PrecisionRecallDisplay_chance_level_, 'tolist') else attr_PrecisionRecallDisplay_chance_level_`
})()
}
/**
Axes with precision recall curve.
*/
get ax_(): Promise<any> {
if (this._isDisposed) {
throw new Error(
'This PrecisionRecallDisplay instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'PrecisionRecallDisplay must call init() before accessing ax_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_PrecisionRecallDisplay_ax_ = bridgePrecisionRecallDisplay[${this.id}].ax_`
// convert the result from python to node.js
return this
._py`attr_PrecisionRecallDisplay_ax_.tolist() if hasattr(attr_PrecisionRecallDisplay_ax_, 'tolist') else attr_PrecisionRecallDisplay_ax_`
})()
}
/**
Figure containing the curve.
*/
get figure_(): Promise<any> {
if (this._isDisposed) {
throw new Error(
'This PrecisionRecallDisplay instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'PrecisionRecallDisplay must call init() before accessing figure_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_PrecisionRecallDisplay_figure_ = bridgePrecisionRecallDisplay[${this.id}].figure_`
// convert the result from python to node.js
return this
._py`attr_PrecisionRecallDisplay_figure_.tolist() if hasattr(attr_PrecisionRecallDisplay_figure_, 'tolist') else attr_PrecisionRecallDisplay_figure_`
})()
}
}