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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jehaad Aljohani
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
# Deepbox
|
|
2
|
+
|
|
3
|
+
## The TypeScript Toolkit for AI & Numerical Computing
|
|
4
|
+
|
|
5
|
+
[](https://github.com/jehaad1/Deepbox/actions/workflows/ci.yml)
|
|
6
|
+
[](https://www.npmjs.com/package/deepbox)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
|
|
9
|
+
Deepbox is a comprehensive, type-safe TypeScript library that unifies numerical computing, tabular data workflows, and machine learning into a single modular package. Zero runtime dependencies. 4 009 tests. Production-ready.
|
|
10
|
+
|
|
11
|
+
_Inspired by NumPy, Pandas, PyTorch, scikit-learn, and Matplotlib._
|
|
12
|
+
|
|
13
|
+
## Requirements
|
|
14
|
+
|
|
15
|
+
- Node.js `>= 24.13.0`
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install deepbox
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { tensor, add, parameter } from "deepbox/ndarray";
|
|
27
|
+
import { DataFrame } from "deepbox/dataframe";
|
|
28
|
+
import { LinearRegression } from "deepbox/ml";
|
|
29
|
+
|
|
30
|
+
// Tensor operations with broadcasting
|
|
31
|
+
const a = tensor([
|
|
32
|
+
[1, 2],
|
|
33
|
+
[3, 4],
|
|
34
|
+
]);
|
|
35
|
+
const b = tensor([
|
|
36
|
+
[5, 6],
|
|
37
|
+
[7, 8],
|
|
38
|
+
]);
|
|
39
|
+
const c = add(a, b); // tensor([[6, 8], [10, 12]])
|
|
40
|
+
|
|
41
|
+
// Automatic differentiation
|
|
42
|
+
const x = parameter([2, 3]);
|
|
43
|
+
const y = x.mul(x).sum();
|
|
44
|
+
y.backward();
|
|
45
|
+
// x.grad -> tensor([4, 6])
|
|
46
|
+
|
|
47
|
+
// DataFrame operations
|
|
48
|
+
const df = new DataFrame({
|
|
49
|
+
name: ["Alice", "Bob", "Charlie"],
|
|
50
|
+
age: [25, 30, 35],
|
|
51
|
+
score: [85, 90, 78],
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Machine learning
|
|
55
|
+
const model = new LinearRegression();
|
|
56
|
+
model.fit(XTrain, yTrain);
|
|
57
|
+
const predictions = model.predict(XTest);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Prefer per-module imports for tree-shaking, or use namespaces from the root:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import * as ds from "deepbox";
|
|
64
|
+
const t = ds.ndarray.tensor([1, 2, 3]);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Modules
|
|
68
|
+
|
|
69
|
+
| Module | What it provides | Inspired by |
|
|
70
|
+
| -------------------- | -------------------------------------------------------------------------------------- | -------------------- |
|
|
71
|
+
| `deepbox/core` | Types, errors, validation, dtype helpers, configuration | - |
|
|
72
|
+
| `deepbox/ndarray` | N-D tensors with autograd, broadcasting, 90+ ops, sparse matrices | NumPy, PyTorch |
|
|
73
|
+
| `deepbox/linalg` | SVD, QR, LU, Cholesky, eigenvalue decomposition, solvers, norms | NumPy/SciPy |
|
|
74
|
+
| `deepbox/dataframe` | DataFrame + Series with 50+ operations, CSV I/O | Pandas |
|
|
75
|
+
| `deepbox/stats` | Descriptive stats, correlations, hypothesis tests (t-test, ANOVA, chi-square, etc.) | SciPy/statsmodels |
|
|
76
|
+
| `deepbox/metrics` | 40+ ML metrics (classification, regression, clustering) | scikit-learn |
|
|
77
|
+
| `deepbox/preprocess` | Scalers, encoders, normalizers, cross-validation splits | scikit-learn |
|
|
78
|
+
| `deepbox/ml` | Classical ML (Linear, Ridge, Lasso, Logistic, Trees, SVM, KNN, Naive Bayes, Ensembles) | scikit-learn |
|
|
79
|
+
| `deepbox/nn` | Neural networks (Linear, Conv, RNN/LSTM/GRU, Attention, Normalization, Losses) | PyTorch |
|
|
80
|
+
| `deepbox/optim` | Optimizers (SGD, Adam, AdamW, RMSprop, etc.) + LR schedulers | PyTorch |
|
|
81
|
+
| `deepbox/random` | Distributions (uniform, normal, binomial, gamma, beta, etc.) + sampling | NumPy.random |
|
|
82
|
+
| `deepbox/datasets` | Built-in datasets (Iris, Digits, Breast Cancer, etc.) + synthetic generators | scikit-learn/PyTorch |
|
|
83
|
+
| `deepbox/plot` | SVG/PNG plotting (scatter, line, bar, hist, heatmap, contour, ML plots) | Matplotlib |
|
|
84
|
+
|
|
85
|
+
## Features
|
|
86
|
+
|
|
87
|
+
### N-Dimensional Arrays
|
|
88
|
+
|
|
89
|
+
- **90+ operations**: arithmetic, trigonometric, logical, reductions, sorting, manipulation
|
|
90
|
+
- **Automatic differentiation**: `GradTensor` with reverse-mode backpropagation
|
|
91
|
+
- **Broadcasting**: NumPy-compatible semantics
|
|
92
|
+
- **Sparse matrices**: CSR format with arithmetic and matrix operations
|
|
93
|
+
- **Multiple dtypes**: float32, float64, int32, int64, uint8, bool, string
|
|
94
|
+
- **Activation functions**: ReLU, Sigmoid, Softmax, GELU, Mish, Swish, ELU, LeakyReLU
|
|
95
|
+
|
|
96
|
+
### DataFrames & Series
|
|
97
|
+
|
|
98
|
+
- **Pandas-like API**: filtering, grouping, joining, merging, pivoting, sorting
|
|
99
|
+
- **CSV I/O**: read and write CSV files
|
|
100
|
+
- **Descriptive statistics**: `describe()`, value counts, correlation matrices
|
|
101
|
+
|
|
102
|
+
### Linear Algebra
|
|
103
|
+
|
|
104
|
+
- **Decompositions**: SVD, QR, LU, Cholesky, Eigenvalue (eig, eigh, eigvals, eigvalsh)
|
|
105
|
+
- **Solvers**: `solve()`, `lstsq()`, `solveTriangular()`
|
|
106
|
+
- **Properties**: `det()`, `trace()`, `matrixRank()`, `cond()`, `slogdet()`
|
|
107
|
+
- **Norms**: `norm()` (L1, L2, Frobenius, nuclear, inf)
|
|
108
|
+
- **Inverse**: `inv()`, `pinv()`
|
|
109
|
+
|
|
110
|
+
### Statistics
|
|
111
|
+
|
|
112
|
+
- **Descriptive**: mean, median, mode, variance, std, skewness, kurtosis, quantile, percentile
|
|
113
|
+
- **Correlations**: Pearson, Spearman, Kendall tau
|
|
114
|
+
- **Hypothesis tests**: t-tests (1-sample, independent, paired), ANOVA, chi-square, Shapiro-Wilk, Mann-Whitney U, Kruskal-Wallis, Friedman, Anderson-Darling, KS test
|
|
115
|
+
- **Variance tests**: Levene, Bartlett
|
|
116
|
+
|
|
117
|
+
### Machine Learning
|
|
118
|
+
|
|
119
|
+
- **Linear models**: LinearRegression, Ridge, Lasso, LogisticRegression
|
|
120
|
+
- **Tree-based**: DecisionTreeClassifier/Regressor, RandomForestClassifier/Regressor
|
|
121
|
+
- **Ensemble**: GradientBoostingClassifier/Regressor
|
|
122
|
+
- **SVM**: LinearSVC, LinearSVR
|
|
123
|
+
- **Neighbors**: KNeighborsClassifier, KNeighborsRegressor
|
|
124
|
+
- **Naive Bayes**: GaussianNB
|
|
125
|
+
- **Clustering**: KMeans, DBSCAN
|
|
126
|
+
- **Dimensionality reduction**: PCA, t-SNE
|
|
127
|
+
|
|
128
|
+
### Neural Networks
|
|
129
|
+
|
|
130
|
+
- **Layers**: Linear, Conv1d, Conv2d, MaxPool2d, AvgPool2d
|
|
131
|
+
- **Recurrent**: RNN, LSTM, GRU
|
|
132
|
+
- **Attention**: MultiheadAttention, TransformerEncoderLayer
|
|
133
|
+
- **Normalization**: BatchNorm1d, LayerNorm
|
|
134
|
+
- **Regularization**: Dropout
|
|
135
|
+
- **Activations**: ReLU, Sigmoid, Tanh, GELU, Mish, Swish, Softmax, LogSoftmax, ELU, LeakyReLU, Softplus
|
|
136
|
+
- **Losses**: mseLoss, maeLoss, crossEntropyLoss, binaryCrossEntropyLoss, binaryCrossEntropyWithLogitsLoss, huberLoss, rmseLoss
|
|
137
|
+
- **Containers**: Sequential
|
|
138
|
+
|
|
139
|
+
### Optimization
|
|
140
|
+
|
|
141
|
+
- **Optimizers**: SGD (with momentum), Adam, AdamW, Nadam, RMSprop, Adagrad, AdaDelta
|
|
142
|
+
- **LR Schedulers**: StepLR, MultiStepLR, ExponentialLR, CosineAnnealingLR, LinearLR, OneCycleLR, ReduceLROnPlateau, WarmupLR
|
|
143
|
+
|
|
144
|
+
### Preprocessing
|
|
145
|
+
|
|
146
|
+
- **Scalers**: StandardScaler, MinMaxScaler, RobustScaler, MaxAbsScaler, Normalizer, PowerTransformer, QuantileTransformer
|
|
147
|
+
- **Encoders**: LabelEncoder, OneHotEncoder, OrdinalEncoder, LabelBinarizer, MultiLabelBinarizer
|
|
148
|
+
- **Splitting**: trainTestSplit, KFold, StratifiedKFold, GroupKFold, LeaveOneOut, LeavePOut
|
|
149
|
+
|
|
150
|
+
### Visualization
|
|
151
|
+
|
|
152
|
+
- **Plot types**: scatter, line, bar, histogram, heatmap, contour, box plot, violin plot, pie chart
|
|
153
|
+
- **ML plots**: confusion matrix, ROC curve, precision-recall curve, learning curves, validation curves, decision boundaries
|
|
154
|
+
- **Output**: SVG (browser + Node.js), PNG (Node.js only)
|
|
155
|
+
|
|
156
|
+
## Examples
|
|
157
|
+
|
|
158
|
+
### Automatic Differentiation
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
import { parameter } from "deepbox/ndarray";
|
|
162
|
+
|
|
163
|
+
const x = parameter([
|
|
164
|
+
[1, 2],
|
|
165
|
+
[3, 4],
|
|
166
|
+
]);
|
|
167
|
+
const w = parameter([[0.5], [0.5]]);
|
|
168
|
+
const y = x.matmul(w).sum();
|
|
169
|
+
y.backward();
|
|
170
|
+
// x.grad -> gradients w.r.t. x
|
|
171
|
+
// w.grad -> gradients w.r.t. w
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Neural Network Training
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
import { Sequential, Linear, ReLU, Dropout, mseLoss } from "deepbox/nn";
|
|
178
|
+
import { Adam } from "deepbox/optim";
|
|
179
|
+
|
|
180
|
+
const model = new Sequential(
|
|
181
|
+
new Linear(10, 64),
|
|
182
|
+
new ReLU(),
|
|
183
|
+
new Dropout(0.2),
|
|
184
|
+
new Linear(64, 32),
|
|
185
|
+
new ReLU(),
|
|
186
|
+
new Linear(32, 1),
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
const optimizer = new Adam(model.parameters(), { lr: 0.001 });
|
|
190
|
+
|
|
191
|
+
for (let epoch = 0; epoch < 100; epoch++) {
|
|
192
|
+
const output = model.forward(xTrain);
|
|
193
|
+
const loss = mseLoss(output, yTrain);
|
|
194
|
+
optimizer.zeroGrad();
|
|
195
|
+
loss.backward();
|
|
196
|
+
optimizer.step();
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### ML Pipeline
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
import { trainTestSplit, StandardScaler } from "deepbox/preprocess";
|
|
204
|
+
import { RandomForestClassifier } from "deepbox/ml";
|
|
205
|
+
import { accuracy, f1Score } from "deepbox/metrics";
|
|
206
|
+
|
|
207
|
+
const [XTrain, XTest, yTrain, yTest] = trainTestSplit(X, y, {
|
|
208
|
+
testSize: 0.2,
|
|
209
|
+
randomState: 42,
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
const scaler = new StandardScaler();
|
|
213
|
+
scaler.fit(XTrain);
|
|
214
|
+
const XTrainScaled = scaler.transform(XTrain);
|
|
215
|
+
const XTestScaled = scaler.transform(XTest);
|
|
216
|
+
|
|
217
|
+
const model = new RandomForestClassifier({ nEstimators: 100, maxDepth: 10 });
|
|
218
|
+
model.fit(XTrainScaled, yTrain);
|
|
219
|
+
|
|
220
|
+
const yPred = model.predict(XTestScaled);
|
|
221
|
+
console.log("Accuracy:", accuracy(yTest, yPred));
|
|
222
|
+
console.log("F1 Score:", f1Score(yTest, yPred));
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Classical ML Models
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
import {
|
|
229
|
+
DecisionTreeClassifier,
|
|
230
|
+
GradientBoostingClassifier,
|
|
231
|
+
KNeighborsClassifier,
|
|
232
|
+
LinearSVC,
|
|
233
|
+
} from "deepbox/ml";
|
|
234
|
+
|
|
235
|
+
const tree = new DecisionTreeClassifier({ maxDepth: 5 });
|
|
236
|
+
tree.fit(XTrain, yTrain);
|
|
237
|
+
|
|
238
|
+
const gb = new GradientBoostingClassifier({
|
|
239
|
+
nEstimators: 100,
|
|
240
|
+
learningRate: 0.1,
|
|
241
|
+
});
|
|
242
|
+
gb.fit(XTrain, yTrain);
|
|
243
|
+
|
|
244
|
+
const knn = new KNeighborsClassifier({ nNeighbors: 5 });
|
|
245
|
+
knn.fit(XTrain, yTrain);
|
|
246
|
+
|
|
247
|
+
const svm = new LinearSVC({ C: 1.0 });
|
|
248
|
+
svm.fit(XTrain, yTrain);
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### DataFrame Operations
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
import { DataFrame } from "deepbox/dataframe";
|
|
255
|
+
|
|
256
|
+
const df = new DataFrame({
|
|
257
|
+
name: ["Alice", "Bob", "Charlie", "David"],
|
|
258
|
+
age: [25, 30, 35, 28],
|
|
259
|
+
salary: [50000, 60000, 75000, 55000],
|
|
260
|
+
department: ["IT", "HR", "IT", "HR"],
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const itDept = df.filter((row) => row.department === "IT");
|
|
264
|
+
const avgSalary = df.groupBy("department").agg({ salary: "mean" });
|
|
265
|
+
const sorted = df.sort("salary", { ascending: false });
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Plotting
|
|
269
|
+
|
|
270
|
+
```ts
|
|
271
|
+
import { scatter, plot, hist, heatmap, saveFig } from "deepbox/plot";
|
|
272
|
+
import { tensor } from "deepbox/ndarray";
|
|
273
|
+
|
|
274
|
+
scatter(tensor([1, 2, 3, 4, 5]), tensor([2, 4, 5, 4, 6]), { color: "#1f77b4" });
|
|
275
|
+
plot(tensor([1, 2, 3, 4, 5]), tensor([2, 4, 5, 4, 6]), { color: "#ff7f0e" });
|
|
276
|
+
hist(tensor([1, 2, 2, 3, 3, 3, 4, 4, 5]), { bins: 5 });
|
|
277
|
+
heatmap(
|
|
278
|
+
tensor([
|
|
279
|
+
[1, 2, 3],
|
|
280
|
+
[4, 5, 6],
|
|
281
|
+
[7, 8, 9],
|
|
282
|
+
]),
|
|
283
|
+
);
|
|
284
|
+
saveFig("output.svg");
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
## Performance
|
|
288
|
+
|
|
289
|
+
Deepbox is pure TypeScript — no native addons, no WebAssembly, no C bindings. Every operation runs on V8’s JIT compiler with `TypedArray` backing. Despite competing against Python libraries that use hand-tuned C and Fortran backends (BLAS, LAPACK, ATen), Deepbox delivers competitive or superior performance in several areas.
|
|
290
|
+
|
|
291
|
+
**542 head-to-head benchmarks** across 10 categories, tested on the same machine with identical data sizes and iteration counts:
|
|
292
|
+
|
|
293
|
+
| Category | Deepbox Wins | Python Package Wins | Competing Against |
|
|
294
|
+
| --- | ---: | ---: | --- |
|
|
295
|
+
| DataFrames | 24 | 31 | Pandas (C / Cython) |
|
|
296
|
+
| Datasets | 11 | 30 | scikit-learn |
|
|
297
|
+
| Linear Algebra | 0 | 54 | NumPy + SciPy (LAPACK) |
|
|
298
|
+
| Metrics | 45 | 18 | scikit-learn (C / Cython) |
|
|
299
|
+
| ML Training | 16 | 32 | scikit-learn (C / Cython) |
|
|
300
|
+
| NDArray Ops | 6 | 88 | NumPy (C / BLAS) |
|
|
301
|
+
| Plotting | 43 | 0 | Matplotlib (C / Agg) |
|
|
302
|
+
| Preprocessing | 20 | 23 | scikit-learn (C / Cython) |
|
|
303
|
+
| Random | 0 | 44 | NumPy (C) |
|
|
304
|
+
| Statistics | 31 | 26 | SciPy (C / Fortran) |
|
|
305
|
+
| **Total** | **196** | **346** | |
|
|
306
|
+
|
|
307
|
+
### Where Deepbox shines
|
|
308
|
+
|
|
309
|
+
- **bar** (200 bars) — 16488.6x faster *(Plotting)*
|
|
310
|
+
- **KNeighborsClassifier fit** (200x5) — 48.4x faster *(ML Training)*
|
|
311
|
+
- **transpose** (500x500) — 43.9x faster *(NDArray Ops)*
|
|
312
|
+
- **fbetaScore (β=0.5)** (1K) — 34.0x faster *(Metrics)*
|
|
313
|
+
- **describe** (100x5) — 29.4x faster *(DataFrames)*
|
|
314
|
+
- **chisquare** (10 bins) — 18.5x faster *(Statistics)*
|
|
315
|
+
- **loadLinnerud** (20x3) — 15.3x faster *(Datasets)*
|
|
316
|
+
- **PowerTransformer fit** (500x10) — 6.3x faster *(Preprocessing)*
|
|
317
|
+
|
|
318
|
+
### Context
|
|
319
|
+
|
|
320
|
+
Python’s numerical libraries delegate heavy lifting to compiled C/Fortran code (OpenBLAS, MKL, LAPACK). Deepbox implements everything in TypeScript, relying on V8’s TurboFan JIT and `Float64Array` for performance. The gap is largest for BLAS-bound operations (matmul, decompositions) and smallest for memory-layout operations (transpose, reshape, indexing) where Deepbox’s lazy-view architecture has an advantage.
|
|
321
|
+
|
|
322
|
+
> Run `npm run bench:all` to reproduce. Full results in [`benchmarks/RESULTS.md`](benchmarks/RESULTS.md).
|
|
323
|
+
|
|
324
|
+
## Development
|
|
325
|
+
|
|
326
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for the full development workflow.
|
|
327
|
+
|
|
328
|
+
```bash
|
|
329
|
+
npm install # Install dependencies
|
|
330
|
+
npm run build # Build the package
|
|
331
|
+
npm test # Run 4 009 tests
|
|
332
|
+
npm run typecheck # Type checking
|
|
333
|
+
npm run lint # Lint with Biome
|
|
334
|
+
npm run format # Format with Biome
|
|
335
|
+
npm run all # Run all checks
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## License
|
|
339
|
+
|
|
340
|
+
MIT License — see [LICENSE](LICENSE) for details.
|
|
341
|
+
|
|
342
|
+
---
|
|
343
|
+
|
|
344
|
+
Built by [Jehaad Aljohani](https://github.com/jehaad1)
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { S as Shape } from './tensor-B96jjJLQ.cjs';
|
|
2
|
+
import { T as Tensor } from './Tensor-BQLk1ltW.cjs';
|
|
3
|
+
|
|
4
|
+
type CSRMatrixInit = {
|
|
5
|
+
readonly data: Float64Array;
|
|
6
|
+
readonly indices: Int32Array;
|
|
7
|
+
readonly indptr: Int32Array;
|
|
8
|
+
readonly shape: Shape;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Compressed Sparse Row (CSR) matrix representation.
|
|
12
|
+
*
|
|
13
|
+
* CSR format stores a sparse matrix using three arrays:
|
|
14
|
+
* - `data`: Non-zero values in row-major order
|
|
15
|
+
* - `indices`: Column indices of non-zero values
|
|
16
|
+
* - `indptr`: Row pointers (indptr[i] to indptr[i+1] gives the range of data/indices for row i)
|
|
17
|
+
*
|
|
18
|
+
* This format is efficient for:
|
|
19
|
+
* - Row slicing
|
|
20
|
+
* - Matrix-vector products
|
|
21
|
+
* - Arithmetic operations
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { CSRMatrix } from 'deepbox/ndarray';
|
|
26
|
+
*
|
|
27
|
+
* // Create a 3x3 sparse matrix with values at (0,0)=1, (1,2)=2, (2,1)=3
|
|
28
|
+
* const sparse = CSRMatrix.fromCOO({
|
|
29
|
+
* rows: 3, cols: 3,
|
|
30
|
+
* rowIndices: new Int32Array([0, 1, 2]),
|
|
31
|
+
* colIndices: new Int32Array([0, 2, 1]),
|
|
32
|
+
* values: new Float64Array([1, 2, 3])
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Convert to dense for operations
|
|
36
|
+
* const dense = sparse.toDense();
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @see {@link https://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_(CSR,_CRS_or_Yale_format) | Wikipedia: CSR}
|
|
40
|
+
*/
|
|
41
|
+
declare class CSRMatrix {
|
|
42
|
+
readonly data: Float64Array;
|
|
43
|
+
readonly indices: Int32Array;
|
|
44
|
+
readonly indptr: Int32Array;
|
|
45
|
+
readonly shape: Shape;
|
|
46
|
+
constructor(init: CSRMatrixInit);
|
|
47
|
+
/** Number of non-zero elements in the matrix */
|
|
48
|
+
get nnz(): number;
|
|
49
|
+
/** Number of rows in the matrix */
|
|
50
|
+
get rows(): number;
|
|
51
|
+
/** Number of columns in the matrix */
|
|
52
|
+
get cols(): number;
|
|
53
|
+
/**
|
|
54
|
+
* Convert the sparse matrix to a dense Tensor.
|
|
55
|
+
*
|
|
56
|
+
* @returns Dense 2D Tensor representation
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* const dense = sparse.toDense();
|
|
61
|
+
* console.log(dense.shape); // [rows, cols]
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
toDense(): Tensor;
|
|
65
|
+
/**
|
|
66
|
+
* Add two sparse matrices element-wise.
|
|
67
|
+
*
|
|
68
|
+
* Both matrices must have the same shape.
|
|
69
|
+
*
|
|
70
|
+
* @param other - Matrix to add
|
|
71
|
+
* @returns New CSRMatrix containing the sum
|
|
72
|
+
* @throws {ShapeError} If shapes don't match
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const c = a.add(b); // c = a + b
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
add(other: CSRMatrix): CSRMatrix;
|
|
80
|
+
/**
|
|
81
|
+
* Subtract another sparse matrix element-wise.
|
|
82
|
+
*
|
|
83
|
+
* Both matrices must have the same shape.
|
|
84
|
+
*
|
|
85
|
+
* @param other - Matrix to subtract
|
|
86
|
+
* @returns New CSRMatrix containing the difference
|
|
87
|
+
* @throws {ShapeError} If shapes don't match
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* const c = a.sub(b); // c = a - b
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
sub(other: CSRMatrix): CSRMatrix;
|
|
95
|
+
/**
|
|
96
|
+
* Multiply all elements by a scalar value.
|
|
97
|
+
*
|
|
98
|
+
* @param scalar - Value to multiply by
|
|
99
|
+
* @returns New CSRMatrix with scaled values
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* const scaled = matrix.scale(2.0); // Double all values
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
scale(scalar: number): CSRMatrix;
|
|
107
|
+
/**
|
|
108
|
+
* Element-wise multiplication (Hadamard product) with another sparse matrix.
|
|
109
|
+
*
|
|
110
|
+
* Both matrices must have the same shape.
|
|
111
|
+
*
|
|
112
|
+
* @param other - Matrix to multiply with
|
|
113
|
+
* @returns New CSRMatrix containing the element-wise product
|
|
114
|
+
* @throws {ShapeError} If shapes don't match
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* const c = a.multiply(b); // c[i,j] = a[i,j] * b[i,j]
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
multiply(other: CSRMatrix): CSRMatrix;
|
|
122
|
+
/**
|
|
123
|
+
* Matrix multiplication with a dense vector.
|
|
124
|
+
*
|
|
125
|
+
* Computes y = A * x where A is this sparse matrix and x is a dense vector.
|
|
126
|
+
*
|
|
127
|
+
* @param vector - Dense vector (1D Tensor or Float64Array)
|
|
128
|
+
* @returns Dense result vector as Tensor
|
|
129
|
+
* @throws {ShapeError} If vector length doesn't match matrix columns
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* const x = tensor([1, 2, 3]);
|
|
134
|
+
* const y = sparse.matvec(x); // y = A * x
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
matvec(vector: Tensor | Float64Array): Tensor;
|
|
138
|
+
/**
|
|
139
|
+
* Matrix multiplication with a dense matrix.
|
|
140
|
+
*
|
|
141
|
+
* Computes C = A * B where A is this sparse matrix and B is a dense matrix.
|
|
142
|
+
*
|
|
143
|
+
* @param dense - Dense matrix (2D Tensor)
|
|
144
|
+
* @returns Dense result matrix as Tensor
|
|
145
|
+
* @throws {ShapeError} If inner dimensions don't match
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* const B = tensor([[1, 2], [3, 4], [5, 6]]);
|
|
150
|
+
* const C = sparse.matmul(B); // C = A * B
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
matmul(dense: Tensor): Tensor;
|
|
154
|
+
/**
|
|
155
|
+
* Transpose the sparse matrix.
|
|
156
|
+
*
|
|
157
|
+
* @returns New CSRMatrix representing the transpose
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* const At = A.transpose(); // At[i,j] = A[j,i]
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
transpose(): CSRMatrix;
|
|
165
|
+
/**
|
|
166
|
+
* Get a specific element from the matrix.
|
|
167
|
+
*
|
|
168
|
+
* @param row - Row index
|
|
169
|
+
* @param col - Column index
|
|
170
|
+
* @returns Value at the specified position (0 if not stored)
|
|
171
|
+
* @throws {RangeError} If indices are out of bounds
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* const value = matrix.get(1, 2);
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
get(row: number, col: number): number;
|
|
179
|
+
/**
|
|
180
|
+
* Create a copy of this matrix.
|
|
181
|
+
*
|
|
182
|
+
* @returns New CSRMatrix with copied data
|
|
183
|
+
*/
|
|
184
|
+
copy(): CSRMatrix;
|
|
185
|
+
/** Helper to convert Tensor to Float64Array */
|
|
186
|
+
private tensorToFloat64;
|
|
187
|
+
/**
|
|
188
|
+
* Create a sparse matrix from COO (Coordinate List) format.
|
|
189
|
+
*
|
|
190
|
+
* @param args - COO format specification
|
|
191
|
+
* @param args.rows - Number of rows
|
|
192
|
+
* @param args.cols - Number of columns
|
|
193
|
+
* @param args.rowIndices - Row indices of non-zero values
|
|
194
|
+
* @param args.colIndices - Column indices of non-zero values
|
|
195
|
+
* @param args.values - Non-zero values
|
|
196
|
+
* @param args.sort - Whether to sort entries (default: true)
|
|
197
|
+
* @returns New CSRMatrix
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* const sparse = CSRMatrix.fromCOO({
|
|
202
|
+
* rows: 3, cols: 3,
|
|
203
|
+
* rowIndices: new Int32Array([0, 1, 2]),
|
|
204
|
+
* colIndices: new Int32Array([0, 2, 1]),
|
|
205
|
+
* values: new Float64Array([1, 2, 3])
|
|
206
|
+
* });
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
static fromCOO(args: {
|
|
210
|
+
readonly rows: number;
|
|
211
|
+
readonly cols: number;
|
|
212
|
+
readonly rowIndices: Int32Array;
|
|
213
|
+
readonly colIndices: Int32Array;
|
|
214
|
+
readonly values: Float64Array;
|
|
215
|
+
readonly sort?: boolean;
|
|
216
|
+
}): CSRMatrix;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export { CSRMatrix as C, type CSRMatrixInit as a };
|