-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ColumnTransformer.ts
522 lines (427 loc) · 18.5 KB
/
ColumnTransformer.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
/* 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'
/**
Applies transformers to columns of an array or pandas DataFrame.
This estimator allows different columns or column subsets of the input to be transformed separately and the features generated by each transformer will be concatenated to form a single feature space. This is useful for heterogeneous or columnar data, to combine several feature extraction mechanisms or transformations into a single transformer.
Read more in the [User Guide](../compose.html#column-transformer).
[Python Reference](https://scikit-learn.org/stable/modules/generated/sklearn.compose.ColumnTransformer.html)
*/
export class ColumnTransformer {
id: string
opts: any
_py: PythonBridge
_isInitialized: boolean = false
_isDisposed: boolean = false
constructor(opts?: {
/**
List of (name, transformer, columns) tuples specifying the transformer objects to be applied to subsets of the data.
*/
transformers?: any
/**
By default, only the specified columns in `transformers` are transformed and combined in the output, and the non-specified columns are dropped. (default of `'drop'`). By specifying `remainder='passthrough'`, all remaining columns that were not specified in `transformers`, but present in the data passed to `fit` will be automatically passed through. This subset of columns is concatenated with the output of the transformers. For dataframes, extra columns not seen during `fit` will be excluded from the output of `transform`. By setting `remainder` to be an estimator, the remaining non-specified columns will use the `remainder` estimator. The estimator must support [fit](../../glossary.html#term-fit) and [transform](../../glossary.html#term-transform). Note that using this feature requires that the DataFrame columns input at [fit](../../glossary.html#term-fit) and [transform](../../glossary.html#term-transform) have identical order.
@defaultValue `'drop'`
*/
remainder?: 'drop' | 'passthrough'
/**
If the output of the different transformers contains sparse matrices, these will be stacked as a sparse matrix if the overall density is lower than this value. Use `sparse\_threshold=0` to always return dense. When the transformed output consists of all dense data, the stacked result will be dense, and this keyword will be ignored.
@defaultValue `0.3`
*/
sparse_threshold?: number
/**
Number of jobs to run in parallel. `undefined` means 1 unless in a [`joblib.parallel\_backend`](https://joblib.readthedocs.io/en/latest/generated/joblib.parallel_backend.html#joblib.parallel_backend "(in joblib v1.4.dev0)") context. `\-1` means using all processors. See [Glossary](../../glossary.html#term-n_jobs) for more details.
*/
n_jobs?: number
/**
Multiplicative weights for features per transformer. The output of the transformer is multiplied by these weights. Keys are transformer names, values the weights.
*/
transformer_weights?: any
/**
If `true`, the time elapsed while fitting each transformer will be printed as it is completed.
@defaultValue `false`
*/
verbose?: boolean
/**
If `true`, [`ColumnTransformer.get\_feature\_names\_out`](#sklearn.compose.ColumnTransformer.get_feature_names_out "sklearn.compose.ColumnTransformer.get_feature_names_out") will prefix all feature names with the name of the transformer that generated that feature. If `false`, [`ColumnTransformer.get\_feature\_names\_out`](#sklearn.compose.ColumnTransformer.get_feature_names_out "sklearn.compose.ColumnTransformer.get_feature_names_out") will not prefix any feature names and will error if feature names are not unique.
@defaultValue `true`
*/
verbose_feature_names_out?: boolean
}) {
this.id = `ColumnTransformer${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 ColumnTransformer instance has already been disposed'
)
}
if (this._isInitialized) {
return
}
if (!py) {
throw new Error('ColumnTransformer.init requires a PythonBridge instance')
}
this._py = py
await this._py.ex`
import numpy as np
from sklearn.compose import ColumnTransformer
try: bridgeColumnTransformer
except NameError: bridgeColumnTransformer = {}
`
// set up constructor params
await this._py.ex`ctor_ColumnTransformer = {'transformers': ${
this.opts['transformers'] ?? undefined
}, 'remainder': ${
this.opts['remainder'] ?? undefined
}, 'sparse_threshold': ${
this.opts['sparse_threshold'] ?? undefined
}, 'n_jobs': ${this.opts['n_jobs'] ?? undefined}, 'transformer_weights': ${
this.opts['transformer_weights'] ?? undefined
}, 'verbose': ${
this.opts['verbose'] ?? undefined
}, 'verbose_feature_names_out': ${
this.opts['verbose_feature_names_out'] ?? undefined
}}
ctor_ColumnTransformer = {k: v for k, v in ctor_ColumnTransformer.items() if v is not None}`
await this._py
.ex`bridgeColumnTransformer[${this.id}] = ColumnTransformer(**ctor_ColumnTransformer)`
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 bridgeColumnTransformer[${this.id}]`
this._isDisposed = true
}
/**
Fit all transformers using X.
*/
async fit(opts: {
/**
Input data, of which specified subsets are used to fit the transformers.
*/
X?: ArrayLike[]
/**
Targets for supervised learning.
*/
y?: ArrayLike[]
}): Promise<any> {
if (this._isDisposed) {
throw new Error(
'This ColumnTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error('ColumnTransformer must call init() before fit()')
}
// set up method params
await this._py.ex`pms_ColumnTransformer_fit = {'X': np.array(${
opts['X'] ?? undefined
}) if ${opts['X'] !== undefined} else None, 'y': np.array(${
opts['y'] ?? undefined
}) if ${opts['y'] !== undefined} else None}
pms_ColumnTransformer_fit = {k: v for k, v in pms_ColumnTransformer_fit.items() if v is not None}`
// invoke method
await this._py
.ex`res_ColumnTransformer_fit = bridgeColumnTransformer[${this.id}].fit(**pms_ColumnTransformer_fit)`
// convert the result from python to node.js
return this
._py`res_ColumnTransformer_fit.tolist() if hasattr(res_ColumnTransformer_fit, 'tolist') else res_ColumnTransformer_fit`
}
/**
Fit all transformers, transform the data and concatenate results.
*/
async fit_transform(opts: {
/**
Input data, of which specified subsets are used to fit the transformers.
*/
X?: ArrayLike[]
/**
Targets for supervised learning.
*/
y?: ArrayLike
}): Promise<ArrayLike | SparseMatrix[]> {
if (this._isDisposed) {
throw new Error(
'This ColumnTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'ColumnTransformer must call init() before fit_transform()'
)
}
// set up method params
await this._py.ex`pms_ColumnTransformer_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}
pms_ColumnTransformer_fit_transform = {k: v for k, v in pms_ColumnTransformer_fit_transform.items() if v is not None}`
// invoke method
await this._py
.ex`res_ColumnTransformer_fit_transform = bridgeColumnTransformer[${this.id}].fit_transform(**pms_ColumnTransformer_fit_transform)`
// convert the result from python to node.js
return this
._py`res_ColumnTransformer_fit_transform.tolist() if hasattr(res_ColumnTransformer_fit_transform, 'tolist') else res_ColumnTransformer_fit_transform`
}
/**
Get output feature names for transformation.
*/
async get_feature_names_out(opts: {
/**
Input features.
*/
input_features?: any
}): Promise<any> {
if (this._isDisposed) {
throw new Error(
'This ColumnTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'ColumnTransformer must call init() before get_feature_names_out()'
)
}
// set up method params
await this._py
.ex`pms_ColumnTransformer_get_feature_names_out = {'input_features': ${
opts['input_features'] ?? undefined
}}
pms_ColumnTransformer_get_feature_names_out = {k: v for k, v in pms_ColumnTransformer_get_feature_names_out.items() if v is not None}`
// invoke method
await this._py
.ex`res_ColumnTransformer_get_feature_names_out = bridgeColumnTransformer[${this.id}].get_feature_names_out(**pms_ColumnTransformer_get_feature_names_out)`
// convert the result from python to node.js
return this
._py`res_ColumnTransformer_get_feature_names_out.tolist() if hasattr(res_ColumnTransformer_get_feature_names_out, 'tolist') else res_ColumnTransformer_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 ColumnTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'ColumnTransformer must call init() before get_metadata_routing()'
)
}
// set up method params
await this._py
.ex`pms_ColumnTransformer_get_metadata_routing = {'routing': ${
opts['routing'] ?? undefined
}}
pms_ColumnTransformer_get_metadata_routing = {k: v for k, v in pms_ColumnTransformer_get_metadata_routing.items() if v is not None}`
// invoke method
await this._py
.ex`res_ColumnTransformer_get_metadata_routing = bridgeColumnTransformer[${this.id}].get_metadata_routing(**pms_ColumnTransformer_get_metadata_routing)`
// convert the result from python to node.js
return this
._py`res_ColumnTransformer_get_metadata_routing.tolist() if hasattr(res_ColumnTransformer_get_metadata_routing, 'tolist') else res_ColumnTransformer_get_metadata_routing`
}
/**
Set the output container when `"transform"` and `"fit\_transform"` are called.
Calling `set\_output` will set the output of all estimators in `transformers` and `transformers\_`.
*/
async set_output(opts: {
/**
Configure output of `transform` and `fit\_transform`.
*/
transform?: 'default' | 'pandas'
}): Promise<any> {
if (this._isDisposed) {
throw new Error(
'This ColumnTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error('ColumnTransformer must call init() before set_output()')
}
// set up method params
await this._py.ex`pms_ColumnTransformer_set_output = {'transform': ${
opts['transform'] ?? undefined
}}
pms_ColumnTransformer_set_output = {k: v for k, v in pms_ColumnTransformer_set_output.items() if v is not None}`
// invoke method
await this._py
.ex`res_ColumnTransformer_set_output = bridgeColumnTransformer[${this.id}].set_output(**pms_ColumnTransformer_set_output)`
// convert the result from python to node.js
return this
._py`res_ColumnTransformer_set_output.tolist() if hasattr(res_ColumnTransformer_set_output, 'tolist') else res_ColumnTransformer_set_output`
}
/**
Transform X separately by each transformer, concatenate results.
*/
async transform(opts: {
/**
The data to be transformed by subset.
*/
X?: ArrayLike[]
}): Promise<ArrayLike | SparseMatrix[]> {
if (this._isDisposed) {
throw new Error(
'This ColumnTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error('ColumnTransformer must call init() before transform()')
}
// set up method params
await this._py.ex`pms_ColumnTransformer_transform = {'X': np.array(${
opts['X'] ?? undefined
}) if ${opts['X'] !== undefined} else None}
pms_ColumnTransformer_transform = {k: v for k, v in pms_ColumnTransformer_transform.items() if v is not None}`
// invoke method
await this._py
.ex`res_ColumnTransformer_transform = bridgeColumnTransformer[${this.id}].transform(**pms_ColumnTransformer_transform)`
// convert the result from python to node.js
return this
._py`res_ColumnTransformer_transform.tolist() if hasattr(res_ColumnTransformer_transform, 'tolist') else res_ColumnTransformer_transform`
}
/**
The collection of fitted transformers as tuples of (name, fitted\_transformer, column). `fitted\_transformer` can be an estimator, ‘drop’, or ‘passthrough’. In case there were no columns selected, this will be the unfitted transformer. If there are remaining columns, the final element is a tuple of the form: (‘remainder’, transformer, remaining\_columns) corresponding to the `remainder` parameter. If there are remaining columns, then `len(transformers\_)==len(transformers)+1`, otherwise `len(transformers\_)==len(transformers)`.
*/
get transformers_(): Promise<any[]> {
if (this._isDisposed) {
throw new Error(
'This ColumnTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'ColumnTransformer must call init() before accessing transformers_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_ColumnTransformer_transformers_ = bridgeColumnTransformer[${this.id}].transformers_`
// convert the result from python to node.js
return this
._py`attr_ColumnTransformer_transformers_.tolist() if hasattr(attr_ColumnTransformer_transformers_, 'tolist') else attr_ColumnTransformer_transformers_`
})()
}
/**
Boolean flag indicating whether the output of `transform` is a sparse matrix or a dense numpy array, which depends on the output of the individual transformers and the `sparse\_threshold` keyword.
*/
get sparse_output_(): Promise<boolean> {
if (this._isDisposed) {
throw new Error(
'This ColumnTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'ColumnTransformer must call init() before accessing sparse_output_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_ColumnTransformer_sparse_output_ = bridgeColumnTransformer[${this.id}].sparse_output_`
// convert the result from python to node.js
return this
._py`attr_ColumnTransformer_sparse_output_.tolist() if hasattr(attr_ColumnTransformer_sparse_output_, 'tolist') else attr_ColumnTransformer_sparse_output_`
})()
}
/**
A dictionary from each transformer name to a slice, where the slice corresponds to indices in the transformed output. This is useful to inspect which transformer is responsible for which transformed feature(s).
*/
get output_indices_(): Promise<any> {
if (this._isDisposed) {
throw new Error(
'This ColumnTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'ColumnTransformer must call init() before accessing output_indices_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_ColumnTransformer_output_indices_ = bridgeColumnTransformer[${this.id}].output_indices_`
// convert the result from python to node.js
return this
._py`attr_ColumnTransformer_output_indices_.tolist() if hasattr(attr_ColumnTransformer_output_indices_, 'tolist') else attr_ColumnTransformer_output_indices_`
})()
}
/**
Number of features seen during [fit](../../glossary.html#term-fit). Only defined if the underlying transformers expose such an attribute when fit.
*/
get n_features_in_(): Promise<number> {
if (this._isDisposed) {
throw new Error(
'This ColumnTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'ColumnTransformer must call init() before accessing n_features_in_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_ColumnTransformer_n_features_in_ = bridgeColumnTransformer[${this.id}].n_features_in_`
// convert the result from python to node.js
return this
._py`attr_ColumnTransformer_n_features_in_.tolist() if hasattr(attr_ColumnTransformer_n_features_in_, 'tolist') else attr_ColumnTransformer_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 ColumnTransformer instance has already been disposed'
)
}
if (!this._isInitialized) {
throw new Error(
'ColumnTransformer must call init() before accessing feature_names_in_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_ColumnTransformer_feature_names_in_ = bridgeColumnTransformer[${this.id}].feature_names_in_`
// convert the result from python to node.js
return this
._py`attr_ColumnTransformer_feature_names_in_.tolist() if hasattr(attr_ColumnTransformer_feature_names_in_, 'tolist') else attr_ColumnTransformer_feature_names_in_`
})()
}
}