Example 36
advanced
36
ML
Metrics
Datasets
Preprocessing

Kernel SVM & Anomaly Detection

Support Vector Machines with kernel tricks and anomaly detection algorithms new in v1.0.0. This example uses deepbox/ml, deepbox/metrics, deepbox/datasets, deepbox/preprocess and focuses on SVC, SVR, NuSVC, OneClassSVM, IsolationForest, LocalOutlierFactor; accuracy, r2Score; makeClassification, makeRegression; trainTestSplit, StandardScaler.

Deepbox Modules Used

deepbox/mldeepbox/metricsdeepbox/datasetsdeepbox/preprocess

What You Will Learn

  • Use deepbox/ml for SVC, SVR, NuSVC, OneClassSVM, IsolationForest, LocalOutlierFactor.
  • Use deepbox/metrics for accuracy, r2Score.
  • Use deepbox/datasets for makeClassification, makeRegression.
  • Use deepbox/preprocess for trainTestSplit, StandardScaler.
  • Support Vector Machines with kernel tricks and anomaly detection algorithms new in v1.0.0.

Source Files

index.ts
1/**2 * Example 36: Kernel SVM & Anomaly Detection3 *4 * New in v1.0.0: Support Vector Machines with kernel tricks (SVC, SVR, NuSVC,5 * NuSVR, OneClassSVM) and anomaly detection (IsolationForest, LocalOutlierFactor).6 */78import { makeClassification, makeRegression } from "deepbox/datasets";9import { accuracy, r2Score } from "deepbox/metrics";10import { IsolationForest, LocalOutlierFactor, NuSVC, OneClassSVM, SVC, SVR } from "deepbox/ml";11import { tensor } from "deepbox/ndarray";12import { StandardScaler, trainTestSplit } from "deepbox/preprocess";1314console.log("=".repeat(60));15console.log("Example 36: Kernel SVM & Anomaly Detection");16console.log("=".repeat(60));1718// ============================================================================19// Generate datasets20// ============================================================================2122const [XClass, yClass] = makeClassification({23  nSamples: 200,24  nFeatures: 6,25  nInformative: 4,26  nClasses: 2,27  randomState: 42,28});2930const [XReg, yReg] = makeRegression({31  nSamples: 150,32  nFeatures: 5,33  noise: 5,34  randomState: 42,35});3637// Scale features for SVM (important for kernel methods)38const scaler = new StandardScaler();39const XClassScaled = scaler.fitTransform(XClass);4041const scalerReg = new StandardScaler();42const XRegScaled = scalerReg.fitTransform(XReg);4344const [XTrain, XTest, yTrain, yTest] = trainTestSplit(XClassScaled, yClass, {45  testSize: 0.25,46  randomState: 42,47});4849const [XTrainR, XTestR, yTrainR, yTestR] = trainTestSplit(XRegScaled, yReg, {50  testSize: 0.25,51  randomState: 42,52});5354// ============================================================================55// Part 1: SVC with RBF Kernel56// ============================================================================57console.log("\n🎯 Part 1: SVC with RBF Kernel");58console.log("-".repeat(60));5960// SVC uses the kernel trick to find nonlinear decision boundaries61const svcRbf = new SVC({62  C: 1.0,63  kernel: "rbf",64  gamma: "scale",65});66svcRbf.fit(XTrain, yTrain);6768const svcRbfPred = svcRbf.predict(XTest);69const svcRbfAcc = accuracy(yTest, svcRbfPred);7071console.log("SVC (RBF kernel, C=1.0, gamma=scale):");72console.log(`  Accuracy: ${(Number(svcRbfAcc) * 100).toFixed(2)}%`);7374// ============================================================================75// Part 2: SVC with Different Kernels76// ============================================================================77console.log("\n🔀 Part 2: SVC Kernel Comparison");78console.log("-".repeat(60));7980// Compare different kernel functions81for (const kernel of ["linear", "poly", "rbf", "sigmoid"] as const) {82  const svc = new SVC({ C: 1.0, kernel, gamma: "scale" });83  svc.fit(XTrain, yTrain);84  const pred = svc.predict(XTest);85  const acc = accuracy(yTest, pred);86  console.log(`  ${kernel.padEnd(8)} kernel — Accuracy: ${(Number(acc) * 100).toFixed(2)}%`);87}8889// ============================================================================90// Part 3: SVC with Regularization Tuning91// ============================================================================92console.log("\n⚙️  Part 3: SVC Regularization (C parameter)");93console.log("-".repeat(60));9495// C controls the trade-off between margin width and classification error96for (const C of [0.01, 0.1, 1.0, 10.0, 100.0]) {97  const svc = new SVC({ C, kernel: "rbf", gamma: "scale" });98  svc.fit(XTrain, yTrain);99  const pred = svc.predict(XTest);100  const acc = accuracy(yTest, pred);101  console.log(`  C=${String(C).padEnd(6)} — Accuracy: ${(Number(acc) * 100).toFixed(2)}%`);102}103104// ============================================================================105// Part 4: NuSVC106// ============================================================================107console.log("\n📊 Part 4: NuSVC");108console.log("-".repeat(60));109110// NuSVC uses nu parameter instead of C to control the number of support vectors111const nuSvc = new NuSVC({112  nu: 0.5,113  kernel: "rbf",114  gamma: "scale",115});116nuSvc.fit(XTrain, yTrain);117118const nuPred = nuSvc.predict(XTest);119const nuAcc = accuracy(yTest, nuPred);120121console.log("NuSVC (nu=0.5, RBF kernel):");122console.log(`  Accuracy: ${(Number(nuAcc) * 100).toFixed(2)}%`);123124// ============================================================================125// Part 5: SVR (Support Vector Regression)126// ============================================================================127console.log("\n📈 Part 5: SVR (Support Vector Regression)");128console.log("-".repeat(60));129130// SVR applies the kernel trick to regression problems131const svr = new SVR({132  C: 1.0,133  kernel: "rbf",134  gamma: "scale",135});136svr.fit(XTrainR, yTrainR);137138const svrPred = svr.predict(XTestR);139const svrR2 = r2Score(yTestR, svrPred);140141console.log("SVR (RBF kernel, C=1.0):");142console.log(`  R² Score: ${Number(svrR2).toFixed(4)}`);143144// Compare SVR kernels145for (const kernel of ["linear", "rbf", "poly"] as const) {146  const sv = new SVR({ C: 1.0, kernel, gamma: "scale" });147  sv.fit(XTrainR, yTrainR);148  const pred = sv.predict(XTestR);149  const r2 = r2Score(yTestR, pred);150  console.log(`  ${kernel.padEnd(8)} kernel — R²: ${Number(r2).toFixed(4)}`);151}152153// ============================================================================154// Part 6: Isolation Forest (Anomaly Detection)155// ============================================================================156console.log("\n🌲 Part 6: Isolation Forest");157console.log("-".repeat(60));158159// Create normal data with some outliers160const normalData = tensor([161  [1, 2],162  [1.5, 1.8],163  [1.2, 2.1],164  [0.8, 1.9],165  [1.3, 2.3],166  [2, 1],167  [1.8, 1.5],168  [2.2, 1.2],169  [1.9, 0.8],170  [2.1, 1.3],171  [1.5, 1.5],172  [1.7, 1.7],173  [1.3, 1.6],174  [1.6, 1.4],175  [1.4, 1.8],176  [10, 10],177  [-5, -5],178  [8, -3], // outliers179]);180181// IsolationForest detects anomalies by how quickly points are isolated182const iforest = new IsolationForest({183  nEstimators: 100,184  contamination: 0.15,185  randomState: 42,186});187iforest.fit(normalData);188189const ifoLabels = iforest.predict(normalData);190const ifoScores = iforest.scoreSamples(normalData);191192console.log("Isolation Forest (100 trees, contamination=0.15):");193console.log(`  Labels (1=inlier, -1=outlier): ${ifoLabels.toString()}`);194console.log(`  Anomaly scores: ${ifoScores.toString()}`);195196// Count detected outliers197const labelData = ifoLabels.data as Int32Array;198let outlierCount = 0;199for (let i = 0; i < labelData.length; i++) {200  if (labelData[i] === -1) outlierCount++;201}202console.log(`  Detected ${outlierCount} outliers out of ${normalData.shape[0]} samples`);203204// ============================================================================205// Part 7: Local Outlier Factor (LOF)206// ============================================================================207console.log("\n🔍 Part 7: Local Outlier Factor (LOF)");208console.log("-".repeat(60));209210// LOF measures local deviation of density compared to neighbors211const lof = new LocalOutlierFactor({212  nNeighbors: 5,213  contamination: 0.15,214});215lof.fit(normalData);216217const lofLabels = lof.predict(normalData);218219console.log("Local Outlier Factor (k=5, contamination=0.15):");220console.log(`  Labels (1=inlier, -1=outlier): ${lofLabels.toString()}`);221222const lofLabelData = lofLabels.data as Int32Array;223let lofOutlierCount = 0;224for (let i = 0; i < lofLabelData.length; i++) {225  if (lofLabelData[i] === -1) lofOutlierCount++;226}227console.log(`  Detected ${lofOutlierCount} outliers out of ${normalData.shape[0]} samples`);228229// ============================================================================230// Part 8: OneClassSVM231// ============================================================================232console.log("\n🛡️  Part 8: OneClassSVM");233console.log("-".repeat(60));234235// OneClassSVM learns a boundary around normal data points236const ocsvm = new OneClassSVM({237  nu: 0.15,238  kernel: "rbf",239  gamma: "scale",240});241ocsvm.fit(normalData);242243const ocLabels = ocsvm.predict(normalData);244245console.log("OneClassSVM (nu=0.15, RBF kernel):");246console.log(`  Labels (1=inlier, -1=outlier): ${ocLabels.toString()}`);247248const ocLabelData = ocLabels.data as Int32Array;249let ocOutlierCount = 0;250for (let i = 0; i < ocLabelData.length; i++) {251  if (ocLabelData[i] === -1) ocOutlierCount++;252}253console.log(`  Detected ${ocOutlierCount} outliers out of ${normalData.shape[0]} samples`);254255// ============================================================================256// Summary257// ============================================================================258console.log("\n💡 Key Takeaways");259console.log("-".repeat(60));260console.log("• SVC: kernel-based classification with RBF, polynomial, linear, sigmoid kernels");261console.log("• NuSVC: nu parameter controls fraction of support vectors (alternative to C)");262console.log("• SVR: kernel regression for nonlinear relationships");263console.log("• Feature scaling is crucial for kernel SVMs (use StandardScaler)");264console.log("• IsolationForest: tree-based anomaly detection, fast and scalable");265console.log("• LOF: density-based anomaly detection, measures local deviation");266console.log("• OneClassSVM: learns a boundary around normal data in kernel space");267console.log("• Anomaly detectors output +1 (inlier) and -1 (outlier)");268269console.log("\n✅ Kernel SVM & Anomaly Detection Example Complete!");270console.log("=".repeat(60));271

Console Output

$ npx tsx 36-svm-anomaly-detection/index.ts
See the example README for the expected console and artifact output.