Example 00
beginner
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/preprocess

What 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!