00
Basics
ML
DataFrame
Quick Start Guide
This example walks you through the three pillars of Deepbox in a single file. You start by creating tensors ā the fundamental N-dimensional arrays that power every computation in the library ā and performing element-wise addition and computing a mean. Next, you construct a DataFrame from plain objects, giving you a tabular data structure with named columns. Finally, you train a LinearRegression model on synthetic data (y = 2x + 1), split into train/test sets, and verify that the model correctly predicts unseen values. By the end, you will understand how Deepbox's modules compose together to form a complete data science workflow entirely in TypeScript.
Deepbox Modules Used
deepbox/ndarraydeepbox/dataframedeepbox/mldeepbox/preprocessWhat You Will Learn
- Create tensors from JavaScript arrays using tensor()
- Perform element-wise arithmetic with add(), and compute statistics with mean()
- Build tabular datasets with DataFrame from plain objects
- Train and evaluate a LinearRegression model end-to-end
- Use trainTestSplit to hold out data for evaluation
Source Code
00-quick-start/index.ts
1import { DataFrame } from "deepbox/dataframe";2import { LinearRegression } from "deepbox/ml";3import { add, mean, tensor } from "deepbox/ndarray";4import { trainTestSplit } from "deepbox/preprocess";56console.log("š Welcome to Deepbox!\n");78// 1. Tensors (N-dimensional arrays)9console.log("1ļøā£ Tensors:");10const a = tensor([1, 2, 3, 4, 5]);11const b = tensor([10, 20, 30, 40, 50]);12const c = add(a, b);13console.log(" a + b =", c.toString());14console.log(" mean(a) =", `${mean(a).toString()}\n`);1516// 2. DataFrames (tabular data)17console.log("2ļøā£ DataFrames:");18const df = new DataFrame({19 name: ["Alice", "Bob", "Charlie"],20 age: [25, 30, 35],21 score: [85, 90, 78],22});23console.log(`${df.toString()}\n`);2425// 3. Machine Learning26console.log("3ļøā£ Machine Learning:");2728// Generate simple data: y = 2x + 129const X = tensor([[1], [2], [3], [4], [5], [6], [7], [8]]);30const y = tensor([3, 5, 7, 9, 11, 13, 15, 17]);3132const [X_train, X_test, y_train, y_test] = trainTestSplit(X, y, {33 testSize: 0.25,34 randomState: 42,35});3637const model = new LinearRegression();38model.fit(X_train, y_train);39const predictions = model.predict(X_test);4041console.log(" Trained linear regression model");42console.log(" Predictions:", predictions.toString());43console.log(" Actual: ", y_test.toString());4445console.log("\n⨠That's Deepbox in a nutshell!");Console Output
$ npx tsx 00-quick-start/index.ts
š Welcome to Deepbox!
1ļøā£ Tensors:
a + b = [11, 22, 33, 44, 55]
mean(a) = 3
2ļøā£ DataFrames:
āāāāāāāāāāā¬āāāāāā¬āāāāāāāā
ā name ā age ā score ā
āāāāāāāāāāā¼āāāāāā¼āāāāāāāā¤
ā Alice ā 25 ā 85 ā
ā Bob ā 30 ā 90 ā
ā Charlie ā 35 ā 78 ā
āāāāāāāāāāā“āāāāāā“āāāāāāāā
3ļøā£ Machine Learning:
Trained linear regression model
Predictions: [5, 9, 13, 17]
Actual: [5, 9, 13, 17]
⨠That's Deepbox in a nutshell!