deepbox 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +344 -0
- package/dist/CSRMatrix-CwGwQRea.d.cts +219 -0
- package/dist/CSRMatrix-KzNt6QpS.d.ts +219 -0
- package/dist/Tensor-BQLk1ltW.d.cts +147 -0
- package/dist/Tensor-g8mUClel.d.ts +147 -0
- package/dist/chunk-4S73VUBD.js +677 -0
- package/dist/chunk-4S73VUBD.js.map +1 -0
- package/dist/chunk-5R4S63PF.js +2925 -0
- package/dist/chunk-5R4S63PF.js.map +1 -0
- package/dist/chunk-6AE5FKKQ.cjs +9264 -0
- package/dist/chunk-6AE5FKKQ.cjs.map +1 -0
- package/dist/chunk-AD436M45.js +3854 -0
- package/dist/chunk-AD436M45.js.map +1 -0
- package/dist/chunk-ALS7ETWZ.cjs +4263 -0
- package/dist/chunk-ALS7ETWZ.cjs.map +1 -0
- package/dist/chunk-AU7XHGKJ.js +2092 -0
- package/dist/chunk-AU7XHGKJ.js.map +1 -0
- package/dist/chunk-B5TNKUEY.js +1481 -0
- package/dist/chunk-B5TNKUEY.js.map +1 -0
- package/dist/chunk-BCR7G3A6.js +9136 -0
- package/dist/chunk-BCR7G3A6.js.map +1 -0
- package/dist/chunk-C4PKXY74.cjs +1917 -0
- package/dist/chunk-C4PKXY74.cjs.map +1 -0
- package/dist/chunk-DWZY6PIP.cjs +6400 -0
- package/dist/chunk-DWZY6PIP.cjs.map +1 -0
- package/dist/chunk-E3EU5FZO.cjs +2113 -0
- package/dist/chunk-E3EU5FZO.cjs.map +1 -0
- package/dist/chunk-F3JWBINJ.js +1054 -0
- package/dist/chunk-F3JWBINJ.js.map +1 -0
- package/dist/chunk-FJYLIGJX.js +1940 -0
- package/dist/chunk-FJYLIGJX.js.map +1 -0
- package/dist/chunk-JSCDE774.cjs +729 -0
- package/dist/chunk-JSCDE774.cjs.map +1 -0
- package/dist/chunk-LWECRCW2.cjs +2412 -0
- package/dist/chunk-LWECRCW2.cjs.map +1 -0
- package/dist/chunk-MLBMYKCG.js +6379 -0
- package/dist/chunk-MLBMYKCG.js.map +1 -0
- package/dist/chunk-OX6QXFMV.cjs +3874 -0
- package/dist/chunk-OX6QXFMV.cjs.map +1 -0
- package/dist/chunk-PHV2DKRS.cjs +1072 -0
- package/dist/chunk-PHV2DKRS.cjs.map +1 -0
- package/dist/chunk-PL7TAYKI.js +4056 -0
- package/dist/chunk-PL7TAYKI.js.map +1 -0
- package/dist/chunk-PR647I7R.js +1898 -0
- package/dist/chunk-PR647I7R.js.map +1 -0
- package/dist/chunk-QERHVCHC.cjs +2960 -0
- package/dist/chunk-QERHVCHC.cjs.map +1 -0
- package/dist/chunk-XEG44RF6.cjs +1514 -0
- package/dist/chunk-XEG44RF6.cjs.map +1 -0
- package/dist/chunk-XMWVME2W.js +2377 -0
- package/dist/chunk-XMWVME2W.js.map +1 -0
- package/dist/chunk-ZB75FESB.cjs +1979 -0
- package/dist/chunk-ZB75FESB.cjs.map +1 -0
- package/dist/chunk-ZLW62TJG.cjs +4061 -0
- package/dist/chunk-ZLW62TJG.cjs.map +1 -0
- package/dist/chunk-ZXKBDFP3.js +4235 -0
- package/dist/chunk-ZXKBDFP3.js.map +1 -0
- package/dist/core/index.cjs +204 -0
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.cts +2 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +3 -0
- package/dist/core/index.js.map +1 -0
- package/dist/dataframe/index.cjs +22 -0
- package/dist/dataframe/index.cjs.map +1 -0
- package/dist/dataframe/index.d.cts +3 -0
- package/dist/dataframe/index.d.ts +3 -0
- package/dist/dataframe/index.js +5 -0
- package/dist/dataframe/index.js.map +1 -0
- package/dist/datasets/index.cjs +134 -0
- package/dist/datasets/index.cjs.map +1 -0
- package/dist/datasets/index.d.cts +3 -0
- package/dist/datasets/index.d.ts +3 -0
- package/dist/datasets/index.js +5 -0
- package/dist/datasets/index.js.map +1 -0
- package/dist/index-74AB8Cyh.d.cts +1126 -0
- package/dist/index-9oQx1HgV.d.cts +1180 -0
- package/dist/index-BJY2SI4i.d.ts +483 -0
- package/dist/index-BWGhrDlr.d.ts +733 -0
- package/dist/index-B_DK4FKY.d.cts +242 -0
- package/dist/index-BbA2Gxfl.d.ts +456 -0
- package/dist/index-BgHYAoSS.d.cts +837 -0
- package/dist/index-BndMbqsM.d.ts +1439 -0
- package/dist/index-C1mfVYoo.d.ts +2517 -0
- package/dist/index-CCvlwAmL.d.cts +809 -0
- package/dist/index-CDw5CnOU.d.ts +785 -0
- package/dist/index-Cn3SdB0O.d.ts +1126 -0
- package/dist/index-CrqLlS-a.d.ts +776 -0
- package/dist/index-D61yaSMY.d.cts +483 -0
- package/dist/index-D9Loo1_A.d.cts +2517 -0
- package/dist/index-DIT_OO9C.d.cts +785 -0
- package/dist/index-DIp_RrRt.d.ts +242 -0
- package/dist/index-DbultU6X.d.cts +1427 -0
- package/dist/index-DmEg_LCm.d.cts +776 -0
- package/dist/index-DoPWVxPo.d.cts +1439 -0
- package/dist/index-DuCxd-8d.d.ts +837 -0
- package/dist/index-Dx42TZaY.d.ts +809 -0
- package/dist/index-DyZ4QQf5.d.cts +456 -0
- package/dist/index-GFAVyOWO.d.ts +1427 -0
- package/dist/index-WHQLn0e8.d.cts +733 -0
- package/dist/index-ZtI1Iy4L.d.ts +1180 -0
- package/dist/index-eJgeni9c.d.cts +1911 -0
- package/dist/index-tk4lSYod.d.ts +1911 -0
- package/dist/index.cjs +72 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +17 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/linalg/index.cjs +86 -0
- package/dist/linalg/index.cjs.map +1 -0
- package/dist/linalg/index.d.cts +3 -0
- package/dist/linalg/index.d.ts +3 -0
- package/dist/linalg/index.js +5 -0
- package/dist/linalg/index.js.map +1 -0
- package/dist/metrics/index.cjs +158 -0
- package/dist/metrics/index.cjs.map +1 -0
- package/dist/metrics/index.d.cts +3 -0
- package/dist/metrics/index.d.ts +3 -0
- package/dist/metrics/index.js +5 -0
- package/dist/metrics/index.js.map +1 -0
- package/dist/ml/index.cjs +87 -0
- package/dist/ml/index.cjs.map +1 -0
- package/dist/ml/index.d.cts +3 -0
- package/dist/ml/index.d.ts +3 -0
- package/dist/ml/index.js +6 -0
- package/dist/ml/index.js.map +1 -0
- package/dist/ndarray/index.cjs +501 -0
- package/dist/ndarray/index.cjs.map +1 -0
- package/dist/ndarray/index.d.cts +5 -0
- package/dist/ndarray/index.d.ts +5 -0
- package/dist/ndarray/index.js +4 -0
- package/dist/ndarray/index.js.map +1 -0
- package/dist/nn/index.cjs +142 -0
- package/dist/nn/index.cjs.map +1 -0
- package/dist/nn/index.d.cts +6 -0
- package/dist/nn/index.d.ts +6 -0
- package/dist/nn/index.js +5 -0
- package/dist/nn/index.js.map +1 -0
- package/dist/optim/index.cjs +77 -0
- package/dist/optim/index.cjs.map +1 -0
- package/dist/optim/index.d.cts +4 -0
- package/dist/optim/index.d.ts +4 -0
- package/dist/optim/index.js +4 -0
- package/dist/optim/index.js.map +1 -0
- package/dist/plot/index.cjs +114 -0
- package/dist/plot/index.cjs.map +1 -0
- package/dist/plot/index.d.cts +6 -0
- package/dist/plot/index.d.ts +6 -0
- package/dist/plot/index.js +5 -0
- package/dist/plot/index.js.map +1 -0
- package/dist/preprocess/index.cjs +82 -0
- package/dist/preprocess/index.cjs.map +1 -0
- package/dist/preprocess/index.d.cts +4 -0
- package/dist/preprocess/index.d.ts +4 -0
- package/dist/preprocess/index.js +5 -0
- package/dist/preprocess/index.js.map +1 -0
- package/dist/random/index.cjs +74 -0
- package/dist/random/index.cjs.map +1 -0
- package/dist/random/index.d.cts +3 -0
- package/dist/random/index.d.ts +3 -0
- package/dist/random/index.js +5 -0
- package/dist/random/index.js.map +1 -0
- package/dist/stats/index.cjs +142 -0
- package/dist/stats/index.cjs.map +1 -0
- package/dist/stats/index.d.cts +3 -0
- package/dist/stats/index.d.ts +3 -0
- package/dist/stats/index.js +5 -0
- package/dist/stats/index.js.map +1 -0
- package/dist/tensor-B96jjJLQ.d.cts +205 -0
- package/dist/tensor-B96jjJLQ.d.ts +205 -0
- package/package.json +226 -0
|
@@ -0,0 +1,2517 @@
|
|
|
1
|
+
import { T as Tensor } from './Tensor-BQLk1ltW.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Base type for all estimators (models) in Deepbox.
|
|
5
|
+
*
|
|
6
|
+
* This follows the scikit-learn estimator type design.
|
|
7
|
+
*
|
|
8
|
+
* @template FitParams - Type of parameters passed to fit method
|
|
9
|
+
*
|
|
10
|
+
* References:
|
|
11
|
+
* - scikit-learn API: https://scikit-learn.org/stable/developers/develop.html
|
|
12
|
+
*/
|
|
13
|
+
type Estimator<FitParams = void> = {
|
|
14
|
+
/**
|
|
15
|
+
* Fit the model to training data.
|
|
16
|
+
*
|
|
17
|
+
* @param X - Training features of shape (n_samples, n_features)
|
|
18
|
+
* @param y - Training targets (optional for unsupervised learning)
|
|
19
|
+
* @param params - Additional fitting parameters
|
|
20
|
+
* @returns The fitted estimator (for method chaining)
|
|
21
|
+
*/
|
|
22
|
+
fit(X: Tensor, y?: Tensor, params?: FitParams): Estimator<FitParams>;
|
|
23
|
+
/**
|
|
24
|
+
* Get parameters for this estimator.
|
|
25
|
+
*
|
|
26
|
+
* @returns Object containing all parameters
|
|
27
|
+
*/
|
|
28
|
+
getParams(): Record<string, unknown>;
|
|
29
|
+
/**
|
|
30
|
+
* Set parameters for this estimator.
|
|
31
|
+
*
|
|
32
|
+
* @param params - Parameters to set
|
|
33
|
+
* @returns The estimator (for method chaining)
|
|
34
|
+
*/
|
|
35
|
+
setParams(params: Record<string, unknown>): Estimator<FitParams>;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Type for classification models.
|
|
39
|
+
*
|
|
40
|
+
* Classifiers predict discrete class labels.
|
|
41
|
+
*/
|
|
42
|
+
type Classifier = Estimator & {
|
|
43
|
+
/**
|
|
44
|
+
* Fit the model to training data.
|
|
45
|
+
*
|
|
46
|
+
* @param X - Training features of shape (n_samples, n_features)
|
|
47
|
+
* @param y - Training targets
|
|
48
|
+
* @returns The fitted estimator
|
|
49
|
+
*/
|
|
50
|
+
fit(X: Tensor, y: Tensor): Classifier;
|
|
51
|
+
/**
|
|
52
|
+
* Predict class labels for samples in X.
|
|
53
|
+
*
|
|
54
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
55
|
+
* @returns Predicted class labels of shape (n_samples,)
|
|
56
|
+
*/
|
|
57
|
+
predict(X: Tensor): Tensor;
|
|
58
|
+
/**
|
|
59
|
+
* Predict class probabilities for samples in X.
|
|
60
|
+
*
|
|
61
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
62
|
+
* @returns Class probabilities of shape (n_samples, n_classes)
|
|
63
|
+
*/
|
|
64
|
+
predictProba(X: Tensor): Tensor;
|
|
65
|
+
/**
|
|
66
|
+
* Compute the mean accuracy on the given test data and labels.
|
|
67
|
+
*
|
|
68
|
+
* @param X - Test samples
|
|
69
|
+
* @param y - True labels
|
|
70
|
+
* @returns Mean accuracy score
|
|
71
|
+
*/
|
|
72
|
+
score(X: Tensor, y: Tensor): number;
|
|
73
|
+
/** Array of unique class labels seen during fit */
|
|
74
|
+
readonly classes?: Tensor | undefined;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Type for regression models.
|
|
78
|
+
*
|
|
79
|
+
* Regressors predict continuous values.
|
|
80
|
+
*/
|
|
81
|
+
type Regressor = Estimator & {
|
|
82
|
+
/**
|
|
83
|
+
* Fit the model to training data.
|
|
84
|
+
*
|
|
85
|
+
* @param X - Training features of shape (n_samples, n_features)
|
|
86
|
+
* @param y - Training targets
|
|
87
|
+
* @returns The fitted estimator
|
|
88
|
+
*/
|
|
89
|
+
fit(X: Tensor, y: Tensor): Regressor;
|
|
90
|
+
/**
|
|
91
|
+
* Predict target values for samples in X.
|
|
92
|
+
*
|
|
93
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
94
|
+
* @returns Predicted values of shape (n_samples,) or (n_samples, n_targets)
|
|
95
|
+
*/
|
|
96
|
+
predict(X: Tensor): Tensor;
|
|
97
|
+
/**
|
|
98
|
+
* Compute the coefficient of determination R^2 of the prediction.
|
|
99
|
+
*
|
|
100
|
+
* @param X - Test samples
|
|
101
|
+
* @param y - True target values
|
|
102
|
+
* @returns R^2 score
|
|
103
|
+
*/
|
|
104
|
+
score(X: Tensor, y: Tensor): number;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Type for clustering models.
|
|
108
|
+
*
|
|
109
|
+
* Clusterers group similar samples together.
|
|
110
|
+
*/
|
|
111
|
+
type Clusterer = Estimator<void> & {
|
|
112
|
+
/**
|
|
113
|
+
* Fit the model to training data.
|
|
114
|
+
*
|
|
115
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
116
|
+
* @param y - Ignored (exists for compatibility)
|
|
117
|
+
* @returns The fitted estimator
|
|
118
|
+
*/
|
|
119
|
+
fit(X: Tensor, y?: Tensor): Clusterer;
|
|
120
|
+
/**
|
|
121
|
+
* Predict cluster labels for samples in X.
|
|
122
|
+
*
|
|
123
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
124
|
+
* @returns Cluster labels of shape (n_samples,)
|
|
125
|
+
*/
|
|
126
|
+
predict(X: Tensor): Tensor;
|
|
127
|
+
/**
|
|
128
|
+
* Fit the model and predict cluster labels.
|
|
129
|
+
*
|
|
130
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
131
|
+
* @param y - Ignored (exists for compatibility)
|
|
132
|
+
* @returns Cluster labels of shape (n_samples,)
|
|
133
|
+
*/
|
|
134
|
+
fitPredict(X: Tensor, y?: Tensor): Tensor;
|
|
135
|
+
/** Cluster centers after fitting */
|
|
136
|
+
readonly clusterCenters?: Tensor;
|
|
137
|
+
/** Labels of each point after fitting */
|
|
138
|
+
readonly labels?: Tensor;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Type for transformer models.
|
|
142
|
+
*
|
|
143
|
+
* Transformers modify or transform the input data.
|
|
144
|
+
*/
|
|
145
|
+
type Transformer = Estimator<void> & {
|
|
146
|
+
/**
|
|
147
|
+
* Fit the model to training data.
|
|
148
|
+
*
|
|
149
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
150
|
+
* @param y - Ignored (exists for compatibility)
|
|
151
|
+
* @returns The fitted estimator
|
|
152
|
+
*/
|
|
153
|
+
fit(X: Tensor, y?: Tensor): Transformer;
|
|
154
|
+
/**
|
|
155
|
+
* Transform the input data.
|
|
156
|
+
*
|
|
157
|
+
* @param X - Data to transform
|
|
158
|
+
* @returns Transformed data
|
|
159
|
+
*/
|
|
160
|
+
transform(X: Tensor): Tensor;
|
|
161
|
+
/**
|
|
162
|
+
* Fit to data, then transform it.
|
|
163
|
+
*
|
|
164
|
+
* @param X - Training data
|
|
165
|
+
* @param y - Target values (optional)
|
|
166
|
+
* @returns Transformed data
|
|
167
|
+
*/
|
|
168
|
+
fitTransform(X: Tensor, y?: Tensor): Tensor;
|
|
169
|
+
/**
|
|
170
|
+
* Inverse transform the data back to original representation.
|
|
171
|
+
*
|
|
172
|
+
* @param X - Transformed data
|
|
173
|
+
* @returns Original representation
|
|
174
|
+
*/
|
|
175
|
+
inverseTransform?(X: Tensor): Tensor;
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* Type for outlier/anomaly detection models.
|
|
179
|
+
*/
|
|
180
|
+
type OutlierDetector = Estimator<void> & {
|
|
181
|
+
/**
|
|
182
|
+
* Fit the model to training data.
|
|
183
|
+
*
|
|
184
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
185
|
+
* @param y - Ignored (exists for compatibility)
|
|
186
|
+
* @returns The fitted estimator
|
|
187
|
+
*/
|
|
188
|
+
fit(X: Tensor, y?: Tensor): OutlierDetector;
|
|
189
|
+
/**
|
|
190
|
+
* Predict if samples are outliers or inliers.
|
|
191
|
+
*
|
|
192
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
193
|
+
* @returns Labels: +1 for inliers, -1 for outliers
|
|
194
|
+
*/
|
|
195
|
+
predict(X: Tensor): Tensor;
|
|
196
|
+
/**
|
|
197
|
+
* Fit the model and predict outliers.
|
|
198
|
+
*
|
|
199
|
+
* @param X - Training data
|
|
200
|
+
* @param y - Ignored
|
|
201
|
+
* @returns Labels: +1 for inliers, -1 for outliers
|
|
202
|
+
*/
|
|
203
|
+
fitPredict(X: Tensor, y?: Tensor): Tensor;
|
|
204
|
+
/**
|
|
205
|
+
* Compute anomaly scores for samples.
|
|
206
|
+
*
|
|
207
|
+
* @param X - Samples to score
|
|
208
|
+
* @returns Anomaly scores (lower = more abnormal)
|
|
209
|
+
*/
|
|
210
|
+
scoreSamples(X: Tensor): Tensor;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* DBSCAN (Density-Based Spatial Clustering of Applications with Noise).
|
|
215
|
+
*
|
|
216
|
+
* Clusters points based on density. Points in high-density regions are
|
|
217
|
+
* grouped together, while points in low-density regions are marked as noise.
|
|
218
|
+
*
|
|
219
|
+
* **Algorithm**:
|
|
220
|
+
* 1. For each point, find all neighbors within eps distance
|
|
221
|
+
* 2. If a point has at least minSamples neighbors, it's a core point
|
|
222
|
+
* 3. Core points and their neighbors form clusters
|
|
223
|
+
* 4. Points not reachable from any core point are noise (label = -1)
|
|
224
|
+
*
|
|
225
|
+
* **Advantages**:
|
|
226
|
+
* - No need to specify number of clusters
|
|
227
|
+
* - Can find arbitrarily shaped clusters
|
|
228
|
+
* - Robust to outliers
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```ts
|
|
232
|
+
* import { DBSCAN } from 'deepbox/ml';
|
|
233
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
234
|
+
*
|
|
235
|
+
* const X = tensor([[1, 2], [2, 2], [2, 3], [8, 7], [8, 8], [25, 80]]);
|
|
236
|
+
* const dbscan = new DBSCAN({ eps: 3, minSamples: 2 });
|
|
237
|
+
* const labels = dbscan.fitPredict(X);
|
|
238
|
+
* // labels: [0, 0, 0, 1, 1, -1] (-1 = noise)
|
|
239
|
+
* ```
|
|
240
|
+
*
|
|
241
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html | scikit-learn DBSCAN}
|
|
242
|
+
*/
|
|
243
|
+
declare class DBSCAN implements Clusterer {
|
|
244
|
+
private eps;
|
|
245
|
+
private minSamples;
|
|
246
|
+
private metric;
|
|
247
|
+
private labels_?;
|
|
248
|
+
private coreIndices_?;
|
|
249
|
+
private fitted;
|
|
250
|
+
constructor(options?: {
|
|
251
|
+
readonly eps?: number;
|
|
252
|
+
readonly minSamples?: number;
|
|
253
|
+
readonly metric?: "euclidean" | "manhattan";
|
|
254
|
+
});
|
|
255
|
+
/**
|
|
256
|
+
* Check if two points are neighbors (distance <= eps).
|
|
257
|
+
*/
|
|
258
|
+
private isNeighbor;
|
|
259
|
+
/**
|
|
260
|
+
* Find all neighbors within eps distance.
|
|
261
|
+
*/
|
|
262
|
+
private getNeighbors;
|
|
263
|
+
/**
|
|
264
|
+
* Perform DBSCAN clustering on data X.
|
|
265
|
+
*
|
|
266
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
267
|
+
* @param _y - Ignored (exists for API compatibility)
|
|
268
|
+
* @returns this - The fitted estimator
|
|
269
|
+
* @throws {ShapeError} If X is not 2D
|
|
270
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
271
|
+
*/
|
|
272
|
+
fit(X: Tensor, _y?: Tensor): this;
|
|
273
|
+
/**
|
|
274
|
+
* Predict cluster labels for samples in X.
|
|
275
|
+
*
|
|
276
|
+
* @param _X - Samples (unused)
|
|
277
|
+
* @throws {NotImplementedError} Always — DBSCAN is transductive and does not support prediction on new data
|
|
278
|
+
*/
|
|
279
|
+
predict(_X: Tensor): Tensor;
|
|
280
|
+
/**
|
|
281
|
+
* Fit DBSCAN and return cluster labels.
|
|
282
|
+
*
|
|
283
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
284
|
+
* @param _y - Ignored (exists for API compatibility)
|
|
285
|
+
* @returns Cluster labels of shape (n_samples,). Noise points are labeled -1.
|
|
286
|
+
* @throws {ShapeError} If X is not 2D
|
|
287
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
288
|
+
* @throws {NotFittedError} If fit did not produce labels (internal error)
|
|
289
|
+
*/
|
|
290
|
+
fitPredict(X: Tensor, _y?: Tensor): Tensor;
|
|
291
|
+
/**
|
|
292
|
+
* Get cluster labels assigned during fitting.
|
|
293
|
+
*
|
|
294
|
+
* @returns Tensor of cluster labels. Noise points are labeled -1.
|
|
295
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
296
|
+
*/
|
|
297
|
+
get labels(): Tensor;
|
|
298
|
+
/**
|
|
299
|
+
* Get indices of core samples discovered during fitting.
|
|
300
|
+
*
|
|
301
|
+
* Core samples are points with at least `minSamples` neighbors within `eps`.
|
|
302
|
+
*
|
|
303
|
+
* @returns Array of core sample indices
|
|
304
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
305
|
+
*/
|
|
306
|
+
get coreIndices(): number[];
|
|
307
|
+
/**
|
|
308
|
+
* Get hyperparameters for this estimator.
|
|
309
|
+
*
|
|
310
|
+
* @returns Object containing all hyperparameters
|
|
311
|
+
*/
|
|
312
|
+
getParams(): Record<string, unknown>;
|
|
313
|
+
/**
|
|
314
|
+
* Set the parameters of this estimator.
|
|
315
|
+
*
|
|
316
|
+
* @param params - Parameters to set (eps, minSamples, metric)
|
|
317
|
+
* @returns this
|
|
318
|
+
* @throws {InvalidParameterError} If any parameter value is invalid
|
|
319
|
+
*/
|
|
320
|
+
setParams(params: Record<string, unknown>): this;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* K-Means clustering algorithm.
|
|
325
|
+
*
|
|
326
|
+
* Partitions n samples into k clusters by minimizing the within-cluster
|
|
327
|
+
* sum of squared distances to cluster centroids.
|
|
328
|
+
*
|
|
329
|
+
* **Algorithm**: Lloyd's algorithm (iterative refinement)
|
|
330
|
+
* 1. Initialize k centroids (random or k-means++)
|
|
331
|
+
* 2. Assign each point to nearest centroid
|
|
332
|
+
* 3. Update centroids as mean of assigned points
|
|
333
|
+
* 4. Repeat until convergence or max iterations
|
|
334
|
+
*
|
|
335
|
+
* **Time Complexity**: O(n * k * i * d) where:
|
|
336
|
+
* - n = number of samples
|
|
337
|
+
* - k = number of clusters
|
|
338
|
+
* - i = number of iterations
|
|
339
|
+
* - d = number of features
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
* ```ts
|
|
343
|
+
* import { KMeans } from 'deepbox/ml';
|
|
344
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
345
|
+
*
|
|
346
|
+
* const X = tensor([[1, 2], [1.5, 1.8], [5, 8], [8, 8], [1, 0.6], [9, 11]]);
|
|
347
|
+
* const kmeans = new KMeans({ nClusters: 2, randomState: 42 });
|
|
348
|
+
* kmeans.fit(X);
|
|
349
|
+
*
|
|
350
|
+
* const labels = kmeans.predict(X);
|
|
351
|
+
* console.log('Cluster labels:', labels);
|
|
352
|
+
* console.log('Centroids:', kmeans.clusterCenters);
|
|
353
|
+
* ```
|
|
354
|
+
*
|
|
355
|
+
* @see {@link https://en.wikipedia.org/wiki/K-means_clustering | Wikipedia: K-means}
|
|
356
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html | scikit-learn KMeans}
|
|
357
|
+
*/
|
|
358
|
+
declare class KMeans implements Clusterer {
|
|
359
|
+
private nClusters;
|
|
360
|
+
private maxIter;
|
|
361
|
+
private tol;
|
|
362
|
+
private init;
|
|
363
|
+
private randomState;
|
|
364
|
+
private clusterCenters_?;
|
|
365
|
+
private labels_?;
|
|
366
|
+
private inertia_?;
|
|
367
|
+
private nIter_?;
|
|
368
|
+
private nFeaturesIn_?;
|
|
369
|
+
private fitted;
|
|
370
|
+
/**
|
|
371
|
+
* Create a new K-Means clustering model.
|
|
372
|
+
*
|
|
373
|
+
* @param options - Configuration options
|
|
374
|
+
* @param options.nClusters - Number of clusters (default: 8)
|
|
375
|
+
* @param options.maxIter - Maximum number of iterations (default: 300)
|
|
376
|
+
* @param options.tol - Tolerance for convergence (default: 1e-4)
|
|
377
|
+
* @param options.init - Initialization method: 'random' or 'kmeans++' (default: 'kmeans++')
|
|
378
|
+
* @param options.randomState - Random seed for reproducibility
|
|
379
|
+
*/
|
|
380
|
+
constructor(options?: {
|
|
381
|
+
readonly nClusters?: number;
|
|
382
|
+
readonly maxIter?: number;
|
|
383
|
+
readonly tol?: number;
|
|
384
|
+
readonly init?: "random" | "kmeans++";
|
|
385
|
+
readonly randomState?: number;
|
|
386
|
+
});
|
|
387
|
+
/**
|
|
388
|
+
* Fit K-Means clustering on training data.
|
|
389
|
+
*
|
|
390
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
391
|
+
* @param y - Ignored (exists for compatibility)
|
|
392
|
+
* @returns this - The fitted estimator
|
|
393
|
+
*/
|
|
394
|
+
fit(X: Tensor, _y?: Tensor): this;
|
|
395
|
+
/**
|
|
396
|
+
* Predict cluster labels for samples.
|
|
397
|
+
*
|
|
398
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
399
|
+
* @returns Cluster labels of shape (n_samples,)
|
|
400
|
+
*/
|
|
401
|
+
predict(X: Tensor): Tensor;
|
|
402
|
+
/**
|
|
403
|
+
* Fit and predict in one step.
|
|
404
|
+
*
|
|
405
|
+
* @param X - Training data
|
|
406
|
+
* @param y - Ignored (exists for compatibility)
|
|
407
|
+
* @returns Cluster labels
|
|
408
|
+
*/
|
|
409
|
+
fitPredict(X: Tensor, _y?: Tensor): Tensor;
|
|
410
|
+
/**
|
|
411
|
+
* Initialize centroids using specified method.
|
|
412
|
+
*/
|
|
413
|
+
private initializeCentroids;
|
|
414
|
+
/**
|
|
415
|
+
* Assign each sample to nearest centroid.
|
|
416
|
+
*/
|
|
417
|
+
private assignClusters;
|
|
418
|
+
/**
|
|
419
|
+
* Calculate inertia (sum of squared distances to centroids).
|
|
420
|
+
*/
|
|
421
|
+
private calculateInertia;
|
|
422
|
+
/**
|
|
423
|
+
* Create a simple RNG for reproducibility.
|
|
424
|
+
*/
|
|
425
|
+
private createRNG;
|
|
426
|
+
/**
|
|
427
|
+
* Get cluster centers.
|
|
428
|
+
*/
|
|
429
|
+
get clusterCenters(): Tensor;
|
|
430
|
+
/**
|
|
431
|
+
* Get training labels.
|
|
432
|
+
*/
|
|
433
|
+
get labels(): Tensor;
|
|
434
|
+
/**
|
|
435
|
+
* Get inertia (sum of squared distances to centroids).
|
|
436
|
+
*/
|
|
437
|
+
get inertia(): number;
|
|
438
|
+
/**
|
|
439
|
+
* Get number of iterations run.
|
|
440
|
+
*/
|
|
441
|
+
get nIter(): number;
|
|
442
|
+
/**
|
|
443
|
+
* Get hyperparameters for this estimator.
|
|
444
|
+
*
|
|
445
|
+
* @returns Object containing all hyperparameters
|
|
446
|
+
*/
|
|
447
|
+
getParams(): Record<string, unknown>;
|
|
448
|
+
/**
|
|
449
|
+
* Set the parameters of this estimator.
|
|
450
|
+
*
|
|
451
|
+
* @param params - Parameters to set (nClusters, maxIter, tol, init, randomState)
|
|
452
|
+
* @returns this
|
|
453
|
+
* @throws {InvalidParameterError} If any parameter value is invalid
|
|
454
|
+
*/
|
|
455
|
+
setParams(params: Record<string, unknown>): this;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Principal Component Analysis (PCA).
|
|
460
|
+
*
|
|
461
|
+
* Linear dimensionality reduction using Singular Value Decomposition (SVD)
|
|
462
|
+
* to project data to a lower dimensional space.
|
|
463
|
+
*
|
|
464
|
+
* **Algorithm**:
|
|
465
|
+
* 1. Center the data by subtracting the mean
|
|
466
|
+
* 2. Compute SVD: X = U * Σ * V^T
|
|
467
|
+
* 3. Principal components are columns of V
|
|
468
|
+
* 4. Transform data by projecting onto principal components
|
|
469
|
+
*
|
|
470
|
+
* **Time Complexity**: O(min(n*d^2, d*n^2)) where n=samples, d=features
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* ```ts
|
|
474
|
+
* import { PCA } from 'deepbox/ml';
|
|
475
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
476
|
+
*
|
|
477
|
+
* const X = tensor([[2.5, 2.4], [0.5, 0.7], [2.2, 2.9], [1.9, 2.2], [3.1, 3.0]]);
|
|
478
|
+
* const pca = new PCA({ nComponents: 1 });
|
|
479
|
+
* pca.fit(X);
|
|
480
|
+
*
|
|
481
|
+
* const XTransformed = pca.transform(X);
|
|
482
|
+
* console.log('Explained variance ratio:', pca.explainedVarianceRatio);
|
|
483
|
+
* ```
|
|
484
|
+
*
|
|
485
|
+
* @see {@link https://en.wikipedia.org/wiki/Principal_component_analysis | Wikipedia: PCA}
|
|
486
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html | scikit-learn PCA}
|
|
487
|
+
*/
|
|
488
|
+
declare class PCA implements Transformer {
|
|
489
|
+
private readonly nComponents?;
|
|
490
|
+
private readonly whiten;
|
|
491
|
+
private components_?;
|
|
492
|
+
private explainedVariance_?;
|
|
493
|
+
private explainedVarianceRatio_?;
|
|
494
|
+
private mean_?;
|
|
495
|
+
private nComponentsActual_?;
|
|
496
|
+
private nFeaturesIn_?;
|
|
497
|
+
private fitted;
|
|
498
|
+
/**
|
|
499
|
+
* Create a new PCA model.
|
|
500
|
+
*
|
|
501
|
+
* @param options - Configuration options
|
|
502
|
+
* @param options.nComponents - Number of components to keep (default: min(n_samples, n_features))
|
|
503
|
+
* @param options.whiten - Whether to whiten the data (default: false)
|
|
504
|
+
*/
|
|
505
|
+
constructor(options?: {
|
|
506
|
+
readonly nComponents?: number;
|
|
507
|
+
readonly whiten?: boolean;
|
|
508
|
+
});
|
|
509
|
+
/**
|
|
510
|
+
* Fit PCA on training data.
|
|
511
|
+
*
|
|
512
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
513
|
+
* @param y - Ignored (exists for compatibility)
|
|
514
|
+
* @returns this
|
|
515
|
+
*/
|
|
516
|
+
fit(X: Tensor, _y?: Tensor): this;
|
|
517
|
+
/**
|
|
518
|
+
* Transform data to principal component space.
|
|
519
|
+
*
|
|
520
|
+
* @param X - Data of shape (n_samples, n_features)
|
|
521
|
+
* @returns Transformed data of shape (n_samples, n_components)
|
|
522
|
+
*/
|
|
523
|
+
transform(X: Tensor): Tensor;
|
|
524
|
+
/**
|
|
525
|
+
* Fit and transform in one step.
|
|
526
|
+
*
|
|
527
|
+
* @param X - Training data
|
|
528
|
+
* @param y - Ignored (exists for compatibility)
|
|
529
|
+
* @returns Transformed data
|
|
530
|
+
*/
|
|
531
|
+
fitTransform(X: Tensor, _y?: Tensor): Tensor;
|
|
532
|
+
/**
|
|
533
|
+
* Transform data back to original space.
|
|
534
|
+
*
|
|
535
|
+
* @param X - Transformed data of shape (n_samples, n_components)
|
|
536
|
+
* @returns Reconstructed data of shape (n_samples, n_features)
|
|
537
|
+
*/
|
|
538
|
+
inverseTransform(X: Tensor): Tensor;
|
|
539
|
+
/**
|
|
540
|
+
* Center data by subtracting mean.
|
|
541
|
+
*/
|
|
542
|
+
private centerData;
|
|
543
|
+
/**
|
|
544
|
+
* Get principal components.
|
|
545
|
+
*/
|
|
546
|
+
get components(): Tensor;
|
|
547
|
+
/**
|
|
548
|
+
* Get explained variance.
|
|
549
|
+
*/
|
|
550
|
+
get explainedVariance(): Tensor;
|
|
551
|
+
/**
|
|
552
|
+
* Get explained variance ratio.
|
|
553
|
+
*/
|
|
554
|
+
get explainedVarianceRatio(): Tensor;
|
|
555
|
+
/**
|
|
556
|
+
* Get hyperparameters for this estimator.
|
|
557
|
+
*
|
|
558
|
+
* @returns Object containing all hyperparameters
|
|
559
|
+
*/
|
|
560
|
+
getParams(): Record<string, unknown>;
|
|
561
|
+
/**
|
|
562
|
+
* Set the parameters of this estimator.
|
|
563
|
+
*
|
|
564
|
+
* @param _params - Parameters to set
|
|
565
|
+
* @throws {NotImplementedError} Always — parameters cannot be changed after construction
|
|
566
|
+
*/
|
|
567
|
+
setParams(_params: Record<string, unknown>): this;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Gradient Boosting Regressor.
|
|
572
|
+
*
|
|
573
|
+
* Builds an additive model in a forward stage-wise fashion using
|
|
574
|
+
* regression trees as weak learners. Optimizes squared error loss.
|
|
575
|
+
*
|
|
576
|
+
* **Algorithm**: Gradient Boosting with regression trees
|
|
577
|
+
* - Stage-wise additive modeling
|
|
578
|
+
* - Uses gradient of squared loss (residuals)
|
|
579
|
+
*
|
|
580
|
+
* @example
|
|
581
|
+
* ```ts
|
|
582
|
+
* import { GradientBoostingRegressor } from 'deepbox/ml';
|
|
583
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
584
|
+
*
|
|
585
|
+
* const X = tensor([[1], [2], [3], [4], [5]]);
|
|
586
|
+
* const y = tensor([1.2, 2.1, 2.9, 4.0, 5.1]);
|
|
587
|
+
*
|
|
588
|
+
* const gbr = new GradientBoostingRegressor({ nEstimators: 100 });
|
|
589
|
+
* gbr.fit(X, y);
|
|
590
|
+
* const predictions = gbr.predict(X);
|
|
591
|
+
* ```
|
|
592
|
+
*
|
|
593
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingRegressor.html | scikit-learn GradientBoostingRegressor}
|
|
594
|
+
*/
|
|
595
|
+
declare class GradientBoostingRegressor implements Regressor {
|
|
596
|
+
/** Number of boosting stages (trees) */
|
|
597
|
+
private nEstimators;
|
|
598
|
+
/** Learning rate shrinks the contribution of each tree */
|
|
599
|
+
private learningRate;
|
|
600
|
+
/** Maximum depth of individual regression trees */
|
|
601
|
+
private maxDepth;
|
|
602
|
+
/** Minimum samples required to split */
|
|
603
|
+
private minSamplesSplit;
|
|
604
|
+
/** Array of weak learners (regression trees) */
|
|
605
|
+
private estimators;
|
|
606
|
+
/** Initial prediction (mean of targets) */
|
|
607
|
+
private initPrediction;
|
|
608
|
+
/** Number of features */
|
|
609
|
+
private nFeatures;
|
|
610
|
+
/** Whether the model has been fitted */
|
|
611
|
+
private fitted;
|
|
612
|
+
constructor(options?: {
|
|
613
|
+
readonly nEstimators?: number;
|
|
614
|
+
readonly learningRate?: number;
|
|
615
|
+
readonly maxDepth?: number;
|
|
616
|
+
readonly minSamplesSplit?: number;
|
|
617
|
+
});
|
|
618
|
+
/**
|
|
619
|
+
* Fit the gradient boosting regressor on training data.
|
|
620
|
+
*
|
|
621
|
+
* Builds an additive model by sequentially fitting regression trees
|
|
622
|
+
* to the negative gradient (residuals) of the loss function.
|
|
623
|
+
*
|
|
624
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
625
|
+
* @param y - Target values of shape (n_samples,)
|
|
626
|
+
* @returns this - The fitted estimator
|
|
627
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
628
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
629
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
630
|
+
*/
|
|
631
|
+
fit(X: Tensor, y: Tensor): this;
|
|
632
|
+
/**
|
|
633
|
+
* Predict target values for samples in X.
|
|
634
|
+
*
|
|
635
|
+
* Aggregates the initial prediction and the scaled contributions of all trees.
|
|
636
|
+
*
|
|
637
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
638
|
+
* @returns Predicted values of shape (n_samples,)
|
|
639
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
640
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
641
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
642
|
+
*/
|
|
643
|
+
predict(X: Tensor): Tensor;
|
|
644
|
+
/**
|
|
645
|
+
* Return the R² score on the given test data and target values.
|
|
646
|
+
*
|
|
647
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
648
|
+
* @param y - True target values of shape (n_samples,)
|
|
649
|
+
* @returns R² score (best possible is 1.0, can be negative)
|
|
650
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
651
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
652
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
653
|
+
*/
|
|
654
|
+
score(X: Tensor, y: Tensor): number;
|
|
655
|
+
/**
|
|
656
|
+
* Get hyperparameters for this estimator.
|
|
657
|
+
*
|
|
658
|
+
* @returns Object containing all hyperparameters
|
|
659
|
+
*/
|
|
660
|
+
getParams(): Record<string, unknown>;
|
|
661
|
+
/**
|
|
662
|
+
* Set the parameters of this estimator.
|
|
663
|
+
*
|
|
664
|
+
* @param _params - Parameters to set
|
|
665
|
+
* @throws {NotImplementedError} Always — parameters cannot be changed after construction
|
|
666
|
+
*/
|
|
667
|
+
setParams(_params: Record<string, unknown>): this;
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* Gradient Boosting Classifier.
|
|
671
|
+
*
|
|
672
|
+
* Uses gradient boosting with shallow regression trees for binary classification.
|
|
673
|
+
* Optimizes log loss (cross-entropy) using sigmoid function.
|
|
674
|
+
*
|
|
675
|
+
* @example
|
|
676
|
+
* ```ts
|
|
677
|
+
* import { GradientBoostingClassifier } from 'deepbox/ml';
|
|
678
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
679
|
+
*
|
|
680
|
+
* const X = tensor([[1, 2], [2, 3], [3, 1], [4, 2]]);
|
|
681
|
+
* const y = tensor([0, 0, 1, 1]);
|
|
682
|
+
*
|
|
683
|
+
* const gbc = new GradientBoostingClassifier({ nEstimators: 100 });
|
|
684
|
+
* gbc.fit(X, y);
|
|
685
|
+
* const predictions = gbc.predict(X);
|
|
686
|
+
* ```
|
|
687
|
+
*
|
|
688
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingClassifier.html | scikit-learn GradientBoostingClassifier}
|
|
689
|
+
*/
|
|
690
|
+
declare class GradientBoostingClassifier implements Classifier {
|
|
691
|
+
/** Number of boosting stages */
|
|
692
|
+
private nEstimators;
|
|
693
|
+
/** Learning rate */
|
|
694
|
+
private learningRate;
|
|
695
|
+
/** Maximum depth */
|
|
696
|
+
private maxDepth;
|
|
697
|
+
/** Minimum samples to split */
|
|
698
|
+
private minSamplesSplit;
|
|
699
|
+
/** Array of weak learners */
|
|
700
|
+
private estimators;
|
|
701
|
+
/** Initial log-odds prediction */
|
|
702
|
+
private initPrediction;
|
|
703
|
+
/** Number of features */
|
|
704
|
+
private nFeatures;
|
|
705
|
+
/** Unique class labels */
|
|
706
|
+
private classLabels;
|
|
707
|
+
/** Whether fitted */
|
|
708
|
+
private fitted;
|
|
709
|
+
constructor(options?: {
|
|
710
|
+
readonly nEstimators?: number;
|
|
711
|
+
readonly learningRate?: number;
|
|
712
|
+
readonly maxDepth?: number;
|
|
713
|
+
readonly minSamplesSplit?: number;
|
|
714
|
+
});
|
|
715
|
+
/**
|
|
716
|
+
* Fit the gradient boosting classifier on training data.
|
|
717
|
+
*
|
|
718
|
+
* Builds an additive model by sequentially fitting regression trees
|
|
719
|
+
* to the pseudo-residuals (gradient of log loss).
|
|
720
|
+
*
|
|
721
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
722
|
+
* @param y - Target class labels of shape (n_samples,). Must contain exactly 2 classes.
|
|
723
|
+
* @returns this - The fitted estimator
|
|
724
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
725
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
726
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
727
|
+
* @throws {InvalidParameterError} If y does not contain exactly 2 classes
|
|
728
|
+
*/
|
|
729
|
+
fit(X: Tensor, y: Tensor): this;
|
|
730
|
+
/**
|
|
731
|
+
* Predict class labels for samples in X.
|
|
732
|
+
*
|
|
733
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
734
|
+
* @returns Predicted class labels of shape (n_samples,)
|
|
735
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
736
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
737
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
738
|
+
*/
|
|
739
|
+
predict(X: Tensor): Tensor;
|
|
740
|
+
/**
|
|
741
|
+
* Predict class probabilities for samples in X.
|
|
742
|
+
*
|
|
743
|
+
* Returns a matrix of shape (n_samples, 2) where columns are
|
|
744
|
+
* [P(class_0), P(class_1)].
|
|
745
|
+
*
|
|
746
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
747
|
+
* @returns Class probability matrix of shape (n_samples, 2)
|
|
748
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
749
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
750
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
751
|
+
*/
|
|
752
|
+
predictProba(X: Tensor): Tensor;
|
|
753
|
+
/**
|
|
754
|
+
* Return the mean accuracy on the given test data and labels.
|
|
755
|
+
*
|
|
756
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
757
|
+
* @param y - True labels of shape (n_samples,)
|
|
758
|
+
* @returns Accuracy score in range [0, 1]
|
|
759
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
760
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
761
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
762
|
+
*/
|
|
763
|
+
score(X: Tensor, y: Tensor): number;
|
|
764
|
+
/**
|
|
765
|
+
* Get hyperparameters for this estimator.
|
|
766
|
+
*
|
|
767
|
+
* @returns Object containing all hyperparameters
|
|
768
|
+
*/
|
|
769
|
+
getParams(): Record<string, unknown>;
|
|
770
|
+
/**
|
|
771
|
+
* Set the parameters of this estimator.
|
|
772
|
+
*
|
|
773
|
+
* @param _params - Parameters to set
|
|
774
|
+
* @throws {NotImplementedError} Always — parameters cannot be changed after construction
|
|
775
|
+
*/
|
|
776
|
+
setParams(_params: Record<string, unknown>): this;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Lasso Regression (L1 Regularized Linear Regression).
|
|
781
|
+
*
|
|
782
|
+
* Lasso performs both regularization and feature selection by adding
|
|
783
|
+
* an L1 penalty that can drive coefficients exactly to zero.
|
|
784
|
+
*
|
|
785
|
+
* @example
|
|
786
|
+
* ```ts
|
|
787
|
+
* import { Lasso } from 'deepbox/ml';
|
|
788
|
+
*
|
|
789
|
+
* const model = new Lasso({ alpha: 0.1, maxIter: 1000 });
|
|
790
|
+
* model.fit(X_train, y_train);
|
|
791
|
+
*
|
|
792
|
+
* // Many coefficients will be exactly 0
|
|
793
|
+
* console.log(model.coef);
|
|
794
|
+
*
|
|
795
|
+
* const predictions = model.predict(X_test);
|
|
796
|
+
* ```
|
|
797
|
+
*
|
|
798
|
+
* @category Linear Models
|
|
799
|
+
* @implements {Regressor}
|
|
800
|
+
*/
|
|
801
|
+
declare class Lasso implements Regressor {
|
|
802
|
+
/** Configuration options for the Lasso regression model */
|
|
803
|
+
private options;
|
|
804
|
+
/** Model coefficients (weights) after fitting - shape (n_features,) */
|
|
805
|
+
private coef_?;
|
|
806
|
+
/** Intercept (bias) term after fitting */
|
|
807
|
+
private intercept_;
|
|
808
|
+
/** Number of features seen during fit - used for validation */
|
|
809
|
+
private nFeaturesIn_?;
|
|
810
|
+
/** Number of iterations run by coordinate descent */
|
|
811
|
+
private nIter_;
|
|
812
|
+
/** Whether the model has been fitted to data */
|
|
813
|
+
private fitted;
|
|
814
|
+
/**
|
|
815
|
+
* Create a new Lasso Regression model.
|
|
816
|
+
*
|
|
817
|
+
* @param options - Configuration options
|
|
818
|
+
* @param options.alpha - Regularization strength (default: 1.0). Must be >= 0. Controls sparsity of solution.
|
|
819
|
+
* @param options.fitIntercept - Whether to calculate the intercept (default: true)
|
|
820
|
+
* @param options.normalize - Whether to normalize features before regression (default: false)
|
|
821
|
+
* @param options.maxIter - Maximum iterations for coordinate descent (default: 1000)
|
|
822
|
+
* @param options.tol - Tolerance for convergence (default: 1e-4). Smaller = more precise but slower.
|
|
823
|
+
* @param options.warmStart - Whether to reuse previous solution as initialization (default: false)
|
|
824
|
+
* @param options.positive - Whether to force coefficients to be positive (default: false)
|
|
825
|
+
* @param options.selection - Coordinate selection: 'cyclic' (default) or 'random'
|
|
826
|
+
*/
|
|
827
|
+
constructor(options?: {
|
|
828
|
+
readonly alpha?: number;
|
|
829
|
+
readonly fitIntercept?: boolean;
|
|
830
|
+
readonly normalize?: boolean;
|
|
831
|
+
readonly maxIter?: number;
|
|
832
|
+
readonly tol?: number;
|
|
833
|
+
readonly warmStart?: boolean;
|
|
834
|
+
readonly positive?: boolean;
|
|
835
|
+
readonly selection?: "cyclic" | "random";
|
|
836
|
+
readonly randomState?: number;
|
|
837
|
+
});
|
|
838
|
+
private createRNG;
|
|
839
|
+
/**
|
|
840
|
+
* Fit Lasso regression model using Coordinate Descent.
|
|
841
|
+
*
|
|
842
|
+
* Solves the L1-regularized least squares problem:
|
|
843
|
+
* minimize (1/(2*n)) ||y - Xw||² + α||w||₁
|
|
844
|
+
*
|
|
845
|
+
* **Algorithm**: Coordinate Descent with Soft Thresholding
|
|
846
|
+
* 1. Initialize coefficients (warm start if enabled)
|
|
847
|
+
* 2. For each iteration:
|
|
848
|
+
* - For each feature (cyclic or random order):
|
|
849
|
+
* - Compute residual correlation
|
|
850
|
+
* - Apply soft thresholding operator
|
|
851
|
+
* - Update predictions incrementally
|
|
852
|
+
* 3. Check convergence based on coefficient changes
|
|
853
|
+
*
|
|
854
|
+
* **Time Complexity**: O(k * n * p) where k = iterations, n = samples, p = features
|
|
855
|
+
* **Space Complexity**: O(n + p)
|
|
856
|
+
*
|
|
857
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
858
|
+
* @param y - Target values of shape (n_samples,)
|
|
859
|
+
* @returns this - The fitted estimator for method chaining
|
|
860
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
861
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
862
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
863
|
+
* @throws {DataValidationError} If X or y are empty
|
|
864
|
+
* @throws {InvalidParameterError} If alpha < 0
|
|
865
|
+
*/
|
|
866
|
+
fit(X: Tensor, y: Tensor): this;
|
|
867
|
+
/**
|
|
868
|
+
* Soft thresholding operator (proximal operator for L1 norm).
|
|
869
|
+
*
|
|
870
|
+
* This is the key operation in Lasso that induces sparsity.
|
|
871
|
+
*
|
|
872
|
+
* Formula:
|
|
873
|
+
* - If x > λ: return x - λ
|
|
874
|
+
* - If x < -λ: return x + λ
|
|
875
|
+
* - Otherwise: return 0
|
|
876
|
+
*
|
|
877
|
+
* Geometrically, this "shrinks" x towards zero by λ,
|
|
878
|
+
* and sets it exactly to zero if |x| ≤ λ.
|
|
879
|
+
*
|
|
880
|
+
* **Time Complexity**: O(1)
|
|
881
|
+
*
|
|
882
|
+
* @param x - Input value
|
|
883
|
+
* @param lambda - Threshold parameter (regularization strength)
|
|
884
|
+
* @returns Soft-thresholded value
|
|
885
|
+
*/
|
|
886
|
+
private softThreshold;
|
|
887
|
+
/**
|
|
888
|
+
* Get the model coefficients (weights).
|
|
889
|
+
*
|
|
890
|
+
* Many coefficients will be exactly zero due to L1 regularization (sparsity).
|
|
891
|
+
*
|
|
892
|
+
* @returns Coefficient tensor of shape (n_features,)
|
|
893
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
894
|
+
*/
|
|
895
|
+
get coef(): Tensor;
|
|
896
|
+
/**
|
|
897
|
+
* Get the intercept (bias term).
|
|
898
|
+
*
|
|
899
|
+
* @returns Intercept value
|
|
900
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
901
|
+
*/
|
|
902
|
+
get intercept(): number;
|
|
903
|
+
/**
|
|
904
|
+
* Get the number of iterations run by coordinate descent.
|
|
905
|
+
*
|
|
906
|
+
* @returns Number of iterations until convergence
|
|
907
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
908
|
+
*/
|
|
909
|
+
get nIter(): number | undefined;
|
|
910
|
+
/**
|
|
911
|
+
* Predict using the Lasso regression model.
|
|
912
|
+
*
|
|
913
|
+
* Computes predictions as: ŷ = X @ coef + intercept
|
|
914
|
+
*
|
|
915
|
+
* **Time Complexity**: O(nm) where n = samples, m = features
|
|
916
|
+
* **Space Complexity**: O(n)
|
|
917
|
+
*
|
|
918
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
919
|
+
* @returns Predicted values of shape (n_samples,)
|
|
920
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
921
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
922
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
923
|
+
*/
|
|
924
|
+
predict(X: Tensor): Tensor;
|
|
925
|
+
/**
|
|
926
|
+
* Return the R² score on the given test data and target values.
|
|
927
|
+
*
|
|
928
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
929
|
+
* @param y - True target values of shape (n_samples,)
|
|
930
|
+
* @returns R² score (best possible is 1.0, can be negative)
|
|
931
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
932
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
933
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
934
|
+
*/
|
|
935
|
+
score(X: Tensor, y: Tensor): number;
|
|
936
|
+
/**
|
|
937
|
+
* Get hyperparameters for this estimator.
|
|
938
|
+
*
|
|
939
|
+
* @returns Object containing all hyperparameters
|
|
940
|
+
*/
|
|
941
|
+
getParams(): Record<string, unknown>;
|
|
942
|
+
/**
|
|
943
|
+
* Set the parameters of this estimator.
|
|
944
|
+
*
|
|
945
|
+
* @param params - Parameters to set (alpha, maxIter, tol, fitIntercept, normalize)
|
|
946
|
+
* @returns this
|
|
947
|
+
* @throws {InvalidParameterError} If any parameter value is invalid
|
|
948
|
+
*/
|
|
949
|
+
setParams(params: Record<string, unknown>): this;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/**
|
|
953
|
+
* Ordinary Least Squares Linear Regression.
|
|
954
|
+
*
|
|
955
|
+
* Fits a linear model with coefficients w = (w1, ..., wp) to minimize
|
|
956
|
+
* the residual sum of squares between the observed targets and the
|
|
957
|
+
* targets predicted by the linear approximation.
|
|
958
|
+
*
|
|
959
|
+
* @example
|
|
960
|
+
* ```ts
|
|
961
|
+
* import { LinearRegression } from 'deepbox/ml';
|
|
962
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
963
|
+
*
|
|
964
|
+
* // Create training data
|
|
965
|
+
* const X = tensor([[1, 1], [1, 2], [2, 2], [2, 3]]);
|
|
966
|
+
* const y = tensor([1, 2, 2, 3]);
|
|
967
|
+
*
|
|
968
|
+
* // Fit model
|
|
969
|
+
* const model = new LinearRegression({ fitIntercept: true });
|
|
970
|
+
* model.fit(X, y);
|
|
971
|
+
*
|
|
972
|
+
* // Make predictions
|
|
973
|
+
* const X_test = tensor([[3, 5]]);
|
|
974
|
+
* const predictions = model.predict(X_test);
|
|
975
|
+
*
|
|
976
|
+
* // Get R^2 score
|
|
977
|
+
* const score = model.score(X, y);
|
|
978
|
+
* ```
|
|
979
|
+
*/
|
|
980
|
+
declare class LinearRegression implements Regressor {
|
|
981
|
+
/** Model coefficients (weights) of shape (n_features,) or (n_features, n_targets) */
|
|
982
|
+
private coef_?;
|
|
983
|
+
/** Independent term (bias/intercept) in the linear model */
|
|
984
|
+
private intercept_?;
|
|
985
|
+
/** Number of features seen during fit */
|
|
986
|
+
private nFeaturesIn_?;
|
|
987
|
+
/** Whether the model has been fitted */
|
|
988
|
+
private fitted;
|
|
989
|
+
private options;
|
|
990
|
+
/**
|
|
991
|
+
* Create a new Linear Regression model.
|
|
992
|
+
*
|
|
993
|
+
* @param options - Configuration options
|
|
994
|
+
* @param options.fitIntercept - Whether to calculate the intercept (default: true)
|
|
995
|
+
* @param options.normalize - Whether to normalize features before regression (default: false)
|
|
996
|
+
* @param options.copyX - Whether to copy X or overwrite it (default: true)
|
|
997
|
+
*/
|
|
998
|
+
constructor(options?: {
|
|
999
|
+
readonly fitIntercept?: boolean;
|
|
1000
|
+
readonly normalize?: boolean;
|
|
1001
|
+
readonly copyX?: boolean;
|
|
1002
|
+
});
|
|
1003
|
+
/**
|
|
1004
|
+
* Fit linear model using Ordinary Least Squares.
|
|
1005
|
+
*
|
|
1006
|
+
* Uses SVD-based least squares solver for numerical stability.
|
|
1007
|
+
* When fitIntercept is true, centers the data before fitting.
|
|
1008
|
+
*
|
|
1009
|
+
* **Algorithm Complexity**: O(n * p^2) where n = samples, p = features
|
|
1010
|
+
*
|
|
1011
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
1012
|
+
* @param y - Target values of shape (n_samples,). Multi-output regression is not currently supported.
|
|
1013
|
+
* @returns this - The fitted estimator
|
|
1014
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
1015
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
1016
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
1017
|
+
* @throws {DataValidationError} If X or y are empty
|
|
1018
|
+
*/
|
|
1019
|
+
fit(X: Tensor, y: Tensor): this;
|
|
1020
|
+
/**
|
|
1021
|
+
* Center data by subtracting the mean.
|
|
1022
|
+
*
|
|
1023
|
+
* @param data - Input tensor to center
|
|
1024
|
+
* @param dataMean - Mean tensor to subtract
|
|
1025
|
+
* @returns Centered tensor
|
|
1026
|
+
*/
|
|
1027
|
+
private centerData;
|
|
1028
|
+
private centerDataInPlace;
|
|
1029
|
+
private computeL2Norm;
|
|
1030
|
+
private scaleData;
|
|
1031
|
+
private scaleDataInPlace;
|
|
1032
|
+
private rescaleCoefs;
|
|
1033
|
+
/**
|
|
1034
|
+
* Predict using the linear model.
|
|
1035
|
+
*
|
|
1036
|
+
* Computes y_pred = X * coef_ + intercept_
|
|
1037
|
+
*
|
|
1038
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
1039
|
+
* @returns Predicted values of shape (n_samples,)
|
|
1040
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1041
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
1042
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
1043
|
+
*/
|
|
1044
|
+
predict(X: Tensor): Tensor;
|
|
1045
|
+
/**
|
|
1046
|
+
* Return the coefficient of determination R^2 of the prediction.
|
|
1047
|
+
*
|
|
1048
|
+
* R^2 = 1 - (SS_res / SS_tot)
|
|
1049
|
+
*
|
|
1050
|
+
* Where:
|
|
1051
|
+
* - SS_res = Σ(y_true - y_pred)^2 (residual sum of squares)
|
|
1052
|
+
* - SS_tot = Σ(y_true - y_mean)^2 (total sum of squares)
|
|
1053
|
+
*
|
|
1054
|
+
* Best possible score is 1.0, and it can be negative (worse than random).
|
|
1055
|
+
*
|
|
1056
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
1057
|
+
* @param y - True target values of shape (n_samples,)
|
|
1058
|
+
* @returns R² score (best possible is 1.0, can be negative)
|
|
1059
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1060
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
1061
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
1062
|
+
*/
|
|
1063
|
+
score(X: Tensor, y: Tensor): number;
|
|
1064
|
+
/**
|
|
1065
|
+
* Get the model coefficients (weights).
|
|
1066
|
+
*
|
|
1067
|
+
* @returns Coefficient tensor of shape (n_features,) or (n_features, n_targets)
|
|
1068
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1069
|
+
*/
|
|
1070
|
+
get coef(): Tensor;
|
|
1071
|
+
/**
|
|
1072
|
+
* Get the intercept (bias term).
|
|
1073
|
+
*
|
|
1074
|
+
* @returns Intercept value or tensor
|
|
1075
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1076
|
+
*/
|
|
1077
|
+
get intercept(): Tensor | undefined;
|
|
1078
|
+
/**
|
|
1079
|
+
* Get parameters for this estimator.
|
|
1080
|
+
*
|
|
1081
|
+
* @returns Object containing all parameters
|
|
1082
|
+
*/
|
|
1083
|
+
getParams(): Record<string, unknown>;
|
|
1084
|
+
/**
|
|
1085
|
+
* Set the parameters of this estimator.
|
|
1086
|
+
*
|
|
1087
|
+
* @param params - Parameters to set
|
|
1088
|
+
* @returns this - The estimator
|
|
1089
|
+
*/
|
|
1090
|
+
setParams(_params: Record<string, unknown>): this;
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
/**
|
|
1094
|
+
* Logistic Regression (Binary and Multiclass Classification).
|
|
1095
|
+
*
|
|
1096
|
+
* Logistic regression uses the logistic (sigmoid) function to model
|
|
1097
|
+
* the probability of class membership.
|
|
1098
|
+
*
|
|
1099
|
+
* @example
|
|
1100
|
+
* ```ts
|
|
1101
|
+
* import { LogisticRegression } from 'deepbox/ml';
|
|
1102
|
+
*
|
|
1103
|
+
* // Binary classification
|
|
1104
|
+
* const model = new LogisticRegression({ C: 1.0, maxIter: 100 });
|
|
1105
|
+
* model.fit(X_train, y_train);
|
|
1106
|
+
*
|
|
1107
|
+
* const predictions = model.predict(X_test);
|
|
1108
|
+
* const probabilities = model.predictProba(X_test);
|
|
1109
|
+
*
|
|
1110
|
+
* // Multiclass classification
|
|
1111
|
+
* const multiModel = new LogisticRegression({
|
|
1112
|
+
* multiClass: 'multinomial',
|
|
1113
|
+
* solver: 'lbfgs'
|
|
1114
|
+
* });
|
|
1115
|
+
* multiModel.fit(X_train_multi, y_train_multi);
|
|
1116
|
+
* ```
|
|
1117
|
+
*
|
|
1118
|
+
* @category Linear Models
|
|
1119
|
+
* @implements {Classifier}
|
|
1120
|
+
*/
|
|
1121
|
+
declare class LogisticRegression implements Classifier {
|
|
1122
|
+
private options;
|
|
1123
|
+
private coef_?;
|
|
1124
|
+
private intercept_?;
|
|
1125
|
+
private nFeaturesIn_?;
|
|
1126
|
+
private classes_?;
|
|
1127
|
+
private fitted;
|
|
1128
|
+
private multiclass_;
|
|
1129
|
+
/**
|
|
1130
|
+
* Create a new Logistic Regression classifier.
|
|
1131
|
+
*
|
|
1132
|
+
* @param options - Configuration options
|
|
1133
|
+
* @param options.penalty - Regularization type: 'l2' or 'none' (default: 'l2')
|
|
1134
|
+
* @param options.C - Inverse regularization strength (default: 1.0). Must be > 0.
|
|
1135
|
+
* @param options.tol - Tolerance for stopping criterion (default: 1e-4)
|
|
1136
|
+
* @param options.maxIter - Maximum number of iterations (default: 100)
|
|
1137
|
+
* @param options.fitIntercept - Whether to fit intercept (default: true)
|
|
1138
|
+
* @param options.learningRate - Learning rate for gradient descent (default: 0.1)
|
|
1139
|
+
* @param options.multiClass - Multiclass strategy: 'ovr' (One-vs-Rest) or 'auto' (default: 'auto')
|
|
1140
|
+
*/
|
|
1141
|
+
constructor(options?: {
|
|
1142
|
+
readonly penalty?: "l2" | "none";
|
|
1143
|
+
readonly tol?: number;
|
|
1144
|
+
readonly C?: number;
|
|
1145
|
+
readonly fitIntercept?: boolean;
|
|
1146
|
+
readonly maxIter?: number;
|
|
1147
|
+
readonly learningRate?: number;
|
|
1148
|
+
readonly multiClass?: "ovr" | "auto";
|
|
1149
|
+
});
|
|
1150
|
+
/**
|
|
1151
|
+
* Numerically stable sigmoid function.
|
|
1152
|
+
*
|
|
1153
|
+
* Uses different formulations for positive and negative inputs
|
|
1154
|
+
* to avoid overflow:
|
|
1155
|
+
* - For z >= 0: σ(z) = 1 / (1 + exp(-z))
|
|
1156
|
+
* - For z < 0: σ(z) = exp(z) / (1 + exp(z))
|
|
1157
|
+
*
|
|
1158
|
+
* @param z - Input value
|
|
1159
|
+
* @returns Sigmoid output in [0, 1]
|
|
1160
|
+
*/
|
|
1161
|
+
private sigmoid;
|
|
1162
|
+
private ensureFitted;
|
|
1163
|
+
private _fitBinary;
|
|
1164
|
+
/**
|
|
1165
|
+
* Fit logistic regression model.
|
|
1166
|
+
*
|
|
1167
|
+
* Uses gradient descent with L2 regularization.
|
|
1168
|
+
* Supports binary and multiclass (One-vs-Rest) classification.
|
|
1169
|
+
*
|
|
1170
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
1171
|
+
* @param y - Target labels of shape (n_samples,)
|
|
1172
|
+
* @returns this - The fitted estimator
|
|
1173
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
1174
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
1175
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
1176
|
+
* @throws {DataValidationError} If X or y are empty
|
|
1177
|
+
* @throws {InvalidParameterError} If C <= 0 or penalty is invalid
|
|
1178
|
+
*/
|
|
1179
|
+
fit(X: Tensor, y: Tensor): this;
|
|
1180
|
+
get classes(): Tensor | undefined;
|
|
1181
|
+
/**
|
|
1182
|
+
* Get the model coefficients (weights).
|
|
1183
|
+
*
|
|
1184
|
+
* @returns Coefficient tensor of shape (n_features,)
|
|
1185
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1186
|
+
*/
|
|
1187
|
+
get coef(): Tensor;
|
|
1188
|
+
/**
|
|
1189
|
+
* Get the intercept (bias term).
|
|
1190
|
+
*
|
|
1191
|
+
* @returns Intercept value (scalar for binary, array for multiclass)
|
|
1192
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1193
|
+
*/
|
|
1194
|
+
get intercept(): number | number[];
|
|
1195
|
+
predict(X: Tensor): Tensor;
|
|
1196
|
+
/**
|
|
1197
|
+
* Predict class probabilities for samples.
|
|
1198
|
+
*
|
|
1199
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
1200
|
+
* @returns Probabilities of shape (n_samples, n_classes)
|
|
1201
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1202
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
1203
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
1204
|
+
*/
|
|
1205
|
+
predictProba(X: Tensor): Tensor;
|
|
1206
|
+
/**
|
|
1207
|
+
* Return the mean accuracy on the given test data and labels.
|
|
1208
|
+
*
|
|
1209
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
1210
|
+
* @param y - True labels of shape (n_samples,)
|
|
1211
|
+
* @returns Accuracy score in range [0, 1]
|
|
1212
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1213
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
1214
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
1215
|
+
*/
|
|
1216
|
+
score(X: Tensor, y: Tensor): number;
|
|
1217
|
+
/**
|
|
1218
|
+
* Get hyperparameters for this estimator.
|
|
1219
|
+
*
|
|
1220
|
+
* @returns Object containing all hyperparameters
|
|
1221
|
+
*/
|
|
1222
|
+
getParams(): Record<string, unknown>;
|
|
1223
|
+
/**
|
|
1224
|
+
* Set the parameters of this estimator.
|
|
1225
|
+
*
|
|
1226
|
+
* @param params - Parameters to set (maxIter, tol, C, learningRate, penalty, fitIntercept)
|
|
1227
|
+
* @returns this
|
|
1228
|
+
* @throws {InvalidParameterError} If any parameter value is invalid
|
|
1229
|
+
*/
|
|
1230
|
+
setParams(params: Record<string, unknown>): this;
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
/**
|
|
1234
|
+
* Ridge Regression (L2 Regularized Linear Regression).
|
|
1235
|
+
*
|
|
1236
|
+
* Ridge regression addresses multicollinearity by adding a penalty term
|
|
1237
|
+
* (L2 regularization) to the loss function.
|
|
1238
|
+
*
|
|
1239
|
+
* @example
|
|
1240
|
+
* ```ts
|
|
1241
|
+
* import { Ridge } from 'deepbox/ml';
|
|
1242
|
+
*
|
|
1243
|
+
* const model = new Ridge({ alpha: 0.5 });
|
|
1244
|
+
* model.fit(X_train, y_train);
|
|
1245
|
+
* const predictions = model.predict(X_test);
|
|
1246
|
+
* ```
|
|
1247
|
+
*
|
|
1248
|
+
* @category Linear Models
|
|
1249
|
+
* @implements {Regressor}
|
|
1250
|
+
*/
|
|
1251
|
+
declare class Ridge implements Regressor {
|
|
1252
|
+
/** Configuration options for the Ridge regression model */
|
|
1253
|
+
private options;
|
|
1254
|
+
/** Model coefficients (weights) after fitting - shape (n_features,) */
|
|
1255
|
+
private coef_?;
|
|
1256
|
+
/** Intercept (bias) term after fitting */
|
|
1257
|
+
private intercept_?;
|
|
1258
|
+
/** Number of features seen during fit - used for validation */
|
|
1259
|
+
private nFeaturesIn_?;
|
|
1260
|
+
/** Number of iterations run by the solver (for iterative solvers) */
|
|
1261
|
+
private nIter_;
|
|
1262
|
+
/** Whether the model has been fitted to data */
|
|
1263
|
+
private fitted;
|
|
1264
|
+
/**
|
|
1265
|
+
* Create a new Ridge Regression model.
|
|
1266
|
+
*
|
|
1267
|
+
* @param options - Configuration options
|
|
1268
|
+
* @param options.alpha - Regularization strength (default: 1.0). Must be >= 0.
|
|
1269
|
+
* @param options.fitIntercept - Whether to calculate the intercept (default: true).
|
|
1270
|
+
* @param options.normalize - Whether to normalize features before regression (default: false).
|
|
1271
|
+
* @param options.solver - Solver to use (default: 'auto'). Options: 'auto', 'svd', 'cholesky', 'lsqr', 'sag'.
|
|
1272
|
+
* @param options.maxIter - Maximum number of iterations for iterative solvers (default: 1000)
|
|
1273
|
+
* @param options.tol - Tolerance for stopping criterion (default: 1e-4)
|
|
1274
|
+
*/
|
|
1275
|
+
constructor(options?: {
|
|
1276
|
+
readonly alpha?: number;
|
|
1277
|
+
readonly fitIntercept?: boolean;
|
|
1278
|
+
readonly normalize?: boolean;
|
|
1279
|
+
readonly solver?: "auto" | "svd" | "cholesky" | "lsqr" | "sag";
|
|
1280
|
+
readonly maxIter?: number;
|
|
1281
|
+
readonly tol?: number;
|
|
1282
|
+
});
|
|
1283
|
+
/**
|
|
1284
|
+
* Fit Ridge regression model.
|
|
1285
|
+
*
|
|
1286
|
+
* Solves the regularized least squares problem:
|
|
1287
|
+
* minimize ||y - Xw||² + α||w||²
|
|
1288
|
+
*
|
|
1289
|
+
* Uses the closed-form solution:
|
|
1290
|
+
* w = (X^T X + αI)^(-1) X^T y
|
|
1291
|
+
*
|
|
1292
|
+
* **Time Complexity**: O(n²p + p³) where n = samples, p = features
|
|
1293
|
+
* **Space Complexity**: O(p²)
|
|
1294
|
+
*
|
|
1295
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
1296
|
+
* @param y - Target values of shape (n_samples,)
|
|
1297
|
+
* @returns this - The fitted estimator for method chaining
|
|
1298
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
1299
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
1300
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
1301
|
+
* @throws {DataValidationError} If X or y are empty
|
|
1302
|
+
* @throws {InvalidParameterError} If alpha < 0
|
|
1303
|
+
*/
|
|
1304
|
+
fit(X: Tensor, y: Tensor): this;
|
|
1305
|
+
/**
|
|
1306
|
+
* Solve linear system Ax = b using Gaussian elimination with partial pivoting.
|
|
1307
|
+
*
|
|
1308
|
+
* This is a numerically stable method for solving dense linear systems.
|
|
1309
|
+
* For Ridge regression, A = X^T X + αI is symmetric positive definite,
|
|
1310
|
+
* so Cholesky decomposition would be more efficient, but Gaussian elimination
|
|
1311
|
+
* is more general and still provides good numerical stability.
|
|
1312
|
+
*
|
|
1313
|
+
* **Algorithm**:
|
|
1314
|
+
* 1. Forward elimination: Convert A to upper triangular form
|
|
1315
|
+
* 2. Partial pivoting: Swap rows to avoid division by small numbers
|
|
1316
|
+
* 3. Back substitution: Solve for x from bottom to top
|
|
1317
|
+
*
|
|
1318
|
+
* **Time Complexity**: O(n³)
|
|
1319
|
+
* **Space Complexity**: O(n²)
|
|
1320
|
+
*
|
|
1321
|
+
* @param A - Coefficient matrix (n × n)
|
|
1322
|
+
* @param b - Right-hand side vector (n × 1)
|
|
1323
|
+
* @returns Solution vector x such that Ax = b
|
|
1324
|
+
*/
|
|
1325
|
+
private solveLinearSystem;
|
|
1326
|
+
private solveConjugateGradient;
|
|
1327
|
+
private solveSag;
|
|
1328
|
+
private rescaleCoefs;
|
|
1329
|
+
/**
|
|
1330
|
+
* Predict using the Ridge regression model.
|
|
1331
|
+
*
|
|
1332
|
+
* Computes predictions as: ŷ = X @ coef + intercept
|
|
1333
|
+
*
|
|
1334
|
+
* **Time Complexity**: O(nm) where n = samples, m = features
|
|
1335
|
+
* **Space Complexity**: O(n)
|
|
1336
|
+
*
|
|
1337
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
1338
|
+
* @returns Predicted values of shape (n_samples,)
|
|
1339
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1340
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
1341
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
1342
|
+
*/
|
|
1343
|
+
predict(X: Tensor): Tensor;
|
|
1344
|
+
/**
|
|
1345
|
+
* Return the coefficient of determination R² of the prediction.
|
|
1346
|
+
*
|
|
1347
|
+
* R² (R-squared) measures the proportion of variance in y explained by the model.
|
|
1348
|
+
* Formula: R² = 1 - (SS_res / SS_tot)
|
|
1349
|
+
*
|
|
1350
|
+
* Where:
|
|
1351
|
+
* - SS_res = Σ(y_true - y_pred)² (residual sum of squares)
|
|
1352
|
+
* - SS_tot = Σ(y_true - y_mean)² (total sum of squares)
|
|
1353
|
+
*
|
|
1354
|
+
* **Interpretation**:
|
|
1355
|
+
* - R² = 1: Perfect predictions
|
|
1356
|
+
* - R² = 0: Model performs as well as predicting the mean
|
|
1357
|
+
* - R² < 0: Model performs worse than predicting the mean
|
|
1358
|
+
*
|
|
1359
|
+
* **Time Complexity**: O(n) where n = number of samples
|
|
1360
|
+
*
|
|
1361
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
1362
|
+
* @param y - True values of shape (n_samples,)
|
|
1363
|
+
* @returns R² score (best possible score is 1.0, can be negative)
|
|
1364
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1365
|
+
* @throws {ShapeError} If y is not 1-dimensional
|
|
1366
|
+
*/
|
|
1367
|
+
score(X: Tensor, y: Tensor): number;
|
|
1368
|
+
/**
|
|
1369
|
+
* Get parameters for this estimator.
|
|
1370
|
+
*
|
|
1371
|
+
* Returns a copy of all hyperparameters set during construction or via setParams.
|
|
1372
|
+
*
|
|
1373
|
+
* @returns Object containing all parameters with their current values
|
|
1374
|
+
*/
|
|
1375
|
+
getParams(): Record<string, unknown>;
|
|
1376
|
+
/**
|
|
1377
|
+
* Set the parameters of this estimator.
|
|
1378
|
+
*
|
|
1379
|
+
* Allows modifying hyperparameters after construction.
|
|
1380
|
+
* Note: Changing parameters requires refitting the model.
|
|
1381
|
+
*
|
|
1382
|
+
* @param params - Dictionary of parameters to set
|
|
1383
|
+
* @returns this - The estimator for method chaining
|
|
1384
|
+
* @throws {TypeError} If parameter value has wrong type
|
|
1385
|
+
* @throws {Error} If parameter name is unknown or value is invalid
|
|
1386
|
+
*/
|
|
1387
|
+
setParams(params: Record<string, unknown>): this;
|
|
1388
|
+
/**
|
|
1389
|
+
* Get the model coefficients (weights).
|
|
1390
|
+
*
|
|
1391
|
+
* @returns Coefficient tensor of shape (n_features,)
|
|
1392
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1393
|
+
*/
|
|
1394
|
+
get coef(): Tensor;
|
|
1395
|
+
/**
|
|
1396
|
+
* Get the intercept (bias term).
|
|
1397
|
+
*
|
|
1398
|
+
* @returns Intercept value
|
|
1399
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1400
|
+
*/
|
|
1401
|
+
get intercept(): number;
|
|
1402
|
+
/**
|
|
1403
|
+
* Get the number of iterations run by the solver.
|
|
1404
|
+
*
|
|
1405
|
+
* @returns Number of iterations (undefined for direct solvers)
|
|
1406
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1407
|
+
*/
|
|
1408
|
+
get nIter(): number | undefined;
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
/**
|
|
1412
|
+
* t-Distributed Stochastic Neighbor Embedding (t-SNE).
|
|
1413
|
+
*
|
|
1414
|
+
* A nonlinear dimensionality reduction technique for embedding high-dimensional
|
|
1415
|
+
* data into a low-dimensional space (typically 2D or 3D) for visualization.
|
|
1416
|
+
*
|
|
1417
|
+
* **Algorithm**: Exact t-SNE with an optional sampling-based approximation
|
|
1418
|
+
* - Computes pairwise affinities in high-dimensional space using Gaussian kernel (exact)
|
|
1419
|
+
* - Computes pairwise affinities in low-dimensional space using Student-t distribution
|
|
1420
|
+
* - Minimizes KL divergence between the two distributions
|
|
1421
|
+
*
|
|
1422
|
+
* **Scalability Note**:
|
|
1423
|
+
* Exact t-SNE is O(n^2) in time and memory. For large datasets, use
|
|
1424
|
+
* `method: "approximate"` (sampled neighbors + negative sampling) or reduce samples.
|
|
1425
|
+
*
|
|
1426
|
+
* @example
|
|
1427
|
+
* ```ts
|
|
1428
|
+
* import { TSNE } from 'deepbox/ml';
|
|
1429
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
1430
|
+
*
|
|
1431
|
+
* const X = tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]);
|
|
1432
|
+
*
|
|
1433
|
+
* const tsne = new TSNE({ nComponents: 2, perplexity: 5 });
|
|
1434
|
+
* const embedding = tsne.fitTransform(X);
|
|
1435
|
+
* ```
|
|
1436
|
+
*
|
|
1437
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.manifold.TSNE.html | scikit-learn TSNE}
|
|
1438
|
+
* @see van der Maaten, L.J.P.; Hinton, G.E. (2008). "Visualizing High-Dimensional Data Using t-SNE"
|
|
1439
|
+
*/
|
|
1440
|
+
declare class TSNE {
|
|
1441
|
+
/** Number of dimensions in the embedding */
|
|
1442
|
+
private readonly nComponents;
|
|
1443
|
+
/** Perplexity parameter (related to number of nearest neighbors) */
|
|
1444
|
+
private readonly perplexity;
|
|
1445
|
+
/** Learning rate for gradient descent */
|
|
1446
|
+
private readonly learningRate;
|
|
1447
|
+
/** Number of iterations */
|
|
1448
|
+
private readonly nIter;
|
|
1449
|
+
/** Early exaggeration factor */
|
|
1450
|
+
private readonly earlyExaggeration;
|
|
1451
|
+
/** Number of iterations with early exaggeration */
|
|
1452
|
+
private readonly earlyExaggerationIter;
|
|
1453
|
+
/** Random seed for reproducibility */
|
|
1454
|
+
private readonly randomState;
|
|
1455
|
+
/** Minimum gradient norm for convergence */
|
|
1456
|
+
private readonly minGradNorm;
|
|
1457
|
+
/** Method for computing affinities and gradients */
|
|
1458
|
+
private readonly method;
|
|
1459
|
+
/** Maximum samples allowed for exact mode */
|
|
1460
|
+
private readonly maxExactSamples;
|
|
1461
|
+
/** Neighbor count for approximate mode */
|
|
1462
|
+
private readonly approximateNeighbors;
|
|
1463
|
+
/** Negative samples per point for approximate mode */
|
|
1464
|
+
private readonly negativeSamples;
|
|
1465
|
+
/** The embedded points after fitting */
|
|
1466
|
+
private embedding;
|
|
1467
|
+
/** Whether the model has been fitted */
|
|
1468
|
+
private fitted;
|
|
1469
|
+
constructor(options?: {
|
|
1470
|
+
readonly nComponents?: number;
|
|
1471
|
+
readonly perplexity?: number;
|
|
1472
|
+
readonly learningRate?: number;
|
|
1473
|
+
readonly nIter?: number;
|
|
1474
|
+
readonly earlyExaggeration?: number;
|
|
1475
|
+
readonly earlyExaggerationIter?: number;
|
|
1476
|
+
readonly randomState?: number;
|
|
1477
|
+
readonly minGradNorm?: number;
|
|
1478
|
+
/** "exact" uses full pairwise interactions; "approximate" uses sampling for large datasets. */
|
|
1479
|
+
readonly method?: "exact" | "approximate";
|
|
1480
|
+
/** Maximum samples allowed in exact mode before requiring approximate. */
|
|
1481
|
+
readonly maxExactSamples?: number;
|
|
1482
|
+
/** Number of neighbors to sample per point in approximate mode. */
|
|
1483
|
+
readonly approximateNeighbors?: number;
|
|
1484
|
+
/** Number of negative samples per point in approximate mode. */
|
|
1485
|
+
readonly negativeSamples?: number;
|
|
1486
|
+
});
|
|
1487
|
+
/**
|
|
1488
|
+
* Compute pairwise squared Euclidean distances.
|
|
1489
|
+
*/
|
|
1490
|
+
private computeDistances;
|
|
1491
|
+
/**
|
|
1492
|
+
* Compute squared Euclidean distance between two vectors.
|
|
1493
|
+
*/
|
|
1494
|
+
private computeSquaredDistance;
|
|
1495
|
+
/**
|
|
1496
|
+
* Sample unique indices excluding a single index.
|
|
1497
|
+
*/
|
|
1498
|
+
private sampleIndices;
|
|
1499
|
+
/**
|
|
1500
|
+
* Sample unique indices excluding a set of indices.
|
|
1501
|
+
*/
|
|
1502
|
+
private sampleIndicesWithExclusions;
|
|
1503
|
+
/**
|
|
1504
|
+
* Compute conditional probabilities P(j|i) using binary search for sigma.
|
|
1505
|
+
*/
|
|
1506
|
+
private computeProbabilities;
|
|
1507
|
+
/**
|
|
1508
|
+
* Compute sparse joint probabilities using sampled neighbors (approximate).
|
|
1509
|
+
*/
|
|
1510
|
+
private computeProbabilitiesSparse;
|
|
1511
|
+
/**
|
|
1512
|
+
* Symmetrize sparse probabilities: P = (P + P^T) / (2n).
|
|
1513
|
+
*/
|
|
1514
|
+
private symmetrizeSparse;
|
|
1515
|
+
/**
|
|
1516
|
+
* Initialize embedding with small random values.
|
|
1517
|
+
*/
|
|
1518
|
+
private initializeEmbedding;
|
|
1519
|
+
/**
|
|
1520
|
+
* Compute Q distribution (Student-t with 1 degree of freedom).
|
|
1521
|
+
*/
|
|
1522
|
+
private computeQ;
|
|
1523
|
+
/**
|
|
1524
|
+
* Compute approximate Q using sampled pairs.
|
|
1525
|
+
*/
|
|
1526
|
+
private computeQApprox;
|
|
1527
|
+
/**
|
|
1528
|
+
* Compute gradients of KL divergence.
|
|
1529
|
+
*/
|
|
1530
|
+
private computeGradients;
|
|
1531
|
+
/**
|
|
1532
|
+
* Compute approximate gradients of KL divergence using sampled pairs.
|
|
1533
|
+
*/
|
|
1534
|
+
private computeGradientsApprox;
|
|
1535
|
+
/**
|
|
1536
|
+
* Fit the t-SNE model and return the embedding.
|
|
1537
|
+
*
|
|
1538
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
1539
|
+
* @returns Low-dimensional embedding of shape (n_samples, n_components)
|
|
1540
|
+
*/
|
|
1541
|
+
fitTransform(X: Tensor): Tensor;
|
|
1542
|
+
/**
|
|
1543
|
+
* Fit the model (same as fitTransform for t-SNE).
|
|
1544
|
+
*/
|
|
1545
|
+
fit(X: Tensor): this;
|
|
1546
|
+
/**
|
|
1547
|
+
* Get the embedding.
|
|
1548
|
+
*/
|
|
1549
|
+
get embeddingResult(): Tensor;
|
|
1550
|
+
/**
|
|
1551
|
+
* Get hyperparameters for this estimator.
|
|
1552
|
+
*
|
|
1553
|
+
* @returns Object containing all hyperparameters
|
|
1554
|
+
*/
|
|
1555
|
+
getParams(): Record<string, unknown>;
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
/**
|
|
1559
|
+
* Gaussian Naive Bayes classifier.
|
|
1560
|
+
*
|
|
1561
|
+
* Implements the Gaussian Naive Bayes algorithm for classification.
|
|
1562
|
+
* Assumes features follow a Gaussian (normal) distribution.
|
|
1563
|
+
*
|
|
1564
|
+
* **Algorithm**:
|
|
1565
|
+
* 1. Calculate mean and variance for each feature per class
|
|
1566
|
+
* 2. For prediction, calculate likelihood using Gaussian PDF
|
|
1567
|
+
* 3. Apply Bayes' theorem to get posterior probabilities
|
|
1568
|
+
* 4. Predict class with highest posterior probability
|
|
1569
|
+
*
|
|
1570
|
+
* **Time Complexity**:
|
|
1571
|
+
* - Training: O(n * d) where n=samples, d=features
|
|
1572
|
+
* - Prediction: O(k * d) per sample where k=classes
|
|
1573
|
+
*
|
|
1574
|
+
* @example
|
|
1575
|
+
* ```ts
|
|
1576
|
+
* import { GaussianNB } from 'deepbox/ml';
|
|
1577
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
1578
|
+
*
|
|
1579
|
+
* const X = tensor([[1, 2], [2, 3], [3, 4], [4, 5]]);
|
|
1580
|
+
* const y = tensor([0, 0, 1, 1]);
|
|
1581
|
+
*
|
|
1582
|
+
* const nb = new GaussianNB();
|
|
1583
|
+
* nb.fit(X, y);
|
|
1584
|
+
*
|
|
1585
|
+
* const predictions = nb.predict(tensor([[2.5, 3.5]]));
|
|
1586
|
+
* ```
|
|
1587
|
+
*
|
|
1588
|
+
* @see {@link https://en.wikipedia.org/wiki/Naive_Bayes_classifier | Wikipedia: Naive Bayes}
|
|
1589
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html | scikit-learn GaussianNB}
|
|
1590
|
+
*/
|
|
1591
|
+
declare class GaussianNB implements Classifier {
|
|
1592
|
+
private readonly varSmoothing;
|
|
1593
|
+
private classes_?;
|
|
1594
|
+
private classPrior_?;
|
|
1595
|
+
private theta_?;
|
|
1596
|
+
private var_?;
|
|
1597
|
+
private nFeaturesIn_?;
|
|
1598
|
+
private fitted;
|
|
1599
|
+
/**
|
|
1600
|
+
* Create a new Gaussian Naive Bayes classifier.
|
|
1601
|
+
*
|
|
1602
|
+
* @param options - Configuration options
|
|
1603
|
+
* @param options.varSmoothing - Portion of largest variance added to variances for stability (default: 1e-9)
|
|
1604
|
+
*/
|
|
1605
|
+
constructor(options?: {
|
|
1606
|
+
readonly varSmoothing?: number;
|
|
1607
|
+
});
|
|
1608
|
+
/**
|
|
1609
|
+
* Fit Gaussian Naive Bayes classifier from the training set.
|
|
1610
|
+
*
|
|
1611
|
+
* Computes per-class mean, variance, and prior probabilities.
|
|
1612
|
+
*
|
|
1613
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
1614
|
+
* @param y - Target class labels of shape (n_samples,)
|
|
1615
|
+
* @returns this - The fitted estimator
|
|
1616
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
1617
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
1618
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
1619
|
+
* @throws {DataValidationError} If zero variance encountered with varSmoothing=0
|
|
1620
|
+
*/
|
|
1621
|
+
fit(X: Tensor, y: Tensor): this;
|
|
1622
|
+
/**
|
|
1623
|
+
* Predict class labels for samples in X.
|
|
1624
|
+
*
|
|
1625
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
1626
|
+
* @returns Predicted class labels of shape (n_samples,)
|
|
1627
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1628
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
1629
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
1630
|
+
*/
|
|
1631
|
+
predict(X: Tensor): Tensor;
|
|
1632
|
+
/**
|
|
1633
|
+
* Predict class probabilities for samples in X.
|
|
1634
|
+
*
|
|
1635
|
+
* Uses Bayes' theorem with Gaussian class-conditional likelihoods.
|
|
1636
|
+
*
|
|
1637
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
1638
|
+
* @returns Class probability matrix of shape (n_samples, n_classes)
|
|
1639
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1640
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
1641
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
1642
|
+
*/
|
|
1643
|
+
predictProba(X: Tensor): Tensor;
|
|
1644
|
+
/**
|
|
1645
|
+
* Return the mean accuracy on the given test data and labels.
|
|
1646
|
+
*
|
|
1647
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
1648
|
+
* @param y - True labels of shape (n_samples,)
|
|
1649
|
+
* @returns Accuracy score in range [0, 1]
|
|
1650
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1651
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
1652
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
1653
|
+
*/
|
|
1654
|
+
score(X: Tensor, y: Tensor): number;
|
|
1655
|
+
/**
|
|
1656
|
+
* Get the unique class labels discovered during fitting.
|
|
1657
|
+
*
|
|
1658
|
+
* @returns Tensor of class labels or undefined if not fitted
|
|
1659
|
+
*/
|
|
1660
|
+
get classes(): Tensor | undefined;
|
|
1661
|
+
/**
|
|
1662
|
+
* Get hyperparameters for this estimator.
|
|
1663
|
+
*
|
|
1664
|
+
* @returns Object containing all hyperparameters
|
|
1665
|
+
*/
|
|
1666
|
+
getParams(): Record<string, unknown>;
|
|
1667
|
+
/**
|
|
1668
|
+
* Set the parameters of this estimator.
|
|
1669
|
+
*
|
|
1670
|
+
* @param _params - Parameters to set
|
|
1671
|
+
* @throws {NotImplementedError} Always — parameters cannot be changed after construction
|
|
1672
|
+
*/
|
|
1673
|
+
setParams(_params: Record<string, unknown>): this;
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
/**
|
|
1677
|
+
* K-Nearest Neighbors base class.
|
|
1678
|
+
*/
|
|
1679
|
+
declare abstract class KNeighborsBase {
|
|
1680
|
+
protected readonly nNeighbors: number;
|
|
1681
|
+
protected readonly weights: "uniform" | "distance";
|
|
1682
|
+
protected readonly metric: "euclidean" | "manhattan";
|
|
1683
|
+
protected XTrain_?: Tensor;
|
|
1684
|
+
protected yTrain_?: Tensor;
|
|
1685
|
+
protected nFeaturesIn_?: number;
|
|
1686
|
+
protected fitted: boolean;
|
|
1687
|
+
constructor(options?: {
|
|
1688
|
+
readonly nNeighbors?: number;
|
|
1689
|
+
readonly weights?: "uniform" | "distance";
|
|
1690
|
+
readonly metric?: "euclidean" | "manhattan";
|
|
1691
|
+
});
|
|
1692
|
+
protected calculateDistance(x1: number[], x2: number[]): number;
|
|
1693
|
+
protected findKNearest(sample: number[]): Array<{
|
|
1694
|
+
index: number;
|
|
1695
|
+
distance: number;
|
|
1696
|
+
}>;
|
|
1697
|
+
/**
|
|
1698
|
+
* Get hyperparameters for this estimator.
|
|
1699
|
+
*
|
|
1700
|
+
* @returns Object containing all hyperparameters
|
|
1701
|
+
*/
|
|
1702
|
+
getParams(): Record<string, unknown>;
|
|
1703
|
+
/**
|
|
1704
|
+
* Set the parameters of this estimator.
|
|
1705
|
+
*
|
|
1706
|
+
* @param _params - Parameters to set
|
|
1707
|
+
* @throws {NotImplementedError} Always — parameters cannot be changed after construction
|
|
1708
|
+
*/
|
|
1709
|
+
setParams(_params: Record<string, unknown>): this;
|
|
1710
|
+
}
|
|
1711
|
+
/**
|
|
1712
|
+
* K-Nearest Neighbors Classifier.
|
|
1713
|
+
*
|
|
1714
|
+
* Classification based on k nearest neighbors. Predicts class by majority vote
|
|
1715
|
+
* of k nearest training samples.
|
|
1716
|
+
*
|
|
1717
|
+
* **Algorithm**: Instance-based learning
|
|
1718
|
+
* 1. Store all training data
|
|
1719
|
+
* 2. For each test sample, find k nearest training samples
|
|
1720
|
+
* 3. Predict class by majority vote (or weighted vote)
|
|
1721
|
+
*
|
|
1722
|
+
* **Time Complexity**:
|
|
1723
|
+
* - Training: O(1) (just stores data)
|
|
1724
|
+
* - Prediction: O(n * d) per sample where n=training samples, d=features
|
|
1725
|
+
*
|
|
1726
|
+
* @example
|
|
1727
|
+
* ```ts
|
|
1728
|
+
* import { KNeighborsClassifier } from 'deepbox/ml';
|
|
1729
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
1730
|
+
*
|
|
1731
|
+
* const X = tensor([[0, 0], [1, 1], [2, 2], [3, 3]]);
|
|
1732
|
+
* const y = tensor([0, 0, 1, 1]);
|
|
1733
|
+
*
|
|
1734
|
+
* const knn = new KNeighborsClassifier({ nNeighbors: 3 });
|
|
1735
|
+
* knn.fit(X, y);
|
|
1736
|
+
*
|
|
1737
|
+
* const predictions = knn.predict(tensor([[1.5, 1.5]]));
|
|
1738
|
+
* ```
|
|
1739
|
+
*
|
|
1740
|
+
* @see {@link https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm | Wikipedia: KNN}
|
|
1741
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html | scikit-learn KNeighborsClassifier}
|
|
1742
|
+
*/
|
|
1743
|
+
declare class KNeighborsClassifier extends KNeighborsBase implements Classifier {
|
|
1744
|
+
/**
|
|
1745
|
+
* Fit the k-nearest neighbors classifier from the training set.
|
|
1746
|
+
*
|
|
1747
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
1748
|
+
* @param y - Target class labels of shape (n_samples,)
|
|
1749
|
+
* @returns this - The fitted estimator
|
|
1750
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
1751
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
1752
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
1753
|
+
* @throws {InvalidParameterError} If nNeighbors > n_samples
|
|
1754
|
+
*/
|
|
1755
|
+
fit(X: Tensor, y: Tensor): this;
|
|
1756
|
+
/**
|
|
1757
|
+
* Predict class labels for samples in X.
|
|
1758
|
+
*
|
|
1759
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
1760
|
+
* @returns Predicted class labels of shape (n_samples,)
|
|
1761
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1762
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
1763
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
1764
|
+
*/
|
|
1765
|
+
predict(X: Tensor): Tensor;
|
|
1766
|
+
/**
|
|
1767
|
+
* Predict class probabilities for samples in X.
|
|
1768
|
+
*
|
|
1769
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
1770
|
+
* @returns Class probability matrix of shape (n_samples, n_classes)
|
|
1771
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1772
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
1773
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
1774
|
+
*/
|
|
1775
|
+
predictProba(X: Tensor): Tensor;
|
|
1776
|
+
/**
|
|
1777
|
+
* Return the mean accuracy on the given test data and labels.
|
|
1778
|
+
*
|
|
1779
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
1780
|
+
* @param y - True labels of shape (n_samples,)
|
|
1781
|
+
* @returns Accuracy score in range [0, 1]
|
|
1782
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1783
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
1784
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
1785
|
+
*/
|
|
1786
|
+
score(X: Tensor, y: Tensor): number;
|
|
1787
|
+
}
|
|
1788
|
+
/**
|
|
1789
|
+
* K-Nearest Neighbors Regressor.
|
|
1790
|
+
*
|
|
1791
|
+
* Regression based on k nearest neighbors. Predicts value as mean (or weighted mean)
|
|
1792
|
+
* of k nearest training samples.
|
|
1793
|
+
*
|
|
1794
|
+
* @example
|
|
1795
|
+
* ```ts
|
|
1796
|
+
* import { KNeighborsRegressor } from 'deepbox/ml';
|
|
1797
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
1798
|
+
*
|
|
1799
|
+
* const X = tensor([[0], [1], [2], [3]]);
|
|
1800
|
+
* const y = tensor([0, 1, 4, 9]);
|
|
1801
|
+
*
|
|
1802
|
+
* const knn = new KNeighborsRegressor({ nNeighbors: 2 });
|
|
1803
|
+
* knn.fit(X, y);
|
|
1804
|
+
*
|
|
1805
|
+
* const predictions = knn.predict(tensor([[1.5]]));
|
|
1806
|
+
* ```
|
|
1807
|
+
*
|
|
1808
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsRegressor.html | scikit-learn KNeighborsRegressor}
|
|
1809
|
+
*/
|
|
1810
|
+
declare class KNeighborsRegressor extends KNeighborsBase implements Regressor {
|
|
1811
|
+
/**
|
|
1812
|
+
* Fit the k-nearest neighbors regressor from the training set.
|
|
1813
|
+
*
|
|
1814
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
1815
|
+
* @param y - Target values of shape (n_samples,)
|
|
1816
|
+
* @returns this - The fitted estimator
|
|
1817
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
1818
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
1819
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
1820
|
+
* @throws {InvalidParameterError} If nNeighbors > n_samples
|
|
1821
|
+
*/
|
|
1822
|
+
fit(X: Tensor, y: Tensor): this;
|
|
1823
|
+
/**
|
|
1824
|
+
* Predict target values for samples in X.
|
|
1825
|
+
*
|
|
1826
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
1827
|
+
* @returns Predicted values of shape (n_samples,)
|
|
1828
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1829
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
1830
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
1831
|
+
*/
|
|
1832
|
+
predict(X: Tensor): Tensor;
|
|
1833
|
+
/**
|
|
1834
|
+
* Return the R² score on the given test data and target values.
|
|
1835
|
+
*
|
|
1836
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
1837
|
+
* @param y - True target values of shape (n_samples,)
|
|
1838
|
+
* @returns R² score (best possible is 1.0, can be negative)
|
|
1839
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1840
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
1841
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
1842
|
+
*/
|
|
1843
|
+
score(X: Tensor, y: Tensor): number;
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
/**
|
|
1847
|
+
* Support Vector Machine (SVM) Classifier.
|
|
1848
|
+
*
|
|
1849
|
+
* Implements a linear SVM using sub-gradient descent on the hinge loss
|
|
1850
|
+
* with L2 regularization (soft margin). Suitable for binary classification tasks.
|
|
1851
|
+
*
|
|
1852
|
+
* **Algorithm**: Sub-gradient descent on hinge loss (linear kernel)
|
|
1853
|
+
*
|
|
1854
|
+
* **Mathematical Formulation**:
|
|
1855
|
+
* - Decision function: f(x) = sign(w · x + b)
|
|
1856
|
+
* - Optimization: minimize (1/2)||w||² + C * Σmax(0, 1 - y_i(w · x_i + b))
|
|
1857
|
+
*
|
|
1858
|
+
* @example
|
|
1859
|
+
* ```ts
|
|
1860
|
+
* import { LinearSVC } from 'deepbox/ml';
|
|
1861
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
1862
|
+
*
|
|
1863
|
+
* const X = tensor([[1, 2], [2, 3], [3, 1], [4, 2]]);
|
|
1864
|
+
* const y = tensor([0, 0, 1, 1]);
|
|
1865
|
+
*
|
|
1866
|
+
* const svm = new LinearSVC({ C: 1.0 });
|
|
1867
|
+
* svm.fit(X, y);
|
|
1868
|
+
* const predictions = svm.predict(X);
|
|
1869
|
+
* ```
|
|
1870
|
+
*
|
|
1871
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html | scikit-learn LinearSVC}
|
|
1872
|
+
*/
|
|
1873
|
+
declare class LinearSVC implements Classifier {
|
|
1874
|
+
/** Regularization parameter (inverse of regularization strength) */
|
|
1875
|
+
private readonly C;
|
|
1876
|
+
/** Maximum number of iterations for optimization */
|
|
1877
|
+
private readonly maxIter;
|
|
1878
|
+
/** Tolerance for stopping criterion */
|
|
1879
|
+
private readonly tol;
|
|
1880
|
+
/** Weight vector of shape (n_features,) */
|
|
1881
|
+
private weights;
|
|
1882
|
+
/** Bias term */
|
|
1883
|
+
private bias;
|
|
1884
|
+
/** Number of features seen during fit */
|
|
1885
|
+
private nFeatures;
|
|
1886
|
+
/** Unique class labels [0, 1] mapped from original labels */
|
|
1887
|
+
private classLabels;
|
|
1888
|
+
/** Whether the model has been fitted */
|
|
1889
|
+
private fitted;
|
|
1890
|
+
/**
|
|
1891
|
+
* Create a new SVM Classifier.
|
|
1892
|
+
*
|
|
1893
|
+
* @param options - Configuration options
|
|
1894
|
+
* @param options.C - Regularization parameter (default: 1.0). Larger C = stronger penalty on errors = harder margin.
|
|
1895
|
+
* @param options.maxIter - Maximum iterations (default: 1000)
|
|
1896
|
+
* @param options.tol - Convergence tolerance (default: 1e-4)
|
|
1897
|
+
*/
|
|
1898
|
+
constructor(options?: {
|
|
1899
|
+
readonly C?: number;
|
|
1900
|
+
readonly maxIter?: number;
|
|
1901
|
+
readonly tol?: number;
|
|
1902
|
+
});
|
|
1903
|
+
/**
|
|
1904
|
+
* Fit the SVM classifier using sub-gradient descent.
|
|
1905
|
+
*
|
|
1906
|
+
* Uses a simplified hinge loss optimization with L2 regularization.
|
|
1907
|
+
* Objective: minimize (1/2)||w||² + C * Σmax(0, 1 - y_i(w · x_i + b))
|
|
1908
|
+
*
|
|
1909
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
1910
|
+
* @param y - Target labels of shape (n_samples,). Must contain exactly 2 classes.
|
|
1911
|
+
* @returns this - The fitted estimator
|
|
1912
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
1913
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
1914
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
1915
|
+
* @throws {InvalidParameterError} If y does not contain exactly 2 classes
|
|
1916
|
+
*/
|
|
1917
|
+
fit(X: Tensor, y: Tensor): this;
|
|
1918
|
+
/**
|
|
1919
|
+
* Predict class labels for samples in X.
|
|
1920
|
+
*
|
|
1921
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
1922
|
+
* @returns Predicted labels of shape (n_samples,)
|
|
1923
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1924
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
1925
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
1926
|
+
*/
|
|
1927
|
+
predict(X: Tensor): Tensor;
|
|
1928
|
+
/**
|
|
1929
|
+
* Predict class probabilities using Platt scaling approximation.
|
|
1930
|
+
*
|
|
1931
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
1932
|
+
* @returns Probability estimates of shape (n_samples, 2)
|
|
1933
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1934
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
1935
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
1936
|
+
*/
|
|
1937
|
+
predictProba(X: Tensor): Tensor;
|
|
1938
|
+
/**
|
|
1939
|
+
* Return the mean accuracy on the given test data and labels.
|
|
1940
|
+
*
|
|
1941
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
1942
|
+
* @param y - True labels of shape (n_samples,)
|
|
1943
|
+
* @returns Accuracy score in range [0, 1]
|
|
1944
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1945
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
1946
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
1947
|
+
*/
|
|
1948
|
+
score(X: Tensor, y: Tensor): number;
|
|
1949
|
+
/**
|
|
1950
|
+
* Get the weight vector.
|
|
1951
|
+
*
|
|
1952
|
+
* @returns Weight vector as tensor of shape (1, n_features)
|
|
1953
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1954
|
+
*/
|
|
1955
|
+
get coef(): Tensor;
|
|
1956
|
+
/**
|
|
1957
|
+
* Get the bias term.
|
|
1958
|
+
*
|
|
1959
|
+
* @returns Bias value
|
|
1960
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
1961
|
+
*/
|
|
1962
|
+
get intercept(): number;
|
|
1963
|
+
/**
|
|
1964
|
+
* Get hyperparameters for this estimator.
|
|
1965
|
+
*
|
|
1966
|
+
* @returns Object containing all hyperparameters
|
|
1967
|
+
*/
|
|
1968
|
+
getParams(): Record<string, unknown>;
|
|
1969
|
+
/**
|
|
1970
|
+
* Set the parameters of this estimator.
|
|
1971
|
+
*
|
|
1972
|
+
* @param _params - Parameters to set
|
|
1973
|
+
* @throws {NotImplementedError} Always — parameters cannot be changed after construction
|
|
1974
|
+
*/
|
|
1975
|
+
setParams(_params: Record<string, unknown>): this;
|
|
1976
|
+
}
|
|
1977
|
+
/**
|
|
1978
|
+
* Support Vector Machine (SVM) Regressor.
|
|
1979
|
+
*
|
|
1980
|
+
* Implements epsilon-SVR (Support Vector Regression) using sub-gradient descent.
|
|
1981
|
+
*
|
|
1982
|
+
* @example
|
|
1983
|
+
* ```ts
|
|
1984
|
+
* import { LinearSVR } from 'deepbox/ml';
|
|
1985
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
1986
|
+
*
|
|
1987
|
+
* const X = tensor([[1], [2], [3], [4]]);
|
|
1988
|
+
* const y = tensor([1.5, 2.5, 3.5, 4.5]);
|
|
1989
|
+
*
|
|
1990
|
+
* const svr = new LinearSVR({ C: 1.0, epsilon: 0.1 });
|
|
1991
|
+
* svr.fit(X, y);
|
|
1992
|
+
* const predictions = svr.predict(X);
|
|
1993
|
+
* ```
|
|
1994
|
+
*
|
|
1995
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVR.html | scikit-learn LinearSVR}
|
|
1996
|
+
*/
|
|
1997
|
+
declare class LinearSVR implements Regressor {
|
|
1998
|
+
/** Regularization parameter */
|
|
1999
|
+
private readonly C;
|
|
2000
|
+
/** Epsilon in the epsilon-SVR model */
|
|
2001
|
+
private readonly epsilon;
|
|
2002
|
+
/** Maximum number of iterations */
|
|
2003
|
+
private readonly maxIter;
|
|
2004
|
+
/** Tolerance for stopping criterion */
|
|
2005
|
+
private readonly tol;
|
|
2006
|
+
/** Weight vector */
|
|
2007
|
+
private weights;
|
|
2008
|
+
/** Bias term */
|
|
2009
|
+
private bias;
|
|
2010
|
+
/** Number of features */
|
|
2011
|
+
private nFeatures;
|
|
2012
|
+
/** Whether the model has been fitted */
|
|
2013
|
+
private fitted;
|
|
2014
|
+
constructor(options?: {
|
|
2015
|
+
readonly C?: number;
|
|
2016
|
+
readonly epsilon?: number;
|
|
2017
|
+
readonly maxIter?: number;
|
|
2018
|
+
readonly tol?: number;
|
|
2019
|
+
});
|
|
2020
|
+
/**
|
|
2021
|
+
* Fit the SVR model using sub-gradient descent on epsilon-insensitive loss.
|
|
2022
|
+
*
|
|
2023
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
2024
|
+
* @param y - Target values of shape (n_samples,)
|
|
2025
|
+
* @returns this - The fitted estimator
|
|
2026
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
2027
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
2028
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
2029
|
+
*/
|
|
2030
|
+
fit(X: Tensor, y: Tensor): this;
|
|
2031
|
+
/**
|
|
2032
|
+
* Predict target values for samples in X.
|
|
2033
|
+
*
|
|
2034
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
2035
|
+
* @returns Predicted values of shape (n_samples,)
|
|
2036
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
2037
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
2038
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
2039
|
+
*/
|
|
2040
|
+
predict(X: Tensor): Tensor;
|
|
2041
|
+
/**
|
|
2042
|
+
* Return the R² score on the given test data and target values.
|
|
2043
|
+
*
|
|
2044
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
2045
|
+
* @param y - True target values of shape (n_samples,)
|
|
2046
|
+
* @returns R² score (best possible is 1.0, can be negative)
|
|
2047
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
2048
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
2049
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
2050
|
+
*/
|
|
2051
|
+
score(X: Tensor, y: Tensor): number;
|
|
2052
|
+
/**
|
|
2053
|
+
* Get hyperparameters for this estimator.
|
|
2054
|
+
*
|
|
2055
|
+
* @returns Object containing all hyperparameters
|
|
2056
|
+
*/
|
|
2057
|
+
getParams(): Record<string, unknown>;
|
|
2058
|
+
/**
|
|
2059
|
+
* Set the parameters of this estimator.
|
|
2060
|
+
*
|
|
2061
|
+
* @param _params - Parameters to set
|
|
2062
|
+
* @throws {NotImplementedError} Always — parameters cannot be changed after construction
|
|
2063
|
+
*/
|
|
2064
|
+
setParams(_params: Record<string, unknown>): this;
|
|
2065
|
+
}
|
|
2066
|
+
|
|
2067
|
+
/**
|
|
2068
|
+
* Decision Tree Classifier.
|
|
2069
|
+
*
|
|
2070
|
+
* A non-parametric supervised learning method that learns simple decision rules
|
|
2071
|
+
* inferred from the data features.
|
|
2072
|
+
*
|
|
2073
|
+
* **Algorithm**: CART (Classification and Regression Trees)
|
|
2074
|
+
* - Uses Gini impurity for classification
|
|
2075
|
+
* - Recursively splits data based on feature thresholds
|
|
2076
|
+
* - Supports max_depth and min_samples_split for regularization
|
|
2077
|
+
*
|
|
2078
|
+
* @example
|
|
2079
|
+
* ```ts
|
|
2080
|
+
* import { DecisionTreeClassifier } from 'deepbox/ml';
|
|
2081
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
2082
|
+
*
|
|
2083
|
+
* const X = tensor([[1, 2], [3, 4], [5, 6], [7, 8]]);
|
|
2084
|
+
* const y = tensor([0, 0, 1, 1]);
|
|
2085
|
+
*
|
|
2086
|
+
* const clf = new DecisionTreeClassifier({ maxDepth: 3 });
|
|
2087
|
+
* clf.fit(X, y);
|
|
2088
|
+
* const predictions = clf.predict(X);
|
|
2089
|
+
* ```
|
|
2090
|
+
*
|
|
2091
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html | scikit-learn DecisionTreeClassifier}
|
|
2092
|
+
*/
|
|
2093
|
+
declare class DecisionTreeClassifier implements Classifier {
|
|
2094
|
+
private maxDepth;
|
|
2095
|
+
private minSamplesSplit;
|
|
2096
|
+
private minSamplesLeaf;
|
|
2097
|
+
private maxFeatures;
|
|
2098
|
+
private randomState;
|
|
2099
|
+
private tree?;
|
|
2100
|
+
private nFeatures?;
|
|
2101
|
+
private classLabels?;
|
|
2102
|
+
private fitted;
|
|
2103
|
+
constructor(options?: {
|
|
2104
|
+
readonly maxDepth?: number;
|
|
2105
|
+
readonly minSamplesSplit?: number;
|
|
2106
|
+
readonly minSamplesLeaf?: number;
|
|
2107
|
+
readonly maxFeatures?: number;
|
|
2108
|
+
readonly randomState?: number;
|
|
2109
|
+
});
|
|
2110
|
+
private getRng;
|
|
2111
|
+
/**
|
|
2112
|
+
* Build a decision tree classifier from the training set (X, y).
|
|
2113
|
+
*
|
|
2114
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
2115
|
+
* @param y - Target class labels of shape (n_samples,)
|
|
2116
|
+
* @returns this - The fitted estimator
|
|
2117
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
2118
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
2119
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
2120
|
+
*/
|
|
2121
|
+
fit(X: Tensor, y: Tensor): this;
|
|
2122
|
+
private buildTree;
|
|
2123
|
+
private getMajorityClass;
|
|
2124
|
+
private getClassProbabilities;
|
|
2125
|
+
private findBestSplit;
|
|
2126
|
+
private giniFromCounts;
|
|
2127
|
+
/**
|
|
2128
|
+
* Predict class labels for samples in X.
|
|
2129
|
+
*
|
|
2130
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
2131
|
+
* @returns Predicted class labels of shape (n_samples,)
|
|
2132
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
2133
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
2134
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
2135
|
+
*/
|
|
2136
|
+
predict(X: Tensor): Tensor;
|
|
2137
|
+
private predictSample;
|
|
2138
|
+
/**
|
|
2139
|
+
* Predict class probabilities for samples in X.
|
|
2140
|
+
*
|
|
2141
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
2142
|
+
* @returns Class probability matrix of shape (n_samples, n_classes)
|
|
2143
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
2144
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
2145
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
2146
|
+
*/
|
|
2147
|
+
predictProba(X: Tensor): Tensor;
|
|
2148
|
+
private predictLeaf;
|
|
2149
|
+
/**
|
|
2150
|
+
* Return the mean accuracy on the given test data and labels.
|
|
2151
|
+
*
|
|
2152
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
2153
|
+
* @param y - True labels of shape (n_samples,)
|
|
2154
|
+
* @returns Accuracy score in range [0, 1]
|
|
2155
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
2156
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
2157
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
2158
|
+
*/
|
|
2159
|
+
score(X: Tensor, y: Tensor): number;
|
|
2160
|
+
/**
|
|
2161
|
+
* Get the unique class labels discovered during fitting.
|
|
2162
|
+
*
|
|
2163
|
+
* @returns Tensor of class labels or undefined if not fitted
|
|
2164
|
+
*/
|
|
2165
|
+
get classes(): Tensor | undefined;
|
|
2166
|
+
/**
|
|
2167
|
+
* Get hyperparameters for this estimator.
|
|
2168
|
+
*
|
|
2169
|
+
* @returns Object containing all hyperparameters
|
|
2170
|
+
*/
|
|
2171
|
+
getParams(): Record<string, unknown>;
|
|
2172
|
+
/**
|
|
2173
|
+
* Set the parameters of this estimator.
|
|
2174
|
+
*
|
|
2175
|
+
* @param _params - Parameters to set
|
|
2176
|
+
* @throws {NotImplementedError} Always — parameters cannot be changed after construction
|
|
2177
|
+
*/
|
|
2178
|
+
setParams(_params: Record<string, unknown>): this;
|
|
2179
|
+
}
|
|
2180
|
+
/**
|
|
2181
|
+
* Decision Tree Regressor.
|
|
2182
|
+
*
|
|
2183
|
+
* Uses MSE reduction to find optimal splits for regression tasks.
|
|
2184
|
+
*
|
|
2185
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeRegressor.html | scikit-learn DecisionTreeRegressor}
|
|
2186
|
+
*/
|
|
2187
|
+
declare class DecisionTreeRegressor implements Regressor {
|
|
2188
|
+
private maxDepth;
|
|
2189
|
+
private minSamplesSplit;
|
|
2190
|
+
private minSamplesLeaf;
|
|
2191
|
+
private maxFeatures;
|
|
2192
|
+
private randomState;
|
|
2193
|
+
private tree?;
|
|
2194
|
+
private nFeatures?;
|
|
2195
|
+
private fitted;
|
|
2196
|
+
constructor(options?: {
|
|
2197
|
+
readonly maxDepth?: number;
|
|
2198
|
+
readonly minSamplesSplit?: number;
|
|
2199
|
+
readonly minSamplesLeaf?: number;
|
|
2200
|
+
readonly maxFeatures?: number;
|
|
2201
|
+
readonly randomState?: number;
|
|
2202
|
+
});
|
|
2203
|
+
private getRng;
|
|
2204
|
+
/**
|
|
2205
|
+
* Build a decision tree regressor from the training set (X, y).
|
|
2206
|
+
*
|
|
2207
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
2208
|
+
* @param y - Target values of shape (n_samples,)
|
|
2209
|
+
* @returns this - The fitted estimator
|
|
2210
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
2211
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
2212
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
2213
|
+
*/
|
|
2214
|
+
fit(X: Tensor, y: Tensor): this;
|
|
2215
|
+
private buildTree;
|
|
2216
|
+
private getMean;
|
|
2217
|
+
private findBestSplit;
|
|
2218
|
+
/**
|
|
2219
|
+
* Predict target values for samples in X.
|
|
2220
|
+
*
|
|
2221
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
2222
|
+
* @returns Predicted values of shape (n_samples,)
|
|
2223
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
2224
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
2225
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
2226
|
+
*/
|
|
2227
|
+
predict(X: Tensor): Tensor;
|
|
2228
|
+
private predictSample;
|
|
2229
|
+
/**
|
|
2230
|
+
* Return the R² score on the given test data and target values.
|
|
2231
|
+
*
|
|
2232
|
+
* R² = 1 - SS_res / SS_tot.
|
|
2233
|
+
*
|
|
2234
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
2235
|
+
* @param y - True target values of shape (n_samples,)
|
|
2236
|
+
* @returns R² score (best possible is 1.0, can be negative)
|
|
2237
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
2238
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
2239
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
2240
|
+
*/
|
|
2241
|
+
score(X: Tensor, y: Tensor): number;
|
|
2242
|
+
/**
|
|
2243
|
+
* Get hyperparameters for this estimator.
|
|
2244
|
+
*
|
|
2245
|
+
* @returns Object containing all hyperparameters
|
|
2246
|
+
*/
|
|
2247
|
+
getParams(): Record<string, unknown>;
|
|
2248
|
+
/**
|
|
2249
|
+
* Set the parameters of this estimator.
|
|
2250
|
+
*
|
|
2251
|
+
* @param _params - Parameters to set
|
|
2252
|
+
* @throws {NotImplementedError} Always — parameters cannot be changed after construction
|
|
2253
|
+
*/
|
|
2254
|
+
setParams(_params: Record<string, unknown>): this;
|
|
2255
|
+
}
|
|
2256
|
+
|
|
2257
|
+
/**
|
|
2258
|
+
* Random Forest Classifier.
|
|
2259
|
+
*
|
|
2260
|
+
* An ensemble of decision trees trained on random subsets of data and features.
|
|
2261
|
+
* Predictions are made by majority voting.
|
|
2262
|
+
*
|
|
2263
|
+
* **Algorithm**:
|
|
2264
|
+
* 1. Create n_estimators bootstrap samples from training data
|
|
2265
|
+
* 2. Train a decision tree on each sample with random feature subsets
|
|
2266
|
+
* 3. Aggregate predictions via majority voting
|
|
2267
|
+
*
|
|
2268
|
+
* @example
|
|
2269
|
+
* ```ts
|
|
2270
|
+
* import { RandomForestClassifier } from 'deepbox/ml';
|
|
2271
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
2272
|
+
*
|
|
2273
|
+
* const clf = new RandomForestClassifier({ nEstimators: 100 });
|
|
2274
|
+
* clf.fit(X_train, y_train);
|
|
2275
|
+
* const predictions = clf.predict(X_test);
|
|
2276
|
+
* ```
|
|
2277
|
+
*
|
|
2278
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html | scikit-learn RandomForestClassifier}
|
|
2279
|
+
*/
|
|
2280
|
+
declare class RandomForestClassifier implements Classifier {
|
|
2281
|
+
private readonly nEstimators;
|
|
2282
|
+
private readonly maxDepth;
|
|
2283
|
+
private readonly minSamplesSplit;
|
|
2284
|
+
private readonly minSamplesLeaf;
|
|
2285
|
+
private readonly maxFeatures;
|
|
2286
|
+
private readonly bootstrap;
|
|
2287
|
+
private readonly randomState?;
|
|
2288
|
+
private trees;
|
|
2289
|
+
private classLabels?;
|
|
2290
|
+
private nFeatures?;
|
|
2291
|
+
private fitted;
|
|
2292
|
+
constructor(options?: {
|
|
2293
|
+
readonly nEstimators?: number;
|
|
2294
|
+
readonly maxDepth?: number;
|
|
2295
|
+
readonly minSamplesSplit?: number;
|
|
2296
|
+
readonly minSamplesLeaf?: number;
|
|
2297
|
+
readonly maxFeatures?: "sqrt" | "log2" | number;
|
|
2298
|
+
readonly bootstrap?: boolean;
|
|
2299
|
+
readonly randomState?: number;
|
|
2300
|
+
});
|
|
2301
|
+
private createRNG;
|
|
2302
|
+
/**
|
|
2303
|
+
* Fit the random forest classifier on training data.
|
|
2304
|
+
*
|
|
2305
|
+
* Builds an ensemble of decision trees, each trained on a bootstrapped
|
|
2306
|
+
* sample with random feature subsets.
|
|
2307
|
+
*
|
|
2308
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
2309
|
+
* @param y - Target class labels of shape (n_samples,)
|
|
2310
|
+
* @returns this - The fitted estimator
|
|
2311
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
2312
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
2313
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
2314
|
+
*/
|
|
2315
|
+
fit(X: Tensor, y: Tensor): this;
|
|
2316
|
+
/**
|
|
2317
|
+
* Predict class labels for samples in X.
|
|
2318
|
+
*
|
|
2319
|
+
* Aggregates predictions from all trees via majority voting.
|
|
2320
|
+
*
|
|
2321
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
2322
|
+
* @returns Predicted class labels of shape (n_samples,)
|
|
2323
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
2324
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
2325
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
2326
|
+
*/
|
|
2327
|
+
predict(X: Tensor): Tensor;
|
|
2328
|
+
/**
|
|
2329
|
+
* Predict class probabilities for samples in X.
|
|
2330
|
+
*
|
|
2331
|
+
* Averages the predicted class probabilities from all trees in the ensemble.
|
|
2332
|
+
*
|
|
2333
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
2334
|
+
* @returns Class probability matrix of shape (n_samples, n_classes)
|
|
2335
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
2336
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
2337
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
2338
|
+
*/
|
|
2339
|
+
predictProba(X: Tensor): Tensor;
|
|
2340
|
+
/**
|
|
2341
|
+
* Return the mean accuracy on the given test data and labels.
|
|
2342
|
+
*
|
|
2343
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
2344
|
+
* @param y - True labels of shape (n_samples,)
|
|
2345
|
+
* @returns Accuracy score in range [0, 1]
|
|
2346
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
2347
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
2348
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
2349
|
+
*/
|
|
2350
|
+
score(X: Tensor, y: Tensor): number;
|
|
2351
|
+
/**
|
|
2352
|
+
* Get the unique class labels discovered during fitting.
|
|
2353
|
+
*
|
|
2354
|
+
* @returns Tensor of class labels or undefined if not fitted
|
|
2355
|
+
*/
|
|
2356
|
+
get classes(): Tensor | undefined;
|
|
2357
|
+
/**
|
|
2358
|
+
* Get hyperparameters for this estimator.
|
|
2359
|
+
*
|
|
2360
|
+
* @returns Object containing all hyperparameters
|
|
2361
|
+
*/
|
|
2362
|
+
getParams(): Record<string, unknown>;
|
|
2363
|
+
/**
|
|
2364
|
+
* Set the parameters of this estimator.
|
|
2365
|
+
*
|
|
2366
|
+
* @param _params - Parameters to set
|
|
2367
|
+
* @throws {NotImplementedError} Always — parameters cannot be changed after construction
|
|
2368
|
+
*/
|
|
2369
|
+
setParams(_params: Record<string, unknown>): this;
|
|
2370
|
+
}
|
|
2371
|
+
/**
|
|
2372
|
+
* Random Forest Regressor.
|
|
2373
|
+
*
|
|
2374
|
+
* An ensemble of decision tree regressors trained on random subsets of data
|
|
2375
|
+
* and features. Predictions are averaged across all trees.
|
|
2376
|
+
*
|
|
2377
|
+
* **Algorithm**:
|
|
2378
|
+
* 1. Create n_estimators bootstrap samples from training data
|
|
2379
|
+
* 2. Train a decision tree on each sample with random feature subsets
|
|
2380
|
+
* 3. Aggregate predictions via averaging
|
|
2381
|
+
*
|
|
2382
|
+
* @example
|
|
2383
|
+
* ```ts
|
|
2384
|
+
* import { RandomForestRegressor } from 'deepbox/ml';
|
|
2385
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
2386
|
+
*
|
|
2387
|
+
* const reg = new RandomForestRegressor({ nEstimators: 100 });
|
|
2388
|
+
* reg.fit(X_train, y_train);
|
|
2389
|
+
* const predictions = reg.predict(X_test);
|
|
2390
|
+
* ```
|
|
2391
|
+
*
|
|
2392
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html | scikit-learn RandomForestRegressor}
|
|
2393
|
+
*/
|
|
2394
|
+
declare class RandomForestRegressor implements Regressor {
|
|
2395
|
+
private readonly nEstimators;
|
|
2396
|
+
private readonly maxDepth;
|
|
2397
|
+
private readonly minSamplesSplit;
|
|
2398
|
+
private readonly minSamplesLeaf;
|
|
2399
|
+
private readonly maxFeatures;
|
|
2400
|
+
private readonly bootstrap;
|
|
2401
|
+
private readonly randomState?;
|
|
2402
|
+
private trees;
|
|
2403
|
+
private nFeatures?;
|
|
2404
|
+
private fitted;
|
|
2405
|
+
constructor(options?: {
|
|
2406
|
+
readonly nEstimators?: number;
|
|
2407
|
+
readonly maxDepth?: number;
|
|
2408
|
+
readonly minSamplesSplit?: number;
|
|
2409
|
+
readonly minSamplesLeaf?: number;
|
|
2410
|
+
readonly maxFeatures?: "sqrt" | "log2" | number;
|
|
2411
|
+
readonly bootstrap?: boolean;
|
|
2412
|
+
readonly randomState?: number;
|
|
2413
|
+
});
|
|
2414
|
+
private createRNG;
|
|
2415
|
+
/**
|
|
2416
|
+
* Fit the random forest regressor on training data.
|
|
2417
|
+
*
|
|
2418
|
+
* Builds an ensemble of decision trees, each trained on a bootstrapped
|
|
2419
|
+
* sample with random feature subsets.
|
|
2420
|
+
*
|
|
2421
|
+
* @param X - Training data of shape (n_samples, n_features)
|
|
2422
|
+
* @param y - Target values of shape (n_samples,)
|
|
2423
|
+
* @returns this - The fitted estimator
|
|
2424
|
+
* @throws {ShapeError} If X is not 2D or y is not 1D
|
|
2425
|
+
* @throws {ShapeError} If X and y have different number of samples
|
|
2426
|
+
* @throws {DataValidationError} If X or y contain NaN/Inf values
|
|
2427
|
+
*/
|
|
2428
|
+
fit(X: Tensor, y: Tensor): this;
|
|
2429
|
+
/**
|
|
2430
|
+
* Predict target values for samples in X.
|
|
2431
|
+
*
|
|
2432
|
+
* Averages predictions from all trees in the ensemble.
|
|
2433
|
+
*
|
|
2434
|
+
* @param X - Samples of shape (n_samples, n_features)
|
|
2435
|
+
* @returns Predicted values of shape (n_samples,)
|
|
2436
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
2437
|
+
* @throws {ShapeError} If X has wrong dimensions or feature count
|
|
2438
|
+
* @throws {DataValidationError} If X contains NaN/Inf values
|
|
2439
|
+
*/
|
|
2440
|
+
predict(X: Tensor): Tensor;
|
|
2441
|
+
/**
|
|
2442
|
+
* Return the R² score on the given test data and target values.
|
|
2443
|
+
*
|
|
2444
|
+
* R² = 1 - SS_res / SS_tot, where SS_res = Σ(y - ŷ)² and SS_tot = Σ(y - ȳ)².
|
|
2445
|
+
*
|
|
2446
|
+
* @param X - Test samples of shape (n_samples, n_features)
|
|
2447
|
+
* @param y - True target values of shape (n_samples,)
|
|
2448
|
+
* @returns R² score (best possible is 1.0, can be negative)
|
|
2449
|
+
* @throws {NotFittedError} If the model has not been fitted
|
|
2450
|
+
* @throws {ShapeError} If y is not 1-dimensional or sample counts mismatch
|
|
2451
|
+
* @throws {DataValidationError} If y contains NaN/Inf values
|
|
2452
|
+
*/
|
|
2453
|
+
score(X: Tensor, y: Tensor): number;
|
|
2454
|
+
/**
|
|
2455
|
+
* Get hyperparameters for this estimator.
|
|
2456
|
+
*
|
|
2457
|
+
* @returns Object containing all hyperparameters
|
|
2458
|
+
*/
|
|
2459
|
+
getParams(): Record<string, unknown>;
|
|
2460
|
+
/**
|
|
2461
|
+
* Set the parameters of this estimator.
|
|
2462
|
+
*
|
|
2463
|
+
* @param _params - Parameters to set
|
|
2464
|
+
* @throws {NotImplementedError} Always — parameters cannot be changed after construction
|
|
2465
|
+
*/
|
|
2466
|
+
setParams(_params: Record<string, unknown>): this;
|
|
2467
|
+
}
|
|
2468
|
+
|
|
2469
|
+
type index_Classifier = Classifier;
|
|
2470
|
+
type index_Clusterer = Clusterer;
|
|
2471
|
+
type index_DBSCAN = DBSCAN;
|
|
2472
|
+
declare const index_DBSCAN: typeof DBSCAN;
|
|
2473
|
+
type index_DecisionTreeClassifier = DecisionTreeClassifier;
|
|
2474
|
+
declare const index_DecisionTreeClassifier: typeof DecisionTreeClassifier;
|
|
2475
|
+
type index_DecisionTreeRegressor = DecisionTreeRegressor;
|
|
2476
|
+
declare const index_DecisionTreeRegressor: typeof DecisionTreeRegressor;
|
|
2477
|
+
type index_Estimator<FitParams = void> = Estimator<FitParams>;
|
|
2478
|
+
type index_GaussianNB = GaussianNB;
|
|
2479
|
+
declare const index_GaussianNB: typeof GaussianNB;
|
|
2480
|
+
type index_GradientBoostingClassifier = GradientBoostingClassifier;
|
|
2481
|
+
declare const index_GradientBoostingClassifier: typeof GradientBoostingClassifier;
|
|
2482
|
+
type index_GradientBoostingRegressor = GradientBoostingRegressor;
|
|
2483
|
+
declare const index_GradientBoostingRegressor: typeof GradientBoostingRegressor;
|
|
2484
|
+
type index_KMeans = KMeans;
|
|
2485
|
+
declare const index_KMeans: typeof KMeans;
|
|
2486
|
+
type index_KNeighborsClassifier = KNeighborsClassifier;
|
|
2487
|
+
declare const index_KNeighborsClassifier: typeof KNeighborsClassifier;
|
|
2488
|
+
type index_KNeighborsRegressor = KNeighborsRegressor;
|
|
2489
|
+
declare const index_KNeighborsRegressor: typeof KNeighborsRegressor;
|
|
2490
|
+
type index_Lasso = Lasso;
|
|
2491
|
+
declare const index_Lasso: typeof Lasso;
|
|
2492
|
+
type index_LinearRegression = LinearRegression;
|
|
2493
|
+
declare const index_LinearRegression: typeof LinearRegression;
|
|
2494
|
+
type index_LinearSVC = LinearSVC;
|
|
2495
|
+
declare const index_LinearSVC: typeof LinearSVC;
|
|
2496
|
+
type index_LinearSVR = LinearSVR;
|
|
2497
|
+
declare const index_LinearSVR: typeof LinearSVR;
|
|
2498
|
+
type index_LogisticRegression = LogisticRegression;
|
|
2499
|
+
declare const index_LogisticRegression: typeof LogisticRegression;
|
|
2500
|
+
type index_OutlierDetector = OutlierDetector;
|
|
2501
|
+
type index_PCA = PCA;
|
|
2502
|
+
declare const index_PCA: typeof PCA;
|
|
2503
|
+
type index_RandomForestClassifier = RandomForestClassifier;
|
|
2504
|
+
declare const index_RandomForestClassifier: typeof RandomForestClassifier;
|
|
2505
|
+
type index_RandomForestRegressor = RandomForestRegressor;
|
|
2506
|
+
declare const index_RandomForestRegressor: typeof RandomForestRegressor;
|
|
2507
|
+
type index_Regressor = Regressor;
|
|
2508
|
+
type index_Ridge = Ridge;
|
|
2509
|
+
declare const index_Ridge: typeof Ridge;
|
|
2510
|
+
type index_TSNE = TSNE;
|
|
2511
|
+
declare const index_TSNE: typeof TSNE;
|
|
2512
|
+
type index_Transformer = Transformer;
|
|
2513
|
+
declare namespace index {
|
|
2514
|
+
export { type index_Classifier as Classifier, type index_Clusterer as Clusterer, index_DBSCAN as DBSCAN, index_DecisionTreeClassifier as DecisionTreeClassifier, index_DecisionTreeRegressor as DecisionTreeRegressor, type index_Estimator as Estimator, index_GaussianNB as GaussianNB, index_GradientBoostingClassifier as GradientBoostingClassifier, index_GradientBoostingRegressor as GradientBoostingRegressor, index_KMeans as KMeans, index_KNeighborsClassifier as KNeighborsClassifier, index_KNeighborsRegressor as KNeighborsRegressor, index_Lasso as Lasso, index_LinearRegression as LinearRegression, index_LinearSVC as LinearSVC, index_LinearSVR as LinearSVR, index_LogisticRegression as LogisticRegression, type index_OutlierDetector as OutlierDetector, index_PCA as PCA, index_RandomForestClassifier as RandomForestClassifier, index_RandomForestRegressor as RandomForestRegressor, type index_Regressor as Regressor, index_Ridge as Ridge, index_TSNE as TSNE, type index_Transformer as Transformer };
|
|
2515
|
+
}
|
|
2516
|
+
|
|
2517
|
+
export { type Classifier as C, DBSCAN as D, type Estimator as E, GradientBoostingClassifier as G, KMeans as K, Lasso as L, type OutlierDetector as O, PCA as P, type Regressor as R, type Transformer as T, type Clusterer as a, GradientBoostingRegressor as b, LinearRegression as c, LogisticRegression as d, Ridge as e, TSNE as f, GaussianNB as g, KNeighborsClassifier as h, index as i, KNeighborsRegressor as j, LinearSVC as k, LinearSVR as l, DecisionTreeClassifier as m, DecisionTreeRegressor as n, RandomForestClassifier as o, RandomForestRegressor as p };
|