-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
SGDClassifier.ts
1009 lines (800 loc) · 36 KB
/
SGDClassifier.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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'
/**
Linear classifiers (SVM, logistic regression, etc.) with SGD training.
This estimator implements regularized linear models with stochastic gradient descent (SGD) learning: the gradient of the loss is estimated each sample at a time and the model is updated along the way with a decreasing strength schedule (aka learning rate). SGD allows minibatch (online/out-of-core) learning via the `partial\_fit` method. For best results using the default learning rate schedule, the data should have zero mean and unit variance.
This implementation works with data represented as dense or sparse arrays of floating point values for the features. The model it fits can be controlled with the loss parameter; by default, it fits a linear support vector machine (SVM).
The regularizer is a penalty added to the loss function that shrinks model parameters towards the zero vector using either the squared euclidean norm L2 or the absolute norm L1 or a combination of both (Elastic Net). If the parameter update crosses the 0.0 value because of the regularizer, the update is truncated to 0.0 to allow for learning sparse models and achieve online feature selection.
Read more in the [User Guide](../sgd.html#sgd).
[Python Reference](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html)
*/
export class SGDClassifier {
id: string
opts: any
_py: PythonBridge
_isInitialized: boolean = false
_isDisposed: boolean = false
constructor(opts?: {
/**
The loss function to be used.
@defaultValue `'hinge'`
*/
loss?:
| 'hinge'
| 'log_loss'
| 'modified_huber'
| 'squared_hinge'
| 'perceptron'
| 'squared_error'
| 'huber'
| 'epsilon_insensitive'
| 'squared_epsilon_insensitive'
/**
The penalty (aka regularization term) to be used. Defaults to ‘l2’ which is the standard regularizer for linear SVM models. ‘l1’ and ‘elasticnet’ might bring sparsity to the model (feature selection) not achievable with ‘l2’. No penalty is added when set to `undefined`.
@defaultValue `'l2'`
*/
penalty?: 'l2' | 'l1' | 'elasticnet'
/**
Constant that multiplies the regularization term. The higher the value, the stronger the regularization. Also used to compute the learning rate when `learning\_rate` is set to ‘optimal’. Values must be in the range `\[0.0, inf)`.
@defaultValue `0.0001`
*/
alpha?: number
/**
The Elastic Net mixing parameter, with 0 <= l1\_ratio <= 1. l1\_ratio=0 corresponds to L2 penalty, l1\_ratio=1 to L1. Only used if `penalty` is ‘elasticnet’. Values must be in the range `\[0.0, 1.0\]`.
@defaultValue `0.15`
*/
l1_ratio?: number
/**
Whether the intercept should be estimated or not. If `false`, the data is assumed to be already centered.
@defaultValue `true`
*/
fit_intercept?: boolean
/**
The maximum number of passes over the training data (aka epochs). It only impacts the behavior in the `fit` method, and not the [`partial\_fit`](#sklearn.linear_model.SGDClassifier.partial_fit "sklearn.linear_model.SGDClassifier.partial_fit") method. Values must be in the range `\[1, inf)`.
@defaultValue `1000`
*/
max_iter?: number
/**
The stopping criterion. If it is not `undefined`, training will stop when (loss > best\_loss - tol) for `n\_iter\_no\_change` consecutive epochs. Convergence is checked against the training loss or the validation loss depending on the `early\_stopping` parameter. Values must be in the range `\[0.0, inf)`.
@defaultValue `0.001`
*/
tol?: number
/**
Whether or not the training data should be shuffled after each epoch.
@defaultValue `true`
*/
shuffle?: boolean
/**
The verbosity level. Values must be in the range `\[0, inf)`.
@defaultValue `0`
*/
verbose?: number
/**
Epsilon in the epsilon-insensitive loss functions; only if `loss` is ‘huber’, ‘epsilon\_insensitive’, or ‘squared\_epsilon\_insensitive’. For ‘huber’, determines the threshold at which it becomes less important to get the prediction exactly right. For epsilon-insensitive, any differences between the current prediction and the correct label are ignored if they are less than this threshold. Values must be in the range `\[0.0, inf)`.
@defaultValue `0.1`
*/
epsilon?: number
/**
The number of CPUs to use to do the OVA (One Versus All, for multi-class problems) computation. `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
/**
Used for shuffling the data, when `shuffle` is set to `true`. Pass an int for reproducible output across multiple function calls. See [Glossary](../../glossary.html#term-random_state). Integer values must be in the range `\[0, 2\*\*32 \- 1\]`.
*/
random_state?: number
/**
The learning rate schedule:
@defaultValue `'optimal'`
*/
learning_rate?: string
/**
The initial learning rate for the ‘constant’, ‘invscaling’ or ‘adaptive’ schedules. The default value is 0.0 as eta0 is not used by the default schedule ‘optimal’. Values must be in the range `(0.0, inf)`.
@defaultValue `0`
*/
eta0?: number
/**
The exponent for inverse scaling learning rate \[default 0.5\]. Values must be in the range `(-inf, inf)`.
@defaultValue `0.5`
*/
power_t?: number
/**
Whether to use early stopping to terminate training when validation score is not improving. If set to `true`, it will automatically set aside a stratified fraction of training data as validation and terminate training when validation score returned by the `score` method is not improving by at least tol for n\_iter\_no\_change consecutive epochs.
@defaultValue `false`
*/
early_stopping?: boolean
/**
The proportion of training data to set aside as validation set for early stopping. Must be between 0 and 1. Only used if `early\_stopping` is `true`. Values must be in the range `(0.0, 1.0)`.
@defaultValue `0.1`
*/
validation_fraction?: number
/**
Number of iterations with no improvement to wait before stopping fitting. Convergence is checked against the training loss or the validation loss depending on the `early\_stopping` parameter. Integer values must be in the range `\[1, max\_iter)`.
@defaultValue `5`
*/
n_iter_no_change?: number
/**
Preset for the class\_weight fit parameter.
Weights associated with classes. If not given, all classes are supposed to have weight one.
The “balanced” mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as `n\_samples / (n\_classes \* np.bincount(y))`.
*/
class_weight?: any | 'balanced'
/**
When set to `true`, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. See [the Glossary](../../glossary.html#term-warm_start).
Repeatedly calling fit or partial\_fit when warm\_start is `true` can result in a different solution than when calling fit a single time because of the way the data is shuffled. If a dynamic learning rate is used, the learning rate is adapted depending on the number of samples already seen. Calling `fit` resets this counter, while `partial\_fit` will result in increasing the existing counter.
@defaultValue `false`
*/
warm_start?: boolean
/**
When set to `true`, computes the averaged SGD weights across all updates and stores the result in the `coef\_` attribute. If set to an int greater than 1, averaging will begin once the total number of samples seen reaches `average`. So `average=10` will begin averaging after seeing 10 samples. Integer values must be in the range `\[1, n\_samples\]`.
@defaultValue `false`
*/
average?: boolean | number
}) {
this.id = `SGDClassifier${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 SGDClassifier instance has already been disposed')
}
if (this._isInitialized) {
return
}
if (!py) {
throw new Error('SGDClassifier.init requires a PythonBridge instance')
}
this._py = py
await this._py.ex`
import numpy as np
from sklearn.linear_model import SGDClassifier
try: bridgeSGDClassifier
except NameError: bridgeSGDClassifier = {}
`
// set up constructor params
await this._py.ex`ctor_SGDClassifier = {'loss': ${
this.opts['loss'] ?? undefined
}, 'penalty': ${this.opts['penalty'] ?? undefined}, 'alpha': ${
this.opts['alpha'] ?? undefined
}, 'l1_ratio': ${this.opts['l1_ratio'] ?? undefined}, 'fit_intercept': ${
this.opts['fit_intercept'] ?? undefined
}, 'max_iter': ${this.opts['max_iter'] ?? undefined}, 'tol': ${
this.opts['tol'] ?? undefined
}, 'shuffle': ${this.opts['shuffle'] ?? undefined}, 'verbose': ${
this.opts['verbose'] ?? undefined
}, 'epsilon': ${this.opts['epsilon'] ?? undefined}, 'n_jobs': ${
this.opts['n_jobs'] ?? undefined
}, 'random_state': ${
this.opts['random_state'] ?? undefined
}, 'learning_rate': ${this.opts['learning_rate'] ?? undefined}, 'eta0': ${
this.opts['eta0'] ?? undefined
}, 'power_t': ${this.opts['power_t'] ?? undefined}, 'early_stopping': ${
this.opts['early_stopping'] ?? undefined
}, 'validation_fraction': ${
this.opts['validation_fraction'] ?? undefined
}, 'n_iter_no_change': ${
this.opts['n_iter_no_change'] ?? undefined
}, 'class_weight': ${
this.opts['class_weight'] ?? undefined
}, 'warm_start': ${this.opts['warm_start'] ?? undefined}, 'average': ${
this.opts['average'] ?? undefined
}}
ctor_SGDClassifier = {k: v for k, v in ctor_SGDClassifier.items() if v is not None}`
await this._py
.ex`bridgeSGDClassifier[${this.id}] = SGDClassifier(**ctor_SGDClassifier)`
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 bridgeSGDClassifier[${this.id}]`
this._isDisposed = true
}
/**
Predict confidence scores for samples.
The confidence score for a sample is proportional to the signed distance of that sample to the hyperplane.
*/
async decision_function(opts: {
/**
The data matrix for which we want to get the confidence scores.
*/
X?: ArrayLike | SparseMatrix[]
}): Promise<NDArray> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'SGDClassifier must call init() before decision_function()'
)
}
// set up method params
await this._py.ex`pms_SGDClassifier_decision_function = {'X': np.array(${
opts['X'] ?? undefined
}) if ${opts['X'] !== undefined} else None}
pms_SGDClassifier_decision_function = {k: v for k, v in pms_SGDClassifier_decision_function.items() if v is not None}`
// invoke method
await this._py
.ex`res_SGDClassifier_decision_function = bridgeSGDClassifier[${this.id}].decision_function(**pms_SGDClassifier_decision_function)`
// convert the result from python to node.js
return this
._py`res_SGDClassifier_decision_function.tolist() if hasattr(res_SGDClassifier_decision_function, 'tolist') else res_SGDClassifier_decision_function`
}
/**
Convert coefficient matrix to dense array format.
Converts the `coef\_` member (back) to a numpy.ndarray. This is the default format of `coef\_` and is required for fitting, so calling this method is only required on models that have previously been sparsified; otherwise, it is a no-op.
*/
async densify(opts: {}): Promise<any> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('SGDClassifier must call init() before densify()')
}
// set up method params
await this._py.ex`pms_SGDClassifier_densify = {}
pms_SGDClassifier_densify = {k: v for k, v in pms_SGDClassifier_densify.items() if v is not None}`
// invoke method
await this._py
.ex`res_SGDClassifier_densify = bridgeSGDClassifier[${this.id}].densify(**pms_SGDClassifier_densify)`
// convert the result from python to node.js
return this
._py`res_SGDClassifier_densify.tolist() if hasattr(res_SGDClassifier_densify, 'tolist') else res_SGDClassifier_densify`
}
/**
Fit linear model with Stochastic Gradient Descent.
*/
async fit(opts: {
/**
Training data.
*/
X?: ArrayLike | SparseMatrix
/**
Target values.
*/
y?: NDArray
/**
The initial coefficients to warm-start the optimization.
*/
coef_init?: NDArray[]
/**
The initial intercept to warm-start the optimization.
*/
intercept_init?: NDArray
/**
Weights applied to individual samples. If not provided, uniform weights are assumed. These weights will be multiplied with class\_weight (passed through the constructor) if class\_weight is specified.
*/
sample_weight?: ArrayLike
}): Promise<any> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('SGDClassifier must call init() before fit()')
}
// set up method params
await this._py.ex`pms_SGDClassifier_fit = {'X': ${
opts['X'] ?? undefined
}, 'y': np.array(${opts['y'] ?? undefined}) if ${
opts['y'] !== undefined
} else None, 'coef_init': np.array(${opts['coef_init'] ?? undefined}) if ${
opts['coef_init'] !== undefined
} else None, 'intercept_init': np.array(${
opts['intercept_init'] ?? undefined
}) if ${opts['intercept_init'] !== undefined} else None, 'sample_weight': ${
opts['sample_weight'] ?? undefined
}}
pms_SGDClassifier_fit = {k: v for k, v in pms_SGDClassifier_fit.items() if v is not None}`
// invoke method
await this._py
.ex`res_SGDClassifier_fit = bridgeSGDClassifier[${this.id}].fit(**pms_SGDClassifier_fit)`
// convert the result from python to node.js
return this
._py`res_SGDClassifier_fit.tolist() if hasattr(res_SGDClassifier_fit, 'tolist') else res_SGDClassifier_fit`
}
/**
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 SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'SGDClassifier must call init() before get_metadata_routing()'
)
}
// set up method params
await this._py.ex`pms_SGDClassifier_get_metadata_routing = {'routing': ${
opts['routing'] ?? undefined
}}
pms_SGDClassifier_get_metadata_routing = {k: v for k, v in pms_SGDClassifier_get_metadata_routing.items() if v is not None}`
// invoke method
await this._py
.ex`res_SGDClassifier_get_metadata_routing = bridgeSGDClassifier[${this.id}].get_metadata_routing(**pms_SGDClassifier_get_metadata_routing)`
// convert the result from python to node.js
return this
._py`res_SGDClassifier_get_metadata_routing.tolist() if hasattr(res_SGDClassifier_get_metadata_routing, 'tolist') else res_SGDClassifier_get_metadata_routing`
}
/**
Perform one epoch of stochastic gradient descent on given samples.
Internally, this method uses `max\_iter \= 1`. Therefore, it is not guaranteed that a minimum of the cost function is reached after calling it once. Matters such as objective convergence, early stopping, and learning rate adjustments should be handled by the user.
*/
async partial_fit(opts: {
/**
Subset of the training data.
*/
X?: ArrayLike | SparseMatrix
/**
Subset of the target values.
*/
y?: NDArray
/**
Classes across all calls to partial\_fit. Can be obtained by via `np.unique(y\_all)`, where y\_all is the target vector of the entire dataset. This argument is required for the first call to partial\_fit and can be omitted in the subsequent calls. Note that y doesn’t need to contain all labels in `classes`.
*/
classes?: NDArray
/**
Weights applied to individual samples. If not provided, uniform weights are assumed.
*/
sample_weight?: ArrayLike
}): Promise<any> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('SGDClassifier must call init() before partial_fit()')
}
// set up method params
await this._py.ex`pms_SGDClassifier_partial_fit = {'X': ${
opts['X'] ?? undefined
}, 'y': np.array(${opts['y'] ?? undefined}) if ${
opts['y'] !== undefined
} else None, 'classes': np.array(${opts['classes'] ?? undefined}) if ${
opts['classes'] !== undefined
} else None, 'sample_weight': ${opts['sample_weight'] ?? undefined}}
pms_SGDClassifier_partial_fit = {k: v for k, v in pms_SGDClassifier_partial_fit.items() if v is not None}`
// invoke method
await this._py
.ex`res_SGDClassifier_partial_fit = bridgeSGDClassifier[${this.id}].partial_fit(**pms_SGDClassifier_partial_fit)`
// convert the result from python to node.js
return this
._py`res_SGDClassifier_partial_fit.tolist() if hasattr(res_SGDClassifier_partial_fit, 'tolist') else res_SGDClassifier_partial_fit`
}
/**
Predict class labels for samples in X.
*/
async predict(opts: {
/**
The data matrix for which we want to get the predictions.
*/
X?: ArrayLike | SparseMatrix[]
}): Promise<NDArray> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('SGDClassifier must call init() before predict()')
}
// set up method params
await this._py.ex`pms_SGDClassifier_predict = {'X': np.array(${
opts['X'] ?? undefined
}) if ${opts['X'] !== undefined} else None}
pms_SGDClassifier_predict = {k: v for k, v in pms_SGDClassifier_predict.items() if v is not None}`
// invoke method
await this._py
.ex`res_SGDClassifier_predict = bridgeSGDClassifier[${this.id}].predict(**pms_SGDClassifier_predict)`
// convert the result from python to node.js
return this
._py`res_SGDClassifier_predict.tolist() if hasattr(res_SGDClassifier_predict, 'tolist') else res_SGDClassifier_predict`
}
/**
Log of probability estimates.
This method is only available for log loss and modified Huber loss.
When loss=”modified\_huber”, probability estimates may be hard zeros and ones, so taking the logarithm is not possible.
See `predict\_proba` for details.
*/
async predict_log_proba(opts: {
/**
Input data for prediction.
*/
X?: ArrayLike | SparseMatrix[]
}): Promise<ArrayLike> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'SGDClassifier must call init() before predict_log_proba()'
)
}
// set up method params
await this._py.ex`pms_SGDClassifier_predict_log_proba = {'X': np.array(${
opts['X'] ?? undefined
}) if ${opts['X'] !== undefined} else None}
pms_SGDClassifier_predict_log_proba = {k: v for k, v in pms_SGDClassifier_predict_log_proba.items() if v is not None}`
// invoke method
await this._py
.ex`res_SGDClassifier_predict_log_proba = bridgeSGDClassifier[${this.id}].predict_log_proba(**pms_SGDClassifier_predict_log_proba)`
// convert the result from python to node.js
return this
._py`res_SGDClassifier_predict_log_proba.tolist() if hasattr(res_SGDClassifier_predict_log_proba, 'tolist') else res_SGDClassifier_predict_log_proba`
}
/**
Probability estimates.
This method is only available for log loss and modified Huber loss.
Multiclass probability estimates are derived from binary (one-vs.-rest) estimates by simple normalization, as recommended by Zadrozny and Elkan.
Binary probability estimates for loss=”modified\_huber” are given by (clip(decision\_function(X), -1, 1) + 1) / 2. For other loss functions it is necessary to perform proper probability calibration by wrapping the classifier with [`CalibratedClassifierCV`](sklearn.calibration.CalibratedClassifierCV.html#sklearn.calibration.CalibratedClassifierCV "sklearn.calibration.CalibratedClassifierCV") instead.
*/
async predict_proba(opts: {
/**
Input data for prediction.
*/
X?: ArrayLike | SparseMatrix
}): Promise<any> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('SGDClassifier must call init() before predict_proba()')
}
// set up method params
await this._py.ex`pms_SGDClassifier_predict_proba = {'X': ${
opts['X'] ?? undefined
}}
pms_SGDClassifier_predict_proba = {k: v for k, v in pms_SGDClassifier_predict_proba.items() if v is not None}`
// invoke method
await this._py
.ex`res_SGDClassifier_predict_proba = bridgeSGDClassifier[${this.id}].predict_proba(**pms_SGDClassifier_predict_proba)`
// convert the result from python to node.js
return this
._py`res_SGDClassifier_predict_proba.tolist() if hasattr(res_SGDClassifier_predict_proba, 'tolist') else res_SGDClassifier_predict_proba`
}
/**
Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
*/
async score(opts: {
/**
Test samples.
*/
X?: ArrayLike[]
/**
True labels for `X`.
*/
y?: ArrayLike
/**
Sample weights.
*/
sample_weight?: ArrayLike
}): Promise<number> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('SGDClassifier must call init() before score()')
}
// set up method params
await this._py.ex`pms_SGDClassifier_score = {'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}
pms_SGDClassifier_score = {k: v for k, v in pms_SGDClassifier_score.items() if v is not None}`
// invoke method
await this._py
.ex`res_SGDClassifier_score = bridgeSGDClassifier[${this.id}].score(**pms_SGDClassifier_score)`
// convert the result from python to node.js
return this
._py`res_SGDClassifier_score.tolist() if hasattr(res_SGDClassifier_score, 'tolist') else res_SGDClassifier_score`
}
/**
Request metadata passed to the `fit` method.
Note that this method is only relevant if `enable\_metadata\_routing=True` (see [`sklearn.set\_config`](sklearn.set_config.html#sklearn.set_config "sklearn.set_config")). Please see [User Guide](../../metadata_routing.html#metadata-routing) on how the routing mechanism works.
The options for each parameter are:
*/
async set_fit_request(opts: {
/**
Metadata routing for `coef\_init` parameter in `fit`.
*/
coef_init?: string | boolean
/**
Metadata routing for `intercept\_init` parameter in `fit`.
*/
intercept_init?: string | boolean
/**
Metadata routing for `sample\_weight` parameter in `fit`.
*/
sample_weight?: string | boolean
}): Promise<any> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('SGDClassifier must call init() before set_fit_request()')
}
// set up method params
await this._py.ex`pms_SGDClassifier_set_fit_request = {'coef_init': ${
opts['coef_init'] ?? undefined
}, 'intercept_init': ${
opts['intercept_init'] ?? undefined
}, 'sample_weight': ${opts['sample_weight'] ?? undefined}}
pms_SGDClassifier_set_fit_request = {k: v for k, v in pms_SGDClassifier_set_fit_request.items() if v is not None}`
// invoke method
await this._py
.ex`res_SGDClassifier_set_fit_request = bridgeSGDClassifier[${this.id}].set_fit_request(**pms_SGDClassifier_set_fit_request)`
// convert the result from python to node.js
return this
._py`res_SGDClassifier_set_fit_request.tolist() if hasattr(res_SGDClassifier_set_fit_request, 'tolist') else res_SGDClassifier_set_fit_request`
}
/**
Request metadata passed to the `partial\_fit` method.
Note that this method is only relevant if `enable\_metadata\_routing=True` (see [`sklearn.set\_config`](sklearn.set_config.html#sklearn.set_config "sklearn.set_config")). Please see [User Guide](../../metadata_routing.html#metadata-routing) on how the routing mechanism works.
The options for each parameter are:
*/
async set_partial_fit_request(opts: {
/**
Metadata routing for `classes` parameter in `partial\_fit`.
*/
classes?: string | boolean
/**
Metadata routing for `sample\_weight` parameter in `partial\_fit`.
*/
sample_weight?: string | boolean
}): Promise<any> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'SGDClassifier must call init() before set_partial_fit_request()'
)
}
// set up method params
await this._py.ex`pms_SGDClassifier_set_partial_fit_request = {'classes': ${
opts['classes'] ?? undefined
}, 'sample_weight': ${opts['sample_weight'] ?? undefined}}
pms_SGDClassifier_set_partial_fit_request = {k: v for k, v in pms_SGDClassifier_set_partial_fit_request.items() if v is not None}`
// invoke method
await this._py
.ex`res_SGDClassifier_set_partial_fit_request = bridgeSGDClassifier[${this.id}].set_partial_fit_request(**pms_SGDClassifier_set_partial_fit_request)`
// convert the result from python to node.js
return this
._py`res_SGDClassifier_set_partial_fit_request.tolist() if hasattr(res_SGDClassifier_set_partial_fit_request, 'tolist') else res_SGDClassifier_set_partial_fit_request`
}
/**
Request metadata passed to the `score` method.
Note that this method is only relevant if `enable\_metadata\_routing=True` (see [`sklearn.set\_config`](sklearn.set_config.html#sklearn.set_config "sklearn.set_config")). Please see [User Guide](../../metadata_routing.html#metadata-routing) on how the routing mechanism works.
The options for each parameter are:
*/
async set_score_request(opts: {
/**
Metadata routing for `sample\_weight` parameter in `score`.
*/
sample_weight?: string | boolean
}): Promise<any> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'SGDClassifier must call init() before set_score_request()'
)
}
// set up method params
await this._py.ex`pms_SGDClassifier_set_score_request = {'sample_weight': ${
opts['sample_weight'] ?? undefined
}}
pms_SGDClassifier_set_score_request = {k: v for k, v in pms_SGDClassifier_set_score_request.items() if v is not None}`
// invoke method
await this._py
.ex`res_SGDClassifier_set_score_request = bridgeSGDClassifier[${this.id}].set_score_request(**pms_SGDClassifier_set_score_request)`
// convert the result from python to node.js
return this
._py`res_SGDClassifier_set_score_request.tolist() if hasattr(res_SGDClassifier_set_score_request, 'tolist') else res_SGDClassifier_set_score_request`
}
/**
Convert coefficient matrix to sparse format.
Converts the `coef\_` member to a scipy.sparse matrix, which for L1-regularized models can be much more memory- and storage-efficient than the usual numpy.ndarray representation.
The `intercept\_` member is not converted.
*/
async sparsify(opts: {}): Promise<any> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('SGDClassifier must call init() before sparsify()')
}
// set up method params
await this._py.ex`pms_SGDClassifier_sparsify = {}
pms_SGDClassifier_sparsify = {k: v for k, v in pms_SGDClassifier_sparsify.items() if v is not None}`
// invoke method
await this._py
.ex`res_SGDClassifier_sparsify = bridgeSGDClassifier[${this.id}].sparsify(**pms_SGDClassifier_sparsify)`
// convert the result from python to node.js
return this
._py`res_SGDClassifier_sparsify.tolist() if hasattr(res_SGDClassifier_sparsify, 'tolist') else res_SGDClassifier_sparsify`
}
/**
Weights assigned to the features.
*/
get coef_(): Promise<NDArray[][]> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('SGDClassifier must call init() before accessing coef_')
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_SGDClassifier_coef_ = bridgeSGDClassifier[${this.id}].coef_`
// convert the result from python to node.js
return this
._py`attr_SGDClassifier_coef_.tolist() if hasattr(attr_SGDClassifier_coef_, 'tolist') else attr_SGDClassifier_coef_`
})()
}
/**
Constants in decision function.
*/
get intercept_(): Promise<NDArray[]> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'SGDClassifier must call init() before accessing intercept_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_SGDClassifier_intercept_ = bridgeSGDClassifier[${this.id}].intercept_`
// convert the result from python to node.js
return this
._py`attr_SGDClassifier_intercept_.tolist() if hasattr(attr_SGDClassifier_intercept_, 'tolist') else attr_SGDClassifier_intercept_`
})()
}
/**
The actual number of iterations before reaching the stopping criterion. For multiclass fits, it is the maximum over every binary fit.
*/
get n_iter_(): Promise<number> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('SGDClassifier must call init() before accessing n_iter_')
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_SGDClassifier_n_iter_ = bridgeSGDClassifier[${this.id}].n_iter_`
// convert the result from python to node.js
return this
._py`attr_SGDClassifier_n_iter_.tolist() if hasattr(attr_SGDClassifier_n_iter_, 'tolist') else attr_SGDClassifier_n_iter_`
})()
}
get loss_function_(): Promise<any> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'SGDClassifier must call init() before accessing loss_function_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_SGDClassifier_loss_function_ = bridgeSGDClassifier[${this.id}].loss_function_`
// convert the result from python to node.js
return this
._py`attr_SGDClassifier_loss_function_.tolist() if hasattr(attr_SGDClassifier_loss_function_, 'tolist') else attr_SGDClassifier_loss_function_`
})()
}
get classes_(): Promise<any[]> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'SGDClassifier must call init() before accessing classes_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_SGDClassifier_classes_ = bridgeSGDClassifier[${this.id}].classes_`
// convert the result from python to node.js
return this
._py`attr_SGDClassifier_classes_.tolist() if hasattr(attr_SGDClassifier_classes_, 'tolist') else attr_SGDClassifier_classes_`
})()
}
/**
Number of weight updates performed during training. Same as `(n\_iter\_ \* n\_samples + 1)`.
*/
get t_(): Promise<number> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('SGDClassifier must call init() before accessing t_')
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_SGDClassifier_t_ = bridgeSGDClassifier[${this.id}].t_`
// convert the result from python to node.js
return this
._py`attr_SGDClassifier_t_.tolist() if hasattr(attr_SGDClassifier_t_, 'tolist') else attr_SGDClassifier_t_`
})()
}
/**
Number of features seen during [fit](../../glossary.html#term-fit).
*/
get n_features_in_(): Promise<number> {
if (this._isDisposed) {
throw new Error('This SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'SGDClassifier must call init() before accessing n_features_in_'
)
}
return (async () => {
// invoke accessor
await this._py
.ex`attr_SGDClassifier_n_features_in_ = bridgeSGDClassifier[${this.id}].n_features_in_`
// convert the result from python to node.js
return this
._py`attr_SGDClassifier_n_features_in_.tolist() if hasattr(attr_SGDClassifier_n_features_in_, 'tolist') else attr_SGDClassifier_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 SGDClassifier instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error(
'SGDClassifier must call init() before accessing feature_names_in_'
)
}
return (async () => {
// invoke accessor