08
Classification
ML Basics
Logistic Regression
Logistic regression models the probability of class membership using a sigmoid function. This example loads the Iris dataset, creates a binary classification target (setosa vs. rest), splits and scales the data, trains a LogisticRegression model, and evaluates with accuracy, precision, recall, F1 score, and confusion matrix.
Deepbox Modules Used
deepbox/datasetsdeepbox/mldeepbox/metricsdeepbox/preprocessWhat You Will Learn
- Train a LogisticRegression classifier for binary classification
- Preprocess with StandardScaler for better convergence
- Evaluate with accuracy, precision, recall, F1 score
- Read confusion matrices: rows are true labels, columns are predictions
Source Code
08-logistic-regression/index.ts
1import { loadIris } from "deepbox/datasets";2import { accuracy, confusionMatrix, f1Score, precision, recall } from "deepbox/metrics";3import { LogisticRegression } from "deepbox/ml";4import { tensor } from "deepbox/ndarray";5import { StandardScaler, trainTestSplit } from "deepbox/preprocess";67console.log("=== Logistic Regression ===\n");89// Load the famous Iris dataset for classification10const iris = loadIris();11console.log(`Dataset: ${iris.data.shape[0]} samples, ${iris.data.shape[1]} features\n`);1213// Simplify to binary classification: setosa (0) vs non-setosa (1)14const y_binary: number[] = [];15// Convert multi-class labels to binary16for (let i = 0; i < iris.target.size; i++) {17 const label = Number(iris.target.data[iris.target.offset + i]);18 // Map setosa to 0 and other classes to 119 y_binary.push(label === 0 ? 0 : 1);20}21const y = tensor(y_binary);2223// Split into train (70%) and test (30%) sets24const [X_train, X_test, y_train, y_test] = trainTestSplit(iris.data, y, {25 testSize: 0.3,26 randomState: 42,27});2829console.log(`Training set: ${X_train.shape[0]} samples`);30console.log(`Test set: ${X_test.shape[0]} samples\n`);3132// Standardize features (mean=0, std=1) for better convergence33const scaler = new StandardScaler();34scaler.fit(X_train);35const X_train_scaled = scaler.transform(X_train);36const X_test_scaled = scaler.transform(X_test);3738console.log("Features scaled\n");3940// Create and train logistic regression classifier41const model = new LogisticRegression({ maxIter: 1000, learningRate: 0.1 });42model.fit(X_train_scaled, y_train);4344console.log("Model trained!\n");4546// Make predictions on test data47const y_pred = model.predict(X_test_scaled);4849// Calculate classification metrics50const acc = accuracy(y_test, y_pred);51const prec = precision(y_test, y_pred);52const rec = recall(y_test, y_pred);53const f1 = f1Score(y_test, y_pred);5455console.log("Model Performance:");56console.log(`Accuracy: ${(Number(acc) * 100).toFixed(2)}%`);57console.log(`Precision: ${(Number(prec) * 100).toFixed(2)}%`);58console.log(`Recall: ${(Number(rec) * 100).toFixed(2)}%`);59console.log(`F1-Score: ${(Number(f1) * 100).toFixed(2)}%\n`);6061const cm = confusionMatrix(y_test, y_pred);62console.log("Confusion Matrix:");63console.log(cm.toString());6465console.log("\n✓ Logistic regression complete!");Console Output
$ npx tsx 08-logistic-regression/index.ts
=== Logistic Regression ===
Dataset: 150 samples, 4 features
Training set: 105 samples
Test set: 45 samples
Features scaled
Model trained!
Model Performance:
Accuracy: 100.00%
Precision: 100.00%
Recall: 100.00%
F1-Score: 100.00%
Confusion Matrix:
tensor([[13, 0]
[0, 32]], dtype=float32)
✓ Logistic regression complete!