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,1180 @@
|
|
|
1
|
+
import { T as Tensor } from './Tensor-BQLk1ltW.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Calculates the accuracy classification score.
|
|
5
|
+
*
|
|
6
|
+
* Accuracy is the fraction of predictions that match the true labels.
|
|
7
|
+
* It's the most intuitive performance measure but can be misleading
|
|
8
|
+
* for imbalanced datasets.
|
|
9
|
+
*
|
|
10
|
+
* **Formula**: accuracy = (correct predictions) / (total predictions)
|
|
11
|
+
*
|
|
12
|
+
* **Time Complexity**: O(n) where n is the number of samples
|
|
13
|
+
* **Space Complexity**: O(1)
|
|
14
|
+
*
|
|
15
|
+
* @param yTrue - Ground truth (correct) target values
|
|
16
|
+
* @param yPred - Estimated targets as returned by a classifier
|
|
17
|
+
* @returns Accuracy score in range [0, 1], where 1 is perfect accuracy
|
|
18
|
+
*
|
|
19
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
20
|
+
* @throws {DTypeError} If yTrue and yPred use incompatible label types
|
|
21
|
+
* @throws {DataValidationError} If numeric labels contain NaN or infinite values
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { accuracy, tensor } from 'deepbox/core';
|
|
26
|
+
*
|
|
27
|
+
* const yTrue = tensor([0, 1, 1, 0, 1]);
|
|
28
|
+
* const yPred = tensor([0, 1, 0, 0, 1]);
|
|
29
|
+
* const acc = accuracy(yTrue, yPred); // 0.8 (4 out of 5 correct)
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html | Scikit-learn accuracy_score}
|
|
33
|
+
*/
|
|
34
|
+
declare function accuracy(yTrue: Tensor, yPred: Tensor): number;
|
|
35
|
+
/**
|
|
36
|
+
* Calculates the precision classification score.
|
|
37
|
+
*
|
|
38
|
+
* Precision is the ratio of true positives to all positive predictions.
|
|
39
|
+
* It answers: "Of all samples predicted as positive, how many are actually positive?"
|
|
40
|
+
* High precision means low false positive rate.
|
|
41
|
+
*
|
|
42
|
+
* **Formula**: precision = TP / (TP + FP)
|
|
43
|
+
*
|
|
44
|
+
* **Time Complexity**: O(n) for binary, O(n*k) for multiclass where k is number of classes
|
|
45
|
+
* **Space Complexity**: O(k) for multiclass
|
|
46
|
+
*
|
|
47
|
+
* @param yTrue - Ground truth (correct) target values
|
|
48
|
+
* @param yPred - Estimated targets as returned by a classifier
|
|
49
|
+
* @param average - Averaging strategy: 'binary', 'micro', 'macro', 'weighted', or null
|
|
50
|
+
* - 'binary': Calculate metrics for positive class only (default)
|
|
51
|
+
* - 'micro': Calculate metrics globally by counting total TP, FP, FN
|
|
52
|
+
* - 'macro': Calculate metrics for each class, return unweighted mean
|
|
53
|
+
* - 'weighted': Calculate metrics for each class, return weighted mean by support
|
|
54
|
+
* - null: Return array of scores for each class
|
|
55
|
+
* @returns Precision score(s) in range [0, 1]
|
|
56
|
+
*
|
|
57
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
58
|
+
* @throws {DTypeError} If labels are not numeric (string or bigint)
|
|
59
|
+
* @throws {DataValidationError} If labels contain NaN or infinite values
|
|
60
|
+
* @throws {InvalidParameterError} If average is invalid or binary labels are not in {0,1}
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { precision, tensor } from 'deepbox/core';
|
|
65
|
+
*
|
|
66
|
+
* // Binary classification
|
|
67
|
+
* const yTrue = tensor([0, 1, 1, 0, 1]);
|
|
68
|
+
* const yPred = tensor([0, 1, 0, 0, 1]);
|
|
69
|
+
* const prec = precision(yTrue, yPred); // 1.0 (2 TP, 0 FP)
|
|
70
|
+
*
|
|
71
|
+
* // Multiclass
|
|
72
|
+
* const yTrueMulti = tensor([0, 1, 2, 0, 1, 2]);
|
|
73
|
+
* const yPredMulti = tensor([0, 2, 1, 0, 0, 1]);
|
|
74
|
+
* const precMacro = precision(yTrueMulti, yPredMulti, 'macro');
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_score.html | Scikit-learn precision_score}
|
|
78
|
+
*/
|
|
79
|
+
declare function precision(yTrue: Tensor, yPred: Tensor): number;
|
|
80
|
+
declare function precision(yTrue: Tensor, yPred: Tensor, average: "binary" | "micro" | "macro" | "weighted"): number;
|
|
81
|
+
declare function precision(yTrue: Tensor, yPred: Tensor, average: null): number[];
|
|
82
|
+
/**
|
|
83
|
+
* Calculates the recall classification score (sensitivity, true positive rate).
|
|
84
|
+
*
|
|
85
|
+
* Recall is the ratio of true positives to all actual positive samples.
|
|
86
|
+
* It answers: "Of all actual positive samples, how many did we correctly identify?"
|
|
87
|
+
* High recall means low false negative rate.
|
|
88
|
+
*
|
|
89
|
+
* **Formula**: recall = TP / (TP + FN)
|
|
90
|
+
*
|
|
91
|
+
* **Time Complexity**: O(n) for binary, O(n*k) for multiclass where k is number of classes
|
|
92
|
+
* **Space Complexity**: O(k) for multiclass
|
|
93
|
+
*
|
|
94
|
+
* @param yTrue - Ground truth (correct) target values
|
|
95
|
+
* @param yPred - Estimated targets as returned by a classifier
|
|
96
|
+
* @param average - Averaging strategy: 'binary', 'micro', 'macro', 'weighted', or null
|
|
97
|
+
* @returns Recall score(s) in range [0, 1]
|
|
98
|
+
*
|
|
99
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
100
|
+
* @throws {DTypeError} If labels are not numeric (string or bigint)
|
|
101
|
+
* @throws {DataValidationError} If labels contain NaN or infinite values
|
|
102
|
+
* @throws {InvalidParameterError} If average is invalid or binary labels are not in {0,1}
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* import { recall, tensor } from 'deepbox/core';
|
|
107
|
+
*
|
|
108
|
+
* const yTrue = tensor([0, 1, 1, 0, 1]);
|
|
109
|
+
* const yPred = tensor([0, 1, 0, 0, 1]);
|
|
110
|
+
* const rec = recall(yTrue, yPred); // 0.667 (2 out of 3 positives found)
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html | Scikit-learn recall_score}
|
|
114
|
+
*/
|
|
115
|
+
declare function recall(yTrue: Tensor, yPred: Tensor): number;
|
|
116
|
+
declare function recall(yTrue: Tensor, yPred: Tensor, average: "binary" | "micro" | "macro" | "weighted"): number;
|
|
117
|
+
declare function recall(yTrue: Tensor, yPred: Tensor, average: null): number[];
|
|
118
|
+
/**
|
|
119
|
+
* Calculates the F1 score (harmonic mean of precision and recall).
|
|
120
|
+
*
|
|
121
|
+
* F1 score is the harmonic mean of precision and recall, providing a single
|
|
122
|
+
* metric that balances both concerns. It's especially useful when you need
|
|
123
|
+
* to balance false positives and false negatives.
|
|
124
|
+
*
|
|
125
|
+
* **Formula**: F1 = 2 * (precision * recall) / (precision + recall)
|
|
126
|
+
*
|
|
127
|
+
* **Time Complexity**: O(n) for binary, O(n*k) for multiclass
|
|
128
|
+
* **Space Complexity**: O(k) for multiclass
|
|
129
|
+
*
|
|
130
|
+
* @param yTrue - Ground truth (correct) target values
|
|
131
|
+
* @param yPred - Estimated targets as returned by a classifier
|
|
132
|
+
* @param average - Averaging strategy: 'binary', 'micro', 'macro', 'weighted', or null
|
|
133
|
+
* @returns F1 score(s) in range [0, 1]
|
|
134
|
+
*
|
|
135
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
136
|
+
* @throws {DTypeError} If labels are not numeric (string or bigint)
|
|
137
|
+
* @throws {DataValidationError} If labels contain NaN or infinite values
|
|
138
|
+
* @throws {InvalidParameterError} If average is invalid or binary labels are not in {0,1}
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* import { f1Score, tensor } from 'deepbox/core';
|
|
143
|
+
*
|
|
144
|
+
* const yTrue = tensor([0, 1, 1, 0, 1]);
|
|
145
|
+
* const yPred = tensor([0, 1, 0, 0, 1]);
|
|
146
|
+
* const f1 = f1Score(yTrue, yPred); // 0.8
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html | Scikit-learn f1_score}
|
|
150
|
+
*/
|
|
151
|
+
declare function f1Score(yTrue: Tensor, yPred: Tensor): number;
|
|
152
|
+
declare function f1Score(yTrue: Tensor, yPred: Tensor, average: "binary" | "micro" | "macro" | "weighted"): number;
|
|
153
|
+
declare function f1Score(yTrue: Tensor, yPred: Tensor, average: null): number[];
|
|
154
|
+
/**
|
|
155
|
+
* Calculates the F-beta score.
|
|
156
|
+
*
|
|
157
|
+
* F-beta score is a weighted harmonic mean of precision and recall, where
|
|
158
|
+
* beta controls the trade-off between precision and recall.
|
|
159
|
+
* - beta < 1: More weight on precision
|
|
160
|
+
* - beta = 1: Equal weight (equivalent to F1 score)
|
|
161
|
+
* - beta > 1: More weight on recall
|
|
162
|
+
*
|
|
163
|
+
* **Formula**: F_beta = (1 + beta²) * (precision * recall) / (beta² * precision + recall)
|
|
164
|
+
*
|
|
165
|
+
* **Time Complexity**: O(n) for binary, O(n*k) for multiclass
|
|
166
|
+
* **Space Complexity**: O(k) for multiclass
|
|
167
|
+
*
|
|
168
|
+
* @param yTrue - Ground truth (correct) target values
|
|
169
|
+
* @param yPred - Estimated targets as returned by a classifier
|
|
170
|
+
* @param beta - Weight of recall vs precision (beta > 1 favors recall, beta < 1 favors precision)
|
|
171
|
+
* @param average - Averaging strategy: 'binary', 'micro', 'macro', 'weighted', or null
|
|
172
|
+
* @returns F-beta score(s) in range [0, 1]
|
|
173
|
+
*
|
|
174
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
175
|
+
* @throws {DTypeError} If labels are not numeric (string or bigint)
|
|
176
|
+
* @throws {DataValidationError} If labels contain NaN or infinite values
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* import { fbetaScore, tensor } from 'deepbox/core';
|
|
181
|
+
*
|
|
182
|
+
* const yTrue = tensor([0, 1, 1, 0, 1]);
|
|
183
|
+
* const yPred = tensor([0, 1, 0, 0, 1]);
|
|
184
|
+
* const fb2 = fbetaScore(yTrue, yPred, 2); // Favors recall
|
|
185
|
+
* const fb05 = fbetaScore(yTrue, yPred, 0.5); // Favors precision
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.fbeta_score.html | Scikit-learn fbeta_score}
|
|
189
|
+
*/
|
|
190
|
+
declare function fbetaScore(yTrue: Tensor, yPred: Tensor, beta: number): number;
|
|
191
|
+
declare function fbetaScore(yTrue: Tensor, yPred: Tensor, beta: number, average: "binary" | "micro" | "macro" | "weighted"): number;
|
|
192
|
+
declare function fbetaScore(yTrue: Tensor, yPred: Tensor, beta: number, average: null): number[];
|
|
193
|
+
/**
|
|
194
|
+
* Computes the confusion matrix to evaluate classification accuracy.
|
|
195
|
+
*
|
|
196
|
+
* A confusion matrix is a table showing the counts of correct and incorrect
|
|
197
|
+
* predictions broken down by each class. Rows represent true labels,
|
|
198
|
+
* columns represent predicted labels.
|
|
199
|
+
*
|
|
200
|
+
* **Time Complexity**: O(n + k²) where n is number of samples, k is number of classes
|
|
201
|
+
* **Space Complexity**: O(k²)
|
|
202
|
+
*
|
|
203
|
+
* @param yTrue - Ground truth (correct) target values
|
|
204
|
+
* @param yPred - Estimated targets as returned by a classifier
|
|
205
|
+
* @returns Confusion matrix as a 2D tensor of shape [n_classes, n_classes]
|
|
206
|
+
*
|
|
207
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
208
|
+
* @throws {DTypeError} If labels are not numeric (string or bigint)
|
|
209
|
+
* @throws {DataValidationError} If labels contain NaN or infinite values
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```ts
|
|
213
|
+
* import { confusionMatrix, tensor } from 'deepbox/core';
|
|
214
|
+
*
|
|
215
|
+
* const yTrue = tensor([0, 1, 1, 0, 1]);
|
|
216
|
+
* const yPred = tensor([0, 1, 0, 0, 1]);
|
|
217
|
+
* const cm = confusionMatrix(yTrue, yPred);
|
|
218
|
+
* // [[2, 0],
|
|
219
|
+
* // [1, 2]]
|
|
220
|
+
* ```
|
|
221
|
+
*
|
|
222
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html | Scikit-learn confusion_matrix}
|
|
223
|
+
*/
|
|
224
|
+
declare function confusionMatrix(yTrue: Tensor, yPred: Tensor): Tensor;
|
|
225
|
+
/**
|
|
226
|
+
* Generates a text classification report showing main classification metrics.
|
|
227
|
+
*
|
|
228
|
+
* Provides a comprehensive summary including precision, recall, F1-score,
|
|
229
|
+
* and accuracy for the classification task.
|
|
230
|
+
*
|
|
231
|
+
* **Time Complexity**: O(n * k) where n is the number of samples and k is the number of classes
|
|
232
|
+
* **Space Complexity**: O(k) where k is the number of classes
|
|
233
|
+
*
|
|
234
|
+
* @param yTrue - Ground truth (correct) binary target values (0 or 1)
|
|
235
|
+
* @param yPred - Estimated binary targets as returned by a classifier (0 or 1)
|
|
236
|
+
* @returns Formatted string report with per-class and aggregate classification metrics
|
|
237
|
+
*
|
|
238
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
239
|
+
* @throws {DTypeError} If labels are not numeric (string or int64)
|
|
240
|
+
* @throws {DataValidationError} If labels contain NaN or infinite values
|
|
241
|
+
* @throws {InvalidParameterError} If labels are not binary (0 or 1)
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* import { classificationReport, tensor } from 'deepbox/core';
|
|
246
|
+
*
|
|
247
|
+
* const yTrue = tensor([0, 1, 1, 0, 1]);
|
|
248
|
+
* const yPred = tensor([0, 1, 0, 0, 1]);
|
|
249
|
+
* console.log(classificationReport(yTrue, yPred));
|
|
250
|
+
* // Classification Report:
|
|
251
|
+
* // Precision: 1.0000
|
|
252
|
+
* // Recall: 0.6667
|
|
253
|
+
* // F1-Score: 0.8000
|
|
254
|
+
* // Accuracy: 0.8000
|
|
255
|
+
* ```
|
|
256
|
+
*
|
|
257
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html | Scikit-learn classification_report}
|
|
258
|
+
*/
|
|
259
|
+
declare function classificationReport(yTrue: Tensor, yPred: Tensor): string;
|
|
260
|
+
/**
|
|
261
|
+
* ROC curve data.
|
|
262
|
+
*
|
|
263
|
+
* Computes Receiver Operating Characteristic (ROC) curve for binary classification.
|
|
264
|
+
* The ROC curve shows the trade-off between true positive rate and false positive rate
|
|
265
|
+
* at various threshold settings.
|
|
266
|
+
*
|
|
267
|
+
* **Returns**: [fpr, tpr, thresholds]
|
|
268
|
+
* - fpr: False positive rates
|
|
269
|
+
* - tpr: True positive rates
|
|
270
|
+
* - thresholds: Decision thresholds (in descending order)
|
|
271
|
+
*
|
|
272
|
+
* **Edge Cases**:
|
|
273
|
+
* - Returns empty tensors if yTrue contains only one class
|
|
274
|
+
* - Handles tied scores by grouping them at the same threshold
|
|
275
|
+
*
|
|
276
|
+
* @param yTrue - Ground truth binary labels (must be 0 or 1)
|
|
277
|
+
* @param yScore - Target scores (higher score = more likely positive class)
|
|
278
|
+
* @returns Tuple of [fpr, tpr, thresholds] tensors
|
|
279
|
+
*
|
|
280
|
+
* @throws {ShapeError} If yTrue and yScore have different sizes or are not 1D/column vectors
|
|
281
|
+
* @throws {DTypeError} If inputs are not numeric (string or int64)
|
|
282
|
+
* @throws {InvalidParameterError} If yTrue contains non-binary values
|
|
283
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
284
|
+
*
|
|
285
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html | Scikit-learn roc_curve}
|
|
286
|
+
*/
|
|
287
|
+
declare function rocCurve(yTrue: Tensor, yScore: Tensor): [Tensor, Tensor, Tensor];
|
|
288
|
+
/**
|
|
289
|
+
* Area Under ROC Curve (AUC-ROC).
|
|
290
|
+
*
|
|
291
|
+
* Computes the Area Under the Receiver Operating Characteristic Curve.
|
|
292
|
+
* AUC represents the probability that a randomly chosen positive sample
|
|
293
|
+
* is ranked higher than a randomly chosen negative sample.
|
|
294
|
+
*
|
|
295
|
+
* **Range**: [0, 1], where 1 is perfect and 0.5 is random.
|
|
296
|
+
*
|
|
297
|
+
* **Time Complexity**: O(n log n) due to sorting
|
|
298
|
+
* **Space Complexity**: O(n)
|
|
299
|
+
*
|
|
300
|
+
* @param yTrue - Ground truth binary labels (must be 0 or 1)
|
|
301
|
+
* @param yScore - Target scores (higher score = more likely positive class)
|
|
302
|
+
* @returns AUC score in range [0, 1], or 0.5 if ROC curve cannot be computed
|
|
303
|
+
*
|
|
304
|
+
* @throws {ShapeError} If yTrue and yScore have different sizes or are not 1D/column vectors
|
|
305
|
+
* @throws {DTypeError} If inputs are not numeric (string or int64)
|
|
306
|
+
* @throws {InvalidParameterError} If yTrue contains non-binary values
|
|
307
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```ts
|
|
311
|
+
* import { rocAucScore, tensor } from 'deepbox/metrics';
|
|
312
|
+
*
|
|
313
|
+
* const yTrue = tensor([0, 0, 1, 1]);
|
|
314
|
+
* const yScore = tensor([0.1, 0.4, 0.35, 0.8]);
|
|
315
|
+
* const auc = rocAucScore(yTrue, yScore); // ~0.75
|
|
316
|
+
* ```
|
|
317
|
+
*
|
|
318
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_auc_score.html | Scikit-learn roc_auc_score}
|
|
319
|
+
*/
|
|
320
|
+
declare function rocAucScore(yTrue: Tensor, yScore: Tensor): number;
|
|
321
|
+
/**
|
|
322
|
+
* Precision-Recall curve.
|
|
323
|
+
*
|
|
324
|
+
* Computes precision-recall pairs for different probability thresholds.
|
|
325
|
+
* Useful for evaluating classifiers on imbalanced datasets where ROC curves
|
|
326
|
+
* may be overly optimistic.
|
|
327
|
+
*
|
|
328
|
+
* **Returns**: [precision, recall, thresholds] as a tuple of tensors
|
|
329
|
+
* - precision: Precision values at each threshold
|
|
330
|
+
* - recall: Recall values at each threshold
|
|
331
|
+
* - thresholds: Decision thresholds (in descending order)
|
|
332
|
+
*
|
|
333
|
+
* **Time Complexity**: O(n log n) due to sorting
|
|
334
|
+
* **Space Complexity**: O(n)
|
|
335
|
+
*
|
|
336
|
+
* @param yTrue - Ground truth binary labels (0 or 1)
|
|
337
|
+
* @param yScore - Target scores (higher score = more likely positive class)
|
|
338
|
+
* @returns Tuple of [precision, recall, thresholds] tensors
|
|
339
|
+
*
|
|
340
|
+
* @throws {ShapeError} If yTrue and yScore have different sizes or are not 1D/column vectors
|
|
341
|
+
* @throws {DTypeError} If inputs are not numeric (string or int64)
|
|
342
|
+
* @throws {InvalidParameterError} If yTrue contains non-binary values
|
|
343
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```ts
|
|
347
|
+
* import { precisionRecallCurve, tensor } from 'deepbox/metrics';
|
|
348
|
+
*
|
|
349
|
+
* const yTrue = tensor([0, 0, 1, 1]);
|
|
350
|
+
* const yScore = tensor([0.1, 0.4, 0.35, 0.8]);
|
|
351
|
+
* const [prec, rec, thresh] = precisionRecallCurve(yTrue, yScore);
|
|
352
|
+
* ```
|
|
353
|
+
*
|
|
354
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_recall_curve.html | Scikit-learn precision_recall_curve}
|
|
355
|
+
*/
|
|
356
|
+
declare function precisionRecallCurve(yTrue: Tensor, yScore: Tensor): [Tensor, Tensor, Tensor];
|
|
357
|
+
/**
|
|
358
|
+
* Average precision score.
|
|
359
|
+
*
|
|
360
|
+
* Computes the average precision (AP) from prediction scores. AP summarizes
|
|
361
|
+
* a precision-recall curve as the weighted mean of precisions achieved at
|
|
362
|
+
* each threshold, with the increase in recall from the previous threshold
|
|
363
|
+
* used as the weight.
|
|
364
|
+
*
|
|
365
|
+
* **Range**: [0, 1], where 1 is perfect.
|
|
366
|
+
*
|
|
367
|
+
* **Time Complexity**: O(n log n) due to sorting
|
|
368
|
+
* **Space Complexity**: O(n)
|
|
369
|
+
*
|
|
370
|
+
* @param yTrue - Ground truth binary labels (0 or 1)
|
|
371
|
+
* @param yScore - Target scores (higher score = more likely positive class)
|
|
372
|
+
* @returns Average precision score in range [0, 1]
|
|
373
|
+
*
|
|
374
|
+
* @throws {ShapeError} If yTrue and yScore have different sizes or are not 1D/column vectors
|
|
375
|
+
* @throws {DTypeError} If inputs are not numeric (string or int64)
|
|
376
|
+
* @throws {InvalidParameterError} If yTrue contains non-binary values
|
|
377
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
* ```ts
|
|
381
|
+
* import { averagePrecisionScore, tensor } from 'deepbox/metrics';
|
|
382
|
+
*
|
|
383
|
+
* const yTrue = tensor([0, 0, 1, 1]);
|
|
384
|
+
* const yScore = tensor([0.1, 0.4, 0.35, 0.8]);
|
|
385
|
+
* const ap = averagePrecisionScore(yTrue, yScore);
|
|
386
|
+
* ```
|
|
387
|
+
*
|
|
388
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.average_precision_score.html | Scikit-learn average_precision_score}
|
|
389
|
+
*/
|
|
390
|
+
declare function averagePrecisionScore(yTrue: Tensor, yScore: Tensor): number;
|
|
391
|
+
/**
|
|
392
|
+
* Log loss (logistic loss, cross-entropy loss).
|
|
393
|
+
*
|
|
394
|
+
* Measures the performance of a classification model where the prediction is a probability
|
|
395
|
+
* value between 0 and 1. Lower log loss indicates better predictions.
|
|
396
|
+
*
|
|
397
|
+
* **Formula**: -1/n * Σ(y_true * log(y_pred) + (1 - y_true) * log(1 - y_pred))
|
|
398
|
+
*
|
|
399
|
+
* **Edge Cases**:
|
|
400
|
+
* - Predictions are clipped to [1e-15, 1-1e-15] to avoid log(0)
|
|
401
|
+
* - Returns 0 for empty inputs
|
|
402
|
+
*
|
|
403
|
+
* @param yTrue - Ground truth binary labels (0 or 1)
|
|
404
|
+
* @param yPred - Predicted probabilities (must be in range [0, 1])
|
|
405
|
+
* @returns Log loss value (lower is better, 0 is perfect)
|
|
406
|
+
*
|
|
407
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
408
|
+
* @throws {DTypeError} If inputs are not numeric (string or int64)
|
|
409
|
+
* @throws {InvalidParameterError} If yTrue is not binary or yPred is outside [0, 1]
|
|
410
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
411
|
+
*
|
|
412
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.log_loss.html | Scikit-learn log_loss}
|
|
413
|
+
*/
|
|
414
|
+
declare function logLoss(yTrue: Tensor, yPred: Tensor): number;
|
|
415
|
+
/**
|
|
416
|
+
* Hamming loss.
|
|
417
|
+
*
|
|
418
|
+
* Computes the fraction of labels that are incorrectly predicted.
|
|
419
|
+
* For binary classification, this equals 1 - accuracy.
|
|
420
|
+
*
|
|
421
|
+
* **Formula**: hamming_loss = (incorrect predictions) / (total predictions)
|
|
422
|
+
*
|
|
423
|
+
* **Range**: [0, 1], where 0 is perfect.
|
|
424
|
+
*
|
|
425
|
+
* **Time Complexity**: O(n)
|
|
426
|
+
* **Space Complexity**: O(1)
|
|
427
|
+
*
|
|
428
|
+
* @param yTrue - Ground truth target values
|
|
429
|
+
* @param yPred - Estimated targets as returned by a classifier
|
|
430
|
+
* @returns Hamming loss in range [0, 1]
|
|
431
|
+
*
|
|
432
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
433
|
+
* @throws {DTypeError} If yTrue and yPred use incompatible label types
|
|
434
|
+
* @throws {DataValidationError} If numeric labels contain NaN or infinite values
|
|
435
|
+
*
|
|
436
|
+
* @example
|
|
437
|
+
* ```ts
|
|
438
|
+
* import { hammingLoss, tensor } from 'deepbox/metrics';
|
|
439
|
+
*
|
|
440
|
+
* const yTrue = tensor([0, 1, 1, 0, 1]);
|
|
441
|
+
* const yPred = tensor([0, 1, 0, 0, 1]);
|
|
442
|
+
* const loss = hammingLoss(yTrue, yPred); // 0.2
|
|
443
|
+
* ```
|
|
444
|
+
*
|
|
445
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.hamming_loss.html | Scikit-learn hamming_loss}
|
|
446
|
+
*/
|
|
447
|
+
declare function hammingLoss(yTrue: Tensor, yPred: Tensor): number;
|
|
448
|
+
/**
|
|
449
|
+
* Jaccard similarity score (Intersection over Union).
|
|
450
|
+
*
|
|
451
|
+
* Computes the Jaccard similarity coefficient between two binary label sets.
|
|
452
|
+
* Also known as the Jaccard index or Intersection over Union (IoU).
|
|
453
|
+
*
|
|
454
|
+
* **Formula**: jaccard = TP / (TP + FP + FN)
|
|
455
|
+
*
|
|
456
|
+
* **Edge Cases**:
|
|
457
|
+
* - If both yTrue and yPred contain no positive labels (TP + FP + FN = 0),
|
|
458
|
+
* returns 1 to reflect perfect similarity of empty sets.
|
|
459
|
+
*
|
|
460
|
+
* **Range**: [0, 1], where 1 is perfect.
|
|
461
|
+
*
|
|
462
|
+
* **Time Complexity**: O(n)
|
|
463
|
+
* **Space Complexity**: O(1)
|
|
464
|
+
*
|
|
465
|
+
* @param yTrue - Ground truth binary labels (0 or 1)
|
|
466
|
+
* @param yPred - Predicted binary labels (0 or 1)
|
|
467
|
+
* @returns Jaccard score in range [0, 1]
|
|
468
|
+
*
|
|
469
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
470
|
+
* @throws {DTypeError} If labels are not numeric (string or bigint)
|
|
471
|
+
* @throws {DataValidationError} If labels contain NaN or infinite values
|
|
472
|
+
* @throws {InvalidParameterError} If labels are not binary (0 or 1)
|
|
473
|
+
*
|
|
474
|
+
* @example
|
|
475
|
+
* ```ts
|
|
476
|
+
* import { jaccardScore, tensor } from 'deepbox/metrics';
|
|
477
|
+
*
|
|
478
|
+
* const yTrue = tensor([0, 1, 1, 0, 1]);
|
|
479
|
+
* const yPred = tensor([0, 1, 0, 0, 1]);
|
|
480
|
+
* const score = jaccardScore(yTrue, yPred); // 0.667
|
|
481
|
+
* ```
|
|
482
|
+
*
|
|
483
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.jaccard_score.html | Scikit-learn jaccard_score}
|
|
484
|
+
*/
|
|
485
|
+
declare function jaccardScore(yTrue: Tensor, yPred: Tensor): number;
|
|
486
|
+
/**
|
|
487
|
+
* Matthews correlation coefficient (MCC).
|
|
488
|
+
*
|
|
489
|
+
* Computes the Matthews correlation coefficient, a balanced measure that
|
|
490
|
+
* can be used even if the classes are of very different sizes. MCC is
|
|
491
|
+
* considered one of the best metrics for binary classification.
|
|
492
|
+
*
|
|
493
|
+
* **Formula**: MCC = (TP*TN - FP*FN) / sqrt((TP+FP)(TP+FN)(TN+FP)(TN+FN))
|
|
494
|
+
*
|
|
495
|
+
* **Range**: [-1, 1], where 1 is perfect, 0 is random, -1 is inverse.
|
|
496
|
+
*
|
|
497
|
+
* **Time Complexity**: O(n)
|
|
498
|
+
* **Space Complexity**: O(1)
|
|
499
|
+
*
|
|
500
|
+
* @param yTrue - Ground truth binary labels (0 or 1)
|
|
501
|
+
* @param yPred - Predicted binary labels (0 or 1)
|
|
502
|
+
* @returns MCC score in range [-1, 1]
|
|
503
|
+
*
|
|
504
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
505
|
+
* @throws {DTypeError} If labels are not numeric (string or bigint)
|
|
506
|
+
* @throws {DataValidationError} If labels contain NaN or infinite values
|
|
507
|
+
*
|
|
508
|
+
* @example
|
|
509
|
+
* ```ts
|
|
510
|
+
* import { matthewsCorrcoef, tensor } from 'deepbox/metrics';
|
|
511
|
+
*
|
|
512
|
+
* const yTrue = tensor([0, 1, 1, 0, 1]);
|
|
513
|
+
* const yPred = tensor([0, 1, 0, 0, 1]);
|
|
514
|
+
* const mcc = matthewsCorrcoef(yTrue, yPred); // ~0.667
|
|
515
|
+
* ```
|
|
516
|
+
*
|
|
517
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html | Scikit-learn matthews_corrcoef}
|
|
518
|
+
*/
|
|
519
|
+
declare function matthewsCorrcoef(yTrue: Tensor, yPred: Tensor): number;
|
|
520
|
+
/**
|
|
521
|
+
* Cohen's kappa score.
|
|
522
|
+
*
|
|
523
|
+
* Computes Cohen's kappa, a statistic that measures inter-annotator agreement.
|
|
524
|
+
* It is generally thought to be a more robust measure than simple percent
|
|
525
|
+
* agreement since it takes into account the possibility of agreement by chance.
|
|
526
|
+
*
|
|
527
|
+
* **Formula**: kappa = (p_o - p_e) / (1 - p_e)
|
|
528
|
+
* - p_o: observed agreement
|
|
529
|
+
* - p_e: expected agreement by chance
|
|
530
|
+
*
|
|
531
|
+
* **Range**: [-1, 1], where 1 is perfect agreement, 0 is chance, <0 is worse than chance.
|
|
532
|
+
*
|
|
533
|
+
* **Time Complexity**: O(n)
|
|
534
|
+
* **Space Complexity**: O(k) where k is number of classes
|
|
535
|
+
*
|
|
536
|
+
* @param yTrue - Ground truth labels
|
|
537
|
+
* @param yPred - Predicted labels
|
|
538
|
+
* @returns Kappa score in range [-1, 1]
|
|
539
|
+
*
|
|
540
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
541
|
+
* @throws {DTypeError} If labels are not numeric (string or bigint)
|
|
542
|
+
* @throws {DataValidationError} If labels contain NaN or infinite values
|
|
543
|
+
*
|
|
544
|
+
* @example
|
|
545
|
+
* ```ts
|
|
546
|
+
* import { cohenKappaScore, tensor } from 'deepbox/metrics';
|
|
547
|
+
*
|
|
548
|
+
* const yTrue = tensor([0, 1, 1, 0, 1]);
|
|
549
|
+
* const yPred = tensor([0, 1, 0, 0, 1]);
|
|
550
|
+
* const kappa = cohenKappaScore(yTrue, yPred); // ~0.615
|
|
551
|
+
* ```
|
|
552
|
+
*
|
|
553
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.cohen_kappa_score.html | Scikit-learn cohen_kappa_score}
|
|
554
|
+
*/
|
|
555
|
+
declare function cohenKappaScore(yTrue: Tensor, yPred: Tensor): number;
|
|
556
|
+
/**
|
|
557
|
+
* Balanced accuracy score.
|
|
558
|
+
*
|
|
559
|
+
* Computes the balanced accuracy, which is the macro-averaged recall.
|
|
560
|
+
* It is useful for imbalanced datasets where regular accuracy can be misleading.
|
|
561
|
+
*
|
|
562
|
+
* **Formula**: balanced_accuracy = (1/n_classes) * Σ(recall_per_class)
|
|
563
|
+
*
|
|
564
|
+
* **Range**: [0, 1], where 1 is perfect.
|
|
565
|
+
*
|
|
566
|
+
* **Time Complexity**: O(n * k) where k is number of classes
|
|
567
|
+
* **Space Complexity**: O(k)
|
|
568
|
+
*
|
|
569
|
+
* @param yTrue - Ground truth labels
|
|
570
|
+
* @param yPred - Predicted labels
|
|
571
|
+
* @returns Balanced accuracy score in range [0, 1]
|
|
572
|
+
*
|
|
573
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
574
|
+
* @throws {DTypeError} If labels are not numeric (string or bigint)
|
|
575
|
+
* @throws {DataValidationError} If labels contain NaN or infinite values
|
|
576
|
+
*
|
|
577
|
+
* @example
|
|
578
|
+
* ```ts
|
|
579
|
+
* import { balancedAccuracyScore, tensor } from 'deepbox/metrics';
|
|
580
|
+
*
|
|
581
|
+
* const yTrue = tensor([0, 0, 0, 0, 1]); // Imbalanced
|
|
582
|
+
* const yPred = tensor([0, 0, 0, 0, 0]); // Predicts all 0
|
|
583
|
+
* const bacc = balancedAccuracyScore(yTrue, yPred); // 0.5 (not 0.8!)
|
|
584
|
+
* ```
|
|
585
|
+
*
|
|
586
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.balanced_accuracy_score.html | Scikit-learn balanced_accuracy_score}
|
|
587
|
+
*/
|
|
588
|
+
declare function balancedAccuracyScore(yTrue: Tensor, yPred: Tensor): number;
|
|
589
|
+
|
|
590
|
+
type AverageMethod = "min" | "geometric" | "arithmetic" | "max";
|
|
591
|
+
type SilhouetteMetric = "euclidean" | "precomputed";
|
|
592
|
+
/**
|
|
593
|
+
* Computes the mean Silhouette Coefficient over all samples.
|
|
594
|
+
*
|
|
595
|
+
* The Silhouette Coefficient for a sample measures how similar it is to its own
|
|
596
|
+
* cluster compared to other clusters. Values range from -1 to 1, where higher
|
|
597
|
+
* values indicate better-defined clusters.
|
|
598
|
+
*
|
|
599
|
+
* **Formula**: s(i) = (b(i) - a(i)) / max(a(i), b(i))
|
|
600
|
+
* - a(i): mean intra-cluster distance for sample i
|
|
601
|
+
* - b(i): mean nearest-cluster distance for sample i
|
|
602
|
+
*
|
|
603
|
+
* **Time Complexity**: O(n²) where n is the number of samples
|
|
604
|
+
* **Space Complexity**: O(n + k) where k is the number of clusters
|
|
605
|
+
*
|
|
606
|
+
* @param X - Feature matrix of shape [n_samples, n_features], or a precomputed distance matrix
|
|
607
|
+
* @param labels - Cluster labels for each sample
|
|
608
|
+
* @param metric - Distance metric: 'euclidean' (default) or 'precomputed'
|
|
609
|
+
* @param options - Optional parameters
|
|
610
|
+
* @param options.sampleSize - Number of samples to use for approximation (required when n > 2000)
|
|
611
|
+
* @param options.randomState - Seed for reproducible sampling
|
|
612
|
+
* @returns Mean silhouette coefficient in range [-1, 1]
|
|
613
|
+
*
|
|
614
|
+
* @throws {InvalidParameterError} If fewer than 2 samples, invalid sampleSize, or unsupported metric
|
|
615
|
+
* @throws {ShapeError} If labels length doesn't match samples, or X shape is invalid
|
|
616
|
+
* @throws {DTypeError} If labels are string or X is non-numeric
|
|
617
|
+
* @throws {DataValidationError} If X contains non-finite values
|
|
618
|
+
*
|
|
619
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.silhouette_score.html | Scikit-learn silhouette_score}
|
|
620
|
+
*/
|
|
621
|
+
declare function silhouetteScore(X: Tensor, labels: Tensor, metric?: SilhouetteMetric, options?: {
|
|
622
|
+
sampleSize?: number;
|
|
623
|
+
randomState?: number;
|
|
624
|
+
}): number;
|
|
625
|
+
/**
|
|
626
|
+
* Computes the Silhouette Coefficient for each sample.
|
|
627
|
+
*
|
|
628
|
+
* Returns a tensor of per-sample silhouette values. Useful for identifying
|
|
629
|
+
* samples that are well-clustered vs. poorly-clustered.
|
|
630
|
+
*
|
|
631
|
+
* **Time Complexity**: O(n²) where n is the number of samples
|
|
632
|
+
* **Space Complexity**: O(n + k) where k is the number of clusters
|
|
633
|
+
*
|
|
634
|
+
* @param X - Feature matrix of shape [n_samples, n_features], or a precomputed distance matrix
|
|
635
|
+
* @param labels - Cluster labels for each sample
|
|
636
|
+
* @param metric - Distance metric: 'euclidean' (default) or 'precomputed'
|
|
637
|
+
* @returns Tensor of silhouette coefficients in range [-1, 1] for each sample
|
|
638
|
+
*
|
|
639
|
+
* @throws {InvalidParameterError} If fewer than 2 samples or unsupported metric
|
|
640
|
+
* @throws {ShapeError} If labels length doesn't match samples, or X shape is invalid
|
|
641
|
+
* @throws {DTypeError} If labels are string or X is non-numeric
|
|
642
|
+
* @throws {DataValidationError} If X contains non-finite values
|
|
643
|
+
*
|
|
644
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.silhouette_samples.html | Scikit-learn silhouette_samples}
|
|
645
|
+
*/
|
|
646
|
+
declare function silhouetteSamples(X: Tensor, labels: Tensor, metric?: SilhouetteMetric): Tensor;
|
|
647
|
+
/**
|
|
648
|
+
* Computes the Davies-Bouldin index.
|
|
649
|
+
*
|
|
650
|
+
* The Davies-Bouldin index measures the average similarity ratio of each cluster
|
|
651
|
+
* with its most similar cluster. Lower values indicate better clustering.
|
|
652
|
+
* Returns 0 when k < 2 or n_samples == 0.
|
|
653
|
+
*
|
|
654
|
+
* **Time Complexity**: O(n * d + k² * d) where n is samples, d is features, k is clusters
|
|
655
|
+
* **Space Complexity**: O(k * d)
|
|
656
|
+
*
|
|
657
|
+
* @param X - Feature matrix of shape [n_samples, n_features]
|
|
658
|
+
* @param labels - Cluster labels for each sample
|
|
659
|
+
* @returns Davies-Bouldin index (lower is better, 0 is minimum)
|
|
660
|
+
*
|
|
661
|
+
* @throws {ShapeError} If labels length doesn't match samples, or X shape is invalid
|
|
662
|
+
* @throws {DTypeError} If labels are string or X is non-numeric
|
|
663
|
+
* @throws {DataValidationError} If X contains non-finite values
|
|
664
|
+
*
|
|
665
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.davies_bouldin_score.html | Scikit-learn davies_bouldin_score}
|
|
666
|
+
*/
|
|
667
|
+
declare function daviesBouldinScore(X: Tensor, labels: Tensor): number;
|
|
668
|
+
/**
|
|
669
|
+
* Computes the Calinski-Harabasz index (Variance Ratio Criterion).
|
|
670
|
+
*
|
|
671
|
+
* The score is the ratio of between-cluster dispersion to within-cluster
|
|
672
|
+
* dispersion. Higher values indicate better-defined clusters.
|
|
673
|
+
* Returns 0 when k < 2, n_samples == 0, or within-group sum of squares is 0.
|
|
674
|
+
*
|
|
675
|
+
* **Time Complexity**: O(n * d) where n is samples and d is features
|
|
676
|
+
* **Space Complexity**: O(k * d) where k is clusters
|
|
677
|
+
*
|
|
678
|
+
* @param X - Feature matrix of shape [n_samples, n_features]
|
|
679
|
+
* @param labels - Cluster labels for each sample
|
|
680
|
+
* @returns Calinski-Harabasz index (higher is better)
|
|
681
|
+
*
|
|
682
|
+
* @throws {ShapeError} If labels length doesn't match samples, or X shape is invalid
|
|
683
|
+
* @throws {DTypeError} If labels are string or X is non-numeric
|
|
684
|
+
* @throws {DataValidationError} If X contains non-finite values
|
|
685
|
+
*
|
|
686
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.calinski_harabasz_score.html | Scikit-learn calinski_harabasz_score}
|
|
687
|
+
*/
|
|
688
|
+
declare function calinskiHarabaszScore(X: Tensor, labels: Tensor): number;
|
|
689
|
+
/**
|
|
690
|
+
* Computes the Adjusted Rand Index (ARI).
|
|
691
|
+
*
|
|
692
|
+
* The ARI measures the similarity between two clusterings, adjusted for chance.
|
|
693
|
+
* It ranges from -1 to 1, where 1 indicates perfect agreement, 0 indicates
|
|
694
|
+
* random labeling, and negative values indicate worse than random.
|
|
695
|
+
*
|
|
696
|
+
* **Time Complexity**: O(n + k₁ * k₂) where k₁, k₂ are cluster counts
|
|
697
|
+
* **Space Complexity**: O(k₁ * k₂)
|
|
698
|
+
*
|
|
699
|
+
* @param labelsTrue - Ground truth cluster labels
|
|
700
|
+
* @param labelsPred - Predicted cluster labels
|
|
701
|
+
* @returns Adjusted Rand Index in range [-1, 1]
|
|
702
|
+
*
|
|
703
|
+
* @throws {ShapeError} If labelsTrue and labelsPred have different sizes
|
|
704
|
+
* @throws {DTypeError} If labels are string
|
|
705
|
+
* @throws {DataValidationError} If labels contain non-finite or non-integer values
|
|
706
|
+
*
|
|
707
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.adjusted_rand_score.html | Scikit-learn adjusted_rand_score}
|
|
708
|
+
*/
|
|
709
|
+
declare function adjustedRandScore(labelsTrue: Tensor, labelsPred: Tensor): number;
|
|
710
|
+
/**
|
|
711
|
+
* Computes the Adjusted Mutual Information (AMI) between two clusterings.
|
|
712
|
+
*
|
|
713
|
+
* AMI adjusts the Mutual Information score to account for chance, providing
|
|
714
|
+
* a normalized measure of agreement between two clusterings.
|
|
715
|
+
*
|
|
716
|
+
* **Time Complexity**: O(n + k₁ * k₂ * min(k₁, k₂))
|
|
717
|
+
* **Space Complexity**: O(k₁ * k₂ + n)
|
|
718
|
+
*
|
|
719
|
+
* @param labelsTrue - Ground truth cluster labels
|
|
720
|
+
* @param labelsPred - Predicted cluster labels
|
|
721
|
+
* @param averageMethod - Method to compute the normalizer: 'min', 'geometric', 'arithmetic' (default), or 'max'
|
|
722
|
+
* @returns Adjusted Mutual Information score, typically in range [0, 1]
|
|
723
|
+
*
|
|
724
|
+
* @throws {ShapeError} If labelsTrue and labelsPred have different sizes
|
|
725
|
+
* @throws {DTypeError} If labels are string
|
|
726
|
+
* @throws {DataValidationError} If labels contain non-finite or non-integer values
|
|
727
|
+
*
|
|
728
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.adjusted_mutual_info_score.html | Scikit-learn adjusted_mutual_info_score}
|
|
729
|
+
*/
|
|
730
|
+
declare function adjustedMutualInfoScore(labelsTrue: Tensor, labelsPred: Tensor, averageMethod?: AverageMethod): number;
|
|
731
|
+
/**
|
|
732
|
+
* Computes the Normalized Mutual Information (NMI) between two clusterings.
|
|
733
|
+
*
|
|
734
|
+
* NMI normalizes the Mutual Information score to scale between 0 and 1,
|
|
735
|
+
* where 1 indicates perfect correlation between clusterings.
|
|
736
|
+
*
|
|
737
|
+
* **Time Complexity**: O(n + k₁ * k₂)
|
|
738
|
+
* **Space Complexity**: O(k₁ * k₂)
|
|
739
|
+
*
|
|
740
|
+
* @param labelsTrue - Ground truth cluster labels
|
|
741
|
+
* @param labelsPred - Predicted cluster labels
|
|
742
|
+
* @param averageMethod - Method to compute the normalizer: 'min', 'geometric', 'arithmetic' (default), or 'max'
|
|
743
|
+
* @returns Normalized Mutual Information score in range [0, 1]
|
|
744
|
+
*
|
|
745
|
+
* @throws {ShapeError} If labelsTrue and labelsPred have different sizes
|
|
746
|
+
* @throws {DTypeError} If labels are string
|
|
747
|
+
* @throws {DataValidationError} If labels contain non-finite or non-integer values
|
|
748
|
+
*
|
|
749
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.normalized_mutual_info_score.html | Scikit-learn normalized_mutual_info_score}
|
|
750
|
+
*/
|
|
751
|
+
declare function normalizedMutualInfoScore(labelsTrue: Tensor, labelsPred: Tensor, averageMethod?: AverageMethod): number;
|
|
752
|
+
/**
|
|
753
|
+
* Computes the Fowlkes-Mallows Index (FMI).
|
|
754
|
+
*
|
|
755
|
+
* The FMI is the geometric mean of pairwise precision and recall.
|
|
756
|
+
* It ranges from 0 to 1, where 1 indicates perfect agreement.
|
|
757
|
+
*
|
|
758
|
+
* **Time Complexity**: O(n + k₁ * k₂)
|
|
759
|
+
* **Space Complexity**: O(k₁ * k₂)
|
|
760
|
+
*
|
|
761
|
+
* @param labelsTrue - Ground truth cluster labels
|
|
762
|
+
* @param labelsPred - Predicted cluster labels
|
|
763
|
+
* @returns Fowlkes-Mallows score in range [0, 1]
|
|
764
|
+
*
|
|
765
|
+
* @throws {ShapeError} If labelsTrue and labelsPred have different sizes
|
|
766
|
+
* @throws {DTypeError} If labels are string
|
|
767
|
+
* @throws {DataValidationError} If labels contain non-finite or non-integer values
|
|
768
|
+
*
|
|
769
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.fowlkes_mallows_score.html | Scikit-learn fowlkes_mallows_score}
|
|
770
|
+
*/
|
|
771
|
+
declare function fowlkesMallowsScore(labelsTrue: Tensor, labelsPred: Tensor): number;
|
|
772
|
+
/**
|
|
773
|
+
* Computes the homogeneity score of a clustering.
|
|
774
|
+
*
|
|
775
|
+
* A clustering result satisfies homogeneity if all of its clusters contain
|
|
776
|
+
* only data points which are members of a single class. Score ranges from
|
|
777
|
+
* 0 to 1, where 1 indicates perfectly homogeneous clustering.
|
|
778
|
+
*
|
|
779
|
+
* **Time Complexity**: O(n + k₁ * k₂)
|
|
780
|
+
* **Space Complexity**: O(k₁ * k₂)
|
|
781
|
+
*
|
|
782
|
+
* @param labelsTrue - Ground truth class labels
|
|
783
|
+
* @param labelsPred - Predicted cluster labels
|
|
784
|
+
* @returns Homogeneity score in range [0, 1]
|
|
785
|
+
*
|
|
786
|
+
* @throws {ShapeError} If labelsTrue and labelsPred have different sizes
|
|
787
|
+
* @throws {DTypeError} If labels are string
|
|
788
|
+
* @throws {DataValidationError} If labels contain non-finite or non-integer values
|
|
789
|
+
*
|
|
790
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.homogeneity_score.html | Scikit-learn homogeneity_score}
|
|
791
|
+
*/
|
|
792
|
+
declare function homogeneityScore(labelsTrue: Tensor, labelsPred: Tensor): number;
|
|
793
|
+
/**
|
|
794
|
+
* Computes the completeness score of a clustering.
|
|
795
|
+
*
|
|
796
|
+
* A clustering result satisfies completeness if all data points that are
|
|
797
|
+
* members of a given class are assigned to the same cluster. Score ranges
|
|
798
|
+
* from 0 to 1, where 1 indicates perfectly complete clustering.
|
|
799
|
+
*
|
|
800
|
+
* **Time Complexity**: O(n + k₁ * k₂)
|
|
801
|
+
* **Space Complexity**: O(k₁ * k₂)
|
|
802
|
+
*
|
|
803
|
+
* @param labelsTrue - Ground truth class labels
|
|
804
|
+
* @param labelsPred - Predicted cluster labels
|
|
805
|
+
* @returns Completeness score in range [0, 1]
|
|
806
|
+
*
|
|
807
|
+
* @throws {ShapeError} If labelsTrue and labelsPred have different sizes
|
|
808
|
+
* @throws {DTypeError} If labels are string
|
|
809
|
+
* @throws {DataValidationError} If labels contain non-finite or non-integer values
|
|
810
|
+
*
|
|
811
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.completeness_score.html | Scikit-learn completeness_score}
|
|
812
|
+
*/
|
|
813
|
+
declare function completenessScore(labelsTrue: Tensor, labelsPred: Tensor): number;
|
|
814
|
+
/**
|
|
815
|
+
* Computes the V-measure score of a clustering.
|
|
816
|
+
*
|
|
817
|
+
* V-measure is the harmonic mean of homogeneity and completeness, controlled
|
|
818
|
+
* by the beta parameter. When beta > 1, completeness is weighted more; when
|
|
819
|
+
* beta < 1, homogeneity is weighted more.
|
|
820
|
+
*
|
|
821
|
+
* **Formula**: v = (1 + beta) * h * c / (beta * h + c)
|
|
822
|
+
*
|
|
823
|
+
* **Time Complexity**: O(n + k₁ * k₂)
|
|
824
|
+
* **Space Complexity**: O(k₁ * k₂)
|
|
825
|
+
*
|
|
826
|
+
* @param labelsTrue - Ground truth class labels
|
|
827
|
+
* @param labelsPred - Predicted cluster labels
|
|
828
|
+
* @param beta - Weight of homogeneity vs completeness (default: 1.0)
|
|
829
|
+
* @returns V-measure score in range [0, 1]
|
|
830
|
+
*
|
|
831
|
+
* @throws {ShapeError} If labelsTrue and labelsPred have different sizes
|
|
832
|
+
* @throws {DTypeError} If labels are string
|
|
833
|
+
* @throws {InvalidParameterError} If beta is not a positive finite number
|
|
834
|
+
* @throws {DataValidationError} If labels contain non-finite or non-integer values
|
|
835
|
+
*
|
|
836
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.v_measure_score.html | Scikit-learn v_measure_score}
|
|
837
|
+
*/
|
|
838
|
+
declare function vMeasureScore(labelsTrue: Tensor, labelsPred: Tensor, beta?: number): number;
|
|
839
|
+
|
|
840
|
+
/**
|
|
841
|
+
* Calculate Mean Squared Error (MSE).
|
|
842
|
+
*
|
|
843
|
+
* Measures the average squared difference between predictions and actual values.
|
|
844
|
+
* MSE is sensitive to outliers due to squaring the errors.
|
|
845
|
+
*
|
|
846
|
+
* **Formula**: MSE = (1/n) * Σ(y_true - y_pred)²
|
|
847
|
+
*
|
|
848
|
+
* **Time Complexity**: O(n) where n is the number of samples
|
|
849
|
+
* **Space Complexity**: O(1)
|
|
850
|
+
*
|
|
851
|
+
* @param yTrue - Ground truth (correct) target values
|
|
852
|
+
* @param yPred - Estimated target values
|
|
853
|
+
* @returns MSE value (always non-negative, 0 is perfect)
|
|
854
|
+
*
|
|
855
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
856
|
+
* @throws {DTypeError} If yTrue or yPred is non-numeric or int64
|
|
857
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
858
|
+
*
|
|
859
|
+
* @example
|
|
860
|
+
* ```ts
|
|
861
|
+
* import { mse, tensor } from 'deepbox/metrics';
|
|
862
|
+
*
|
|
863
|
+
* const yTrue = tensor([3, -0.5, 2, 7]);
|
|
864
|
+
* const yPred = tensor([2.5, 0.0, 2, 8]);
|
|
865
|
+
* const error = mse(yTrue, yPred); // 0.375
|
|
866
|
+
* ```
|
|
867
|
+
*
|
|
868
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html | Scikit-learn mean_squared_error}
|
|
869
|
+
*/
|
|
870
|
+
declare function mse(yTrue: Tensor, yPred: Tensor): number;
|
|
871
|
+
/**
|
|
872
|
+
* Calculate Root Mean Squared Error (RMSE).
|
|
873
|
+
*
|
|
874
|
+
* Square root of MSE, expressed in the same units as the target variable.
|
|
875
|
+
* RMSE is more interpretable than MSE as it's in the original scale.
|
|
876
|
+
*
|
|
877
|
+
* **Formula**: RMSE = √(MSE) = √((1/n) * Σ(y_true - y_pred)²)
|
|
878
|
+
*
|
|
879
|
+
* **Time Complexity**: O(n) where n is the number of samples
|
|
880
|
+
* **Space Complexity**: O(1)
|
|
881
|
+
*
|
|
882
|
+
* @param yTrue - Ground truth (correct) target values
|
|
883
|
+
* @param yPred - Estimated target values
|
|
884
|
+
* @returns RMSE value (always non-negative, 0 is perfect)
|
|
885
|
+
*
|
|
886
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
887
|
+
* @throws {DTypeError} If yTrue or yPred is non-numeric or int64
|
|
888
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
889
|
+
*
|
|
890
|
+
* @example
|
|
891
|
+
* ```ts
|
|
892
|
+
* import { rmse, tensor } from 'deepbox/metrics';
|
|
893
|
+
*
|
|
894
|
+
* const yTrue = tensor([3, -0.5, 2, 7]);
|
|
895
|
+
* const yPred = tensor([2.5, 0.0, 2, 8]);
|
|
896
|
+
* const error = rmse(yTrue, yPred); // √0.375 ≈ 0.612
|
|
897
|
+
* ```
|
|
898
|
+
*
|
|
899
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html | Scikit-learn mean_squared_error}
|
|
900
|
+
*/
|
|
901
|
+
declare function rmse(yTrue: Tensor, yPred: Tensor): number;
|
|
902
|
+
/**
|
|
903
|
+
* Calculate Mean Absolute Error (MAE).
|
|
904
|
+
*
|
|
905
|
+
* Measures the average absolute difference between predictions and actual values.
|
|
906
|
+
* MAE is more robust to outliers than MSE.
|
|
907
|
+
*
|
|
908
|
+
* **Formula**: MAE = (1/n) * Σ|y_true - y_pred|
|
|
909
|
+
*
|
|
910
|
+
* **Time Complexity**: O(n) where n is the number of samples
|
|
911
|
+
* **Space Complexity**: O(1)
|
|
912
|
+
*
|
|
913
|
+
* @param yTrue - Ground truth (correct) target values
|
|
914
|
+
* @param yPred - Estimated target values
|
|
915
|
+
* @returns MAE value (always non-negative, 0 is perfect)
|
|
916
|
+
*
|
|
917
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
918
|
+
* @throws {DTypeError} If yTrue or yPred is non-numeric or int64
|
|
919
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
920
|
+
*
|
|
921
|
+
* @example
|
|
922
|
+
* ```ts
|
|
923
|
+
* import { mae, tensor } from 'deepbox/metrics';
|
|
924
|
+
*
|
|
925
|
+
* const yTrue = tensor([3, -0.5, 2, 7]);
|
|
926
|
+
* const yPred = tensor([2.5, 0.0, 2, 8]);
|
|
927
|
+
* const error = mae(yTrue, yPred); // 0.5
|
|
928
|
+
* ```
|
|
929
|
+
*
|
|
930
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_absolute_error.html | Scikit-learn mean_absolute_error}
|
|
931
|
+
*/
|
|
932
|
+
declare function mae(yTrue: Tensor, yPred: Tensor): number;
|
|
933
|
+
/**
|
|
934
|
+
* Calculate R² (coefficient of determination) score.
|
|
935
|
+
*
|
|
936
|
+
* Represents the proportion of variance in the target variable that is
|
|
937
|
+
* explained by the model. R² of 1 indicates perfect predictions, 0 indicates
|
|
938
|
+
* the model is no better than predicting the mean, and negative values indicate
|
|
939
|
+
* the model is worse than predicting the mean.
|
|
940
|
+
*
|
|
941
|
+
* **Formula**: R² = 1 - (SS_res / SS_tot)
|
|
942
|
+
* - SS_res = Σ(y_true - y_pred)² (residual sum of squares)
|
|
943
|
+
* - SS_tot = Σ(y_true - mean(y_true))² (total sum of squares)
|
|
944
|
+
*
|
|
945
|
+
* **Time Complexity**: O(n) where n is the number of samples
|
|
946
|
+
* **Space Complexity**: O(1)
|
|
947
|
+
*
|
|
948
|
+
* @param yTrue - Ground truth (correct) target values
|
|
949
|
+
* @param yPred - Estimated target values
|
|
950
|
+
* @returns R² score (1 is perfect, 0 is baseline, negative is worse than baseline)
|
|
951
|
+
*
|
|
952
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
953
|
+
* @throws {DTypeError} If yTrue or yPred is non-numeric or int64
|
|
954
|
+
* @throws {InvalidParameterError} If inputs are empty
|
|
955
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
956
|
+
*
|
|
957
|
+
* @example
|
|
958
|
+
* ```ts
|
|
959
|
+
* import { r2Score, tensor } from 'deepbox/metrics';
|
|
960
|
+
*
|
|
961
|
+
* const yTrue = tensor([3, -0.5, 2, 7]);
|
|
962
|
+
* const yPred = tensor([2.5, 0.0, 2, 8]);
|
|
963
|
+
* const score = r2Score(yTrue, yPred); // Close to 1 for good fit
|
|
964
|
+
* ```
|
|
965
|
+
*
|
|
966
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html | Scikit-learn r2_score}
|
|
967
|
+
*/
|
|
968
|
+
declare function r2Score(yTrue: Tensor, yPred: Tensor): number;
|
|
969
|
+
/**
|
|
970
|
+
* Calculate Adjusted R² score.
|
|
971
|
+
*
|
|
972
|
+
* R² adjusted for the number of features in the model. Penalizes the addition
|
|
973
|
+
* of features that don't improve the model. More appropriate than R² when
|
|
974
|
+
* comparing models with different numbers of features.
|
|
975
|
+
*
|
|
976
|
+
* **Formula**: Adjusted R² = 1 - ((1 - R²) * (n - 1)) / (n - p - 1)
|
|
977
|
+
* - n = number of samples
|
|
978
|
+
* - p = number of features
|
|
979
|
+
*
|
|
980
|
+
* **Time Complexity**: O(n) where n is the number of samples
|
|
981
|
+
* **Space Complexity**: O(1)
|
|
982
|
+
*
|
|
983
|
+
* **Constraints**: Requires n > p + 1 (more samples than features + 1)
|
|
984
|
+
*
|
|
985
|
+
* @param yTrue - Ground truth (correct) target values
|
|
986
|
+
* @param yPred - Estimated target values
|
|
987
|
+
* @param nFeatures - Number of features (predictors) used in the model
|
|
988
|
+
* @returns Adjusted R² score
|
|
989
|
+
*
|
|
990
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
991
|
+
* @throws {DTypeError} If yTrue or yPred is non-numeric or int64
|
|
992
|
+
* @throws {InvalidParameterError} If nFeatures is not a non-negative integer, n <= p + 1, or inputs are empty
|
|
993
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
994
|
+
*
|
|
995
|
+
* @example
|
|
996
|
+
* ```ts
|
|
997
|
+
* import { adjustedR2Score, tensor } from 'deepbox/metrics';
|
|
998
|
+
*
|
|
999
|
+
* const yTrue = tensor([3, -0.5, 2, 7]);
|
|
1000
|
+
* const yPred = tensor([2.5, 0.0, 2, 8]);
|
|
1001
|
+
* const score = adjustedR2Score(yTrue, yPred, 2); // Adjusted for 2 features
|
|
1002
|
+
* ```
|
|
1003
|
+
*
|
|
1004
|
+
* @see {@link https://en.wikipedia.org/wiki/Coefficient_of_determination#Adjusted_R2 | Wikipedia: Adjusted R²}
|
|
1005
|
+
*/
|
|
1006
|
+
declare function adjustedR2Score(yTrue: Tensor, yPred: Tensor, nFeatures: number): number;
|
|
1007
|
+
/**
|
|
1008
|
+
* Calculate Mean Absolute Percentage Error (MAPE).
|
|
1009
|
+
*
|
|
1010
|
+
* Measures the average absolute percentage difference between predictions
|
|
1011
|
+
* and actual values. Expressed as a percentage, making it scale-independent
|
|
1012
|
+
* and easy to interpret.
|
|
1013
|
+
*
|
|
1014
|
+
* **Formula**: MAPE = (100/m) * Σ|((y_true - y_pred) / y_true)|
|
|
1015
|
+
* where m is the number of non-zero targets.
|
|
1016
|
+
*
|
|
1017
|
+
* **Time Complexity**: O(n) where n is the number of samples
|
|
1018
|
+
* **Space Complexity**: O(1)
|
|
1019
|
+
*
|
|
1020
|
+
* **Important**: Zero values in yTrue are skipped. If all targets are zero,
|
|
1021
|
+
* this function returns 0.
|
|
1022
|
+
*
|
|
1023
|
+
* @param yTrue - Ground truth (correct) target values
|
|
1024
|
+
* @param yPred - Estimated target values
|
|
1025
|
+
* @returns MAPE value as percentage (0 is perfect, lower is better)
|
|
1026
|
+
*
|
|
1027
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
1028
|
+
* @throws {DTypeError} If yTrue or yPred is non-numeric or int64
|
|
1029
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
1030
|
+
*
|
|
1031
|
+
* @example
|
|
1032
|
+
* ```ts
|
|
1033
|
+
* import { mape, tensor } from 'deepbox/metrics';
|
|
1034
|
+
*
|
|
1035
|
+
* const yTrue = tensor([3, -0.5, 2, 7]);
|
|
1036
|
+
* const yPred = tensor([2.5, 0.0, 2, 8]);
|
|
1037
|
+
* const error = mape(yTrue, yPred); // Percentage error
|
|
1038
|
+
* ```
|
|
1039
|
+
*
|
|
1040
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_absolute_percentage_error.html | Scikit-learn MAPE}
|
|
1041
|
+
*/
|
|
1042
|
+
declare function mape(yTrue: Tensor, yPred: Tensor): number;
|
|
1043
|
+
/**
|
|
1044
|
+
* Calculate Median Absolute Error (MedAE).
|
|
1045
|
+
*
|
|
1046
|
+
* Measures the median of absolute differences between predictions and actual values.
|
|
1047
|
+
* More robust to outliers than MAE or MSE as it uses the median instead of mean.
|
|
1048
|
+
*
|
|
1049
|
+
* **Formula**: MedAE = median(|y_true - y_pred|)
|
|
1050
|
+
*
|
|
1051
|
+
* **Time Complexity**: O(n log n) due to sorting for median calculation
|
|
1052
|
+
* **Space Complexity**: O(n) for storing error array
|
|
1053
|
+
*
|
|
1054
|
+
* @param yTrue - Ground truth (correct) target values
|
|
1055
|
+
* @param yPred - Estimated target values
|
|
1056
|
+
* @returns Median absolute error (always non-negative, 0 is perfect)
|
|
1057
|
+
*
|
|
1058
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
1059
|
+
* @throws {DTypeError} If yTrue or yPred is non-numeric or int64
|
|
1060
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
1061
|
+
*
|
|
1062
|
+
* @example
|
|
1063
|
+
* ```ts
|
|
1064
|
+
* import { medianAbsoluteError, tensor } from 'deepbox/metrics';
|
|
1065
|
+
*
|
|
1066
|
+
* const yTrue = tensor([3, -0.5, 2, 7]);
|
|
1067
|
+
* const yPred = tensor([2.5, 0.0, 2, 8]);
|
|
1068
|
+
* const error = medianAbsoluteError(yTrue, yPred);
|
|
1069
|
+
* ```
|
|
1070
|
+
*
|
|
1071
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.median_absolute_error.html | Scikit-learn median_absolute_error}
|
|
1072
|
+
*/
|
|
1073
|
+
declare function medianAbsoluteError(yTrue: Tensor, yPred: Tensor): number;
|
|
1074
|
+
/**
|
|
1075
|
+
* Calculate maximum residual error.
|
|
1076
|
+
*
|
|
1077
|
+
* Returns the maximum absolute difference between predictions and actual values.
|
|
1078
|
+
* Useful for identifying the worst-case prediction error.
|
|
1079
|
+
*
|
|
1080
|
+
* **Formula**: max_error = max(|y_true - y_pred|)
|
|
1081
|
+
*
|
|
1082
|
+
* **Time Complexity**: O(n) where n is the number of samples
|
|
1083
|
+
* **Space Complexity**: O(1)
|
|
1084
|
+
*
|
|
1085
|
+
* @param yTrue - Ground truth (correct) target values
|
|
1086
|
+
* @param yPred - Estimated target values
|
|
1087
|
+
* @returns Maximum absolute error (always non-negative, 0 is perfect)
|
|
1088
|
+
*
|
|
1089
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
1090
|
+
* @throws {DTypeError} If yTrue or yPred is non-numeric or int64
|
|
1091
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
1092
|
+
*
|
|
1093
|
+
* @example
|
|
1094
|
+
* ```ts
|
|
1095
|
+
* import { maxError, tensor } from 'deepbox/metrics';
|
|
1096
|
+
*
|
|
1097
|
+
* const yTrue = tensor([3, -0.5, 2, 7]);
|
|
1098
|
+
* const yPred = tensor([2.5, 0.0, 2, 8]);
|
|
1099
|
+
* const error = maxError(yTrue, yPred); // 1.0 (worst prediction)
|
|
1100
|
+
* ```
|
|
1101
|
+
*
|
|
1102
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.max_error.html | Scikit-learn max_error}
|
|
1103
|
+
*/
|
|
1104
|
+
declare function maxError(yTrue: Tensor, yPred: Tensor): number;
|
|
1105
|
+
/**
|
|
1106
|
+
* Calculate explained variance score.
|
|
1107
|
+
*
|
|
1108
|
+
* Measures the proportion of variance in the target variable that is explained
|
|
1109
|
+
* by the model. Similar to R² but uses variance instead of sum of squares.
|
|
1110
|
+
* Best possible score is 1.0, lower values are worse.
|
|
1111
|
+
*
|
|
1112
|
+
* **Formula**: explained_variance = 1 - Var(y_true - y_pred) / Var(y_true)
|
|
1113
|
+
*
|
|
1114
|
+
* **Time Complexity**: O(n) where n is the number of samples
|
|
1115
|
+
* **Space Complexity**: O(1)
|
|
1116
|
+
*
|
|
1117
|
+
* @param yTrue - Ground truth (correct) target values
|
|
1118
|
+
* @param yPred - Estimated target values
|
|
1119
|
+
* @returns Explained variance score (1.0 is perfect, lower is worse)
|
|
1120
|
+
*
|
|
1121
|
+
* @throws {ShapeError} If yTrue and yPred have different sizes or are not 1D/column vectors
|
|
1122
|
+
* @throws {DTypeError} If yTrue or yPred is non-numeric or int64
|
|
1123
|
+
* @throws {InvalidParameterError} If inputs are empty
|
|
1124
|
+
* @throws {DataValidationError} If inputs contain NaN or infinite values
|
|
1125
|
+
*
|
|
1126
|
+
* @example
|
|
1127
|
+
* ```ts
|
|
1128
|
+
* import { explainedVarianceScore, tensor } from 'deepbox/metrics';
|
|
1129
|
+
*
|
|
1130
|
+
* const yTrue = tensor([3, -0.5, 2, 7]);
|
|
1131
|
+
* const yPred = tensor([2.5, 0.0, 2, 8]);
|
|
1132
|
+
* const score = explainedVarianceScore(yTrue, yPred);
|
|
1133
|
+
* ```
|
|
1134
|
+
*
|
|
1135
|
+
* @see {@link https://scikit-learn.org/stable/modules/generated/sklearn.metrics.explained_variance_score.html | Scikit-learn explained_variance_score}
|
|
1136
|
+
*/
|
|
1137
|
+
declare function explainedVarianceScore(yTrue: Tensor, yPred: Tensor): number;
|
|
1138
|
+
|
|
1139
|
+
declare const index_accuracy: typeof accuracy;
|
|
1140
|
+
declare const index_adjustedMutualInfoScore: typeof adjustedMutualInfoScore;
|
|
1141
|
+
declare const index_adjustedR2Score: typeof adjustedR2Score;
|
|
1142
|
+
declare const index_adjustedRandScore: typeof adjustedRandScore;
|
|
1143
|
+
declare const index_averagePrecisionScore: typeof averagePrecisionScore;
|
|
1144
|
+
declare const index_balancedAccuracyScore: typeof balancedAccuracyScore;
|
|
1145
|
+
declare const index_calinskiHarabaszScore: typeof calinskiHarabaszScore;
|
|
1146
|
+
declare const index_classificationReport: typeof classificationReport;
|
|
1147
|
+
declare const index_cohenKappaScore: typeof cohenKappaScore;
|
|
1148
|
+
declare const index_completenessScore: typeof completenessScore;
|
|
1149
|
+
declare const index_confusionMatrix: typeof confusionMatrix;
|
|
1150
|
+
declare const index_daviesBouldinScore: typeof daviesBouldinScore;
|
|
1151
|
+
declare const index_explainedVarianceScore: typeof explainedVarianceScore;
|
|
1152
|
+
declare const index_f1Score: typeof f1Score;
|
|
1153
|
+
declare const index_fbetaScore: typeof fbetaScore;
|
|
1154
|
+
declare const index_fowlkesMallowsScore: typeof fowlkesMallowsScore;
|
|
1155
|
+
declare const index_hammingLoss: typeof hammingLoss;
|
|
1156
|
+
declare const index_homogeneityScore: typeof homogeneityScore;
|
|
1157
|
+
declare const index_jaccardScore: typeof jaccardScore;
|
|
1158
|
+
declare const index_logLoss: typeof logLoss;
|
|
1159
|
+
declare const index_mae: typeof mae;
|
|
1160
|
+
declare const index_mape: typeof mape;
|
|
1161
|
+
declare const index_matthewsCorrcoef: typeof matthewsCorrcoef;
|
|
1162
|
+
declare const index_maxError: typeof maxError;
|
|
1163
|
+
declare const index_medianAbsoluteError: typeof medianAbsoluteError;
|
|
1164
|
+
declare const index_mse: typeof mse;
|
|
1165
|
+
declare const index_normalizedMutualInfoScore: typeof normalizedMutualInfoScore;
|
|
1166
|
+
declare const index_precision: typeof precision;
|
|
1167
|
+
declare const index_precisionRecallCurve: typeof precisionRecallCurve;
|
|
1168
|
+
declare const index_r2Score: typeof r2Score;
|
|
1169
|
+
declare const index_recall: typeof recall;
|
|
1170
|
+
declare const index_rmse: typeof rmse;
|
|
1171
|
+
declare const index_rocAucScore: typeof rocAucScore;
|
|
1172
|
+
declare const index_rocCurve: typeof rocCurve;
|
|
1173
|
+
declare const index_silhouetteSamples: typeof silhouetteSamples;
|
|
1174
|
+
declare const index_silhouetteScore: typeof silhouetteScore;
|
|
1175
|
+
declare const index_vMeasureScore: typeof vMeasureScore;
|
|
1176
|
+
declare namespace index {
|
|
1177
|
+
export { index_accuracy as accuracy, index_adjustedMutualInfoScore as adjustedMutualInfoScore, index_adjustedR2Score as adjustedR2Score, index_adjustedRandScore as adjustedRandScore, index_averagePrecisionScore as averagePrecisionScore, index_balancedAccuracyScore as balancedAccuracyScore, index_calinskiHarabaszScore as calinskiHarabaszScore, index_classificationReport as classificationReport, index_cohenKappaScore as cohenKappaScore, index_completenessScore as completenessScore, index_confusionMatrix as confusionMatrix, index_daviesBouldinScore as daviesBouldinScore, index_explainedVarianceScore as explainedVarianceScore, index_f1Score as f1Score, index_fbetaScore as fbetaScore, index_fowlkesMallowsScore as fowlkesMallowsScore, index_hammingLoss as hammingLoss, index_homogeneityScore as homogeneityScore, index_jaccardScore as jaccardScore, index_logLoss as logLoss, index_mae as mae, index_mape as mape, index_matthewsCorrcoef as matthewsCorrcoef, index_maxError as maxError, index_medianAbsoluteError as medianAbsoluteError, index_mse as mse, index_normalizedMutualInfoScore as normalizedMutualInfoScore, index_precision as precision, index_precisionRecallCurve as precisionRecallCurve, index_r2Score as r2Score, index_recall as recall, index_rmse as rmse, index_rocAucScore as rocAucScore, index_rocCurve as rocCurve, index_silhouetteSamples as silhouetteSamples, index_silhouetteScore as silhouetteScore, index_vMeasureScore as vMeasureScore };
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
export { silhouetteSamples as A, silhouetteScore as B, vMeasureScore as C, adjustedR2Score as D, explainedVarianceScore as E, mae as F, mape as G, maxError as H, medianAbsoluteError as I, mse as J, r2Score as K, rmse as L, accuracy as a, averagePrecisionScore as b, balancedAccuracyScore as c, classificationReport as d, cohenKappaScore as e, confusionMatrix as f, f1Score as g, fbetaScore as h, index as i, hammingLoss as j, jaccardScore as k, logLoss as l, matthewsCorrcoef as m, precisionRecallCurve as n, rocAucScore as o, precision as p, rocCurve as q, recall as r, adjustedMutualInfoScore as s, adjustedRandScore as t, calinskiHarabaszScore as u, completenessScore as v, daviesBouldinScore as w, fowlkesMallowsScore as x, homogeneityScore as y, normalizedMutualInfoScore as z };
|