-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
FunctionTransformer.ts
499 lines (405 loc) · 15.8 KB
/
FunctionTransformer.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'
/**
Constructs a transformer from an arbitrary callable.
A FunctionTransformer forwards its X (and optionally y) arguments to a user-defined function or function object and returns the result of this function. This is useful for stateless transformations such as taking the log of frequencies, doing custom scaling, etc.
Note: If a lambda is used as the function, then the resulting transformer will not be pickleable.
[Python Reference](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.FunctionTransformer.html)
*/
export class FunctionTransformer {
id: string
opts: any
_py: PythonBridge
_isInitialized: boolean = false
_isDisposed: boolean = false
constructor(opts?: {
/**
The callable to use for the transformation. This will be passed the same arguments as transform, with args and kwargs forwarded. If func is `undefined`, then func will be the identity function.
*/
func?: any
/**
The callable to use for the inverse transformation. This will be passed the same arguments as inverse transform, with args and kwargs forwarded. If inverse\_func is `undefined`, then inverse\_func will be the identity function.
*/
inverse_func?: any
/**
Indicate that the input X array should be checked before calling `func`. The possibilities are:
@defaultValue `false`
*/
validate?: boolean
/**
Indicate that func accepts a sparse matrix as input. If validate is `false`, this has no effect. Otherwise, if accept\_sparse is false, sparse matrix inputs will cause an exception to be raised.
@defaultValue `false`
*/
accept_sparse?: boolean
/**
Whether to check that or `func` followed by `inverse\_func` leads to the original inputs. It can be used for a sanity check, raising a warning when the condition is not fulfilled.
@defaultValue `true`
*/
check_inverse?: boolean
/**
Determines the list of feature names that will be returned by the `get\_feature\_names\_out` method. If it is ‘one-to-one’, then the output feature names will be equal to the input feature names. If it is a callable, then it must take two positional arguments: this `FunctionTransformer` (`self`) and an array-like of input feature names (`input\_features`). It must return an array-like of output feature names. The `get\_feature\_names\_out` method is only defined if `feature\_names\_out` is not `undefined`.
See `get\_feature\_names\_out` for more details.
*/
feature_names_out?: 'one-to-one'
/**
Dictionary of additional keyword arguments to pass to func.
*/
kw_args?: any
/**
Dictionary of additional keyword arguments to pass to inverse\_func.
*/
inv_kw_args?: any
}) {
this.id = `FunctionTransformer${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 FunctionTransformer instance has already been disposed'
)
}
if (this._isInitialized) {
return
}
if (!py) {
throw new Error(
'FunctionTransformer.init requires a PythonBridge instance'
)
}
this._py = py
await this._py.ex`
import numpy as np
from sklearn.preprocessing import FunctionTransformer
try: bridgeFunctionTransformer
except NameError: bridgeFunctionTransformer = {}
`
// set up constructor params
await this._py.ex`ctor_FunctionTransformer = {'func': ${
this.opts['func'] ?? undefined
}, 'inverse_func': ${this.opts['inverse_func'] ?? undefined}, 'validate': ${
this.opts['validate'] ?? undefined
}, 'accept_sparse': ${
this.opts['accept_sparse'] ?? undefined
}, 'check_inverse': ${
this.opts['check_inverse'] ?? undefined
}, 'feature_names_out': ${
this.opts['feature_names_out'] ?? undefined
}, 'kw_args': ${this.opts['kw_args'] ?? undefined}, 'inv_kw_args': ${
this.opts['inv_kw_args'] ?? undefined
}}
ctor_FunctionTransformer = {k: v for k, v in ctor_FunctionTransformer.items() if v is not None}`
await this._py
.ex`bridgeFunctionTransformer[${this.id}] = FunctionTransformer(**ctor_FunctionTransformer)`
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 bridgeFunctionTransformer[${this.id}]`
this._isDisposed = true
}
/**
Fit transformer by checking X.
If `validate` is `true`, `X` will be checked.
*/
async fit(opts: {
/**
Input array.
*/
X?: any
/**
Not used, present here for API consistency by convention.
*/
y?: any
}): Promise<any> {
if (this._isDisposed) {
throw new Error(
'This FunctionTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error('FunctionTransformer must call init() before fit()')
}
// set up method params
await this._py.ex`pms_FunctionTransformer_fit = {'X': np.array(${
opts['X'] ?? undefined
}) if ${opts['X'] !== undefined} else None, 'y': ${opts['y'] ?? undefined}}
pms_FunctionTransformer_fit = {k: v for k, v in pms_FunctionTransformer_fit.items() if v is not None}`
// invoke method
await this._py
.ex`res_FunctionTransformer_fit = bridgeFunctionTransformer[${this.id}].fit(**pms_FunctionTransformer_fit)`
// convert the result from python to node.js
return this
._py`res_FunctionTransformer_fit.tolist() if hasattr(res_FunctionTransformer_fit, 'tolist') else res_FunctionTransformer_fit`
}
/**
Fit to data, then transform it.
Fits transformer to `X` and `y` with optional parameters `fit\_params` and returns a transformed version of `X`.
*/
async fit_transform(opts: {
/**
Input samples.
*/
X?: ArrayLike[]
/**
Target values (`undefined` for unsupervised transformations).
*/
y?: ArrayLike
/**
Additional fit parameters.
*/
fit_params?: any
}): Promise<any[]> {
if (this._isDisposed) {
throw new Error(
'This FunctionTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'FunctionTransformer must call init() before fit_transform()'
)
}
// set up method params
await this._py.ex`pms_FunctionTransformer_fit_transform = {'X': np.array(${
opts['X'] ?? undefined
}) if ${opts['X'] !== undefined} else None, 'y': np.array(${
opts['y'] ?? undefined
}) if ${opts['y'] !== undefined} else None, 'fit_params': ${
opts['fit_params'] ?? undefined
}}
pms_FunctionTransformer_fit_transform = {k: v for k, v in pms_FunctionTransformer_fit_transform.items() if v is not None}`
// invoke method
await this._py
.ex`res_FunctionTransformer_fit_transform = bridgeFunctionTransformer[${this.id}].fit_transform(**pms_FunctionTransformer_fit_transform)`
// convert the result from python to node.js
return this
._py`res_FunctionTransformer_fit_transform.tolist() if hasattr(res_FunctionTransformer_fit_transform, 'tolist') else res_FunctionTransformer_fit_transform`
}
/**
Get output feature names for transformation.
This method is only defined if `feature\_names\_out` is not `undefined`.
*/
async get_feature_names_out(opts: {
/**
Input feature names.
*/
input_features?: any
}): Promise<any> {
if (this._isDisposed) {
throw new Error(
'This FunctionTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'FunctionTransformer must call init() before get_feature_names_out()'
)
}
// set up method params
await this._py
.ex`pms_FunctionTransformer_get_feature_names_out = {'input_features': ${
opts['input_features'] ?? undefined
}}
pms_FunctionTransformer_get_feature_names_out = {k: v for k, v in pms_FunctionTransformer_get_feature_names_out.items() if v is not None}`
// invoke method
await this._py
.ex`res_FunctionTransformer_get_feature_names_out = bridgeFunctionTransformer[${this.id}].get_feature_names_out(**pms_FunctionTransformer_get_feature_names_out)`
// convert the result from python to node.js
return this
._py`res_FunctionTransformer_get_feature_names_out.tolist() if hasattr(res_FunctionTransformer_get_feature_names_out, 'tolist') else res_FunctionTransformer_get_feature_names_out`
}
/**
Get metadata routing of this object.
Please check [User Guide](../../metadata_routing.html#metadata-routing) on how the routing mechanism works.
*/
async get_metadata_routing(opts: {
/**
A [`MetadataRequest`](sklearn.utils.metadata_routing.MetadataRequest.html#sklearn.utils.metadata_routing.MetadataRequest "sklearn.utils.metadata_routing.MetadataRequest") encapsulating routing information.
*/
routing?: any
}): Promise<any> {
if (this._isDisposed) {
throw new Error(
'This FunctionTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'FunctionTransformer must call init() before get_metadata_routing()'
)
}
// set up method params
await this._py
.ex`pms_FunctionTransformer_get_metadata_routing = {'routing': ${
opts['routing'] ?? undefined
}}
pms_FunctionTransformer_get_metadata_routing = {k: v for k, v in pms_FunctionTransformer_get_metadata_routing.items() if v is not None}`
// invoke method
await this._py
.ex`res_FunctionTransformer_get_metadata_routing = bridgeFunctionTransformer[${this.id}].get_metadata_routing(**pms_FunctionTransformer_get_metadata_routing)`
// convert the result from python to node.js
return this
._py`res_FunctionTransformer_get_metadata_routing.tolist() if hasattr(res_FunctionTransformer_get_metadata_routing, 'tolist') else res_FunctionTransformer_get_metadata_routing`
}
/**
Transform X using the inverse function.
*/
async inverse_transform(opts: {
/**
Input array.
*/
X?: any
}): Promise<ArrayLike> {
if (this._isDisposed) {
throw new Error(
'This FunctionTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'FunctionTransformer must call init() before inverse_transform()'
)
}
// set up method params
await this._py
.ex`pms_FunctionTransformer_inverse_transform = {'X': np.array(${
opts['X'] ?? undefined
}) if ${opts['X'] !== undefined} else None}
pms_FunctionTransformer_inverse_transform = {k: v for k, v in pms_FunctionTransformer_inverse_transform.items() if v is not None}`
// invoke method
await this._py
.ex`res_FunctionTransformer_inverse_transform = bridgeFunctionTransformer[${this.id}].inverse_transform(**pms_FunctionTransformer_inverse_transform)`
// convert the result from python to node.js
return this
._py`res_FunctionTransformer_inverse_transform.tolist() if hasattr(res_FunctionTransformer_inverse_transform, 'tolist') else res_FunctionTransformer_inverse_transform`
}
/**
Set output container.
See [Introducing the set\_output API](../../auto_examples/miscellaneous/plot_set_output.html#sphx-glr-auto-examples-miscellaneous-plot-set-output-py) for an example on how to use the API.
*/
async set_output(opts: {
/**
Configure output of `transform` and `fit\_transform`.
*/
transform?: 'default' | 'pandas'
}): Promise<any> {
if (this._isDisposed) {
throw new Error(
'This FunctionTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'FunctionTransformer must call init() before set_output()'
)
}
// set up method params
await this._py.ex`pms_FunctionTransformer_set_output = {'transform': ${
opts['transform'] ?? undefined
}}
pms_FunctionTransformer_set_output = {k: v for k, v in pms_FunctionTransformer_set_output.items() if v is not None}`
// invoke method
await this._py
.ex`res_FunctionTransformer_set_output = bridgeFunctionTransformer[${this.id}].set_output(**pms_FunctionTransformer_set_output)`
// convert the result from python to node.js
return this
._py`res_FunctionTransformer_set_output.tolist() if hasattr(res_FunctionTransformer_set_output, 'tolist') else res_FunctionTransformer_set_output`
}
/**
Transform X using the forward function.
*/
async transform(opts: {
/**
Input array.
*/
X?: any
}): Promise<ArrayLike> {
if (this._isDisposed) {
throw new Error(
'This FunctionTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error('FunctionTransformer must call init() before transform()')
}
// set up method params
await this._py.ex`pms_FunctionTransformer_transform = {'X': np.array(${
opts['X'] ?? undefined
}) if ${opts['X'] !== undefined} else None}
pms_FunctionTransformer_transform = {k: v for k, v in pms_FunctionTransformer_transform.items() if v is not None}`
// invoke method
await this._py
.ex`res_FunctionTransformer_transform = bridgeFunctionTransformer[${this.id}].transform(**pms_FunctionTransformer_transform)`
// convert the result from python to node.js
return this
._py`res_FunctionTransformer_transform.tolist() if hasattr(res_FunctionTransformer_transform, 'tolist') else res_FunctionTransformer_transform`
}
/**
Number of features seen during [fit](../../glossary.html#term-fit).
*/
get n_features_in_(): Promise<number> {
if (this._isDisposed) {
throw new Error(
'This FunctionTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'FunctionTransformer must call init() before accessing n_features_in_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_FunctionTransformer_n_features_in_ = bridgeFunctionTransformer[${this.id}].n_features_in_`
// convert the result from python to node.js
return this
._py`attr_FunctionTransformer_n_features_in_.tolist() if hasattr(attr_FunctionTransformer_n_features_in_, 'tolist') else attr_FunctionTransformer_n_features_in_`
})()
}
/**
Names of features seen during [fit](../../glossary.html#term-fit). Defined only when `X` has feature names that are all strings.
*/
get feature_names_in_(): Promise<NDArray> {
if (this._isDisposed) {
throw new Error(
'This FunctionTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'FunctionTransformer must call init() before accessing feature_names_in_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_FunctionTransformer_feature_names_in_ = bridgeFunctionTransformer[${this.id}].feature_names_in_`
// convert the result from python to node.js
return this
._py`attr_FunctionTransformer_feature_names_in_.tolist() if hasattr(attr_FunctionTransformer_feature_names_in_, 'tolist') else attr_FunctionTransformer_feature_names_in_`
})()
}
}