10
ML
Tensors
Metrics
Preprocessing
Advanced ML Models
Demonstrates advanced ML models: KMeans clustering, K-Nearest Neighbors (classification and regression), PCA dimensionality reduction, and Gaussian Naive Bayes. This example uses deepbox/ml, deepbox/ndarray, deepbox/metrics, deepbox/preprocess and focuses on KMeans, KNeighborsClassifier, KNeighborsRegressor, PCA, GaussianNB; tensor; accuracy; trainTestSplit.
Deepbox Modules Used
deepbox/mldeepbox/ndarraydeepbox/metricsdeepbox/preprocessWhat You Will Learn
- Use deepbox/ml for KMeans, KNeighborsClassifier, KNeighborsRegressor, PCA, GaussianNB.
- Use deepbox/ndarray for tensor.
- Use deepbox/metrics for accuracy.
- Use deepbox/preprocess for trainTestSplit.
- Demonstrates advanced ML models: KMeans clustering, K-Nearest Neighbors (classification and regression), PCA dimensionality reduction, and Gaussian Naive Bayes.
Source Files
index.ts
1/**2 * Advanced ML Models Example3 *4 * Demonstrates the new ML models added to Deepbox:5 * - KMeans clustering6 * - K-Nearest Neighbors (classification and regression)7 * - PCA (dimensionality reduction)8 * - Gaussian Naive Bayes9 */1011import { isNumericTypedArray, isTypedArray } from "deepbox/core";12import { accuracy } from "deepbox/metrics";13import { GaussianNB, KMeans, KNeighborsClassifier, KNeighborsRegressor, PCA } from "deepbox/ml";14import { tensor } from "deepbox/ndarray";15import { trainTestSplit } from "deepbox/preprocess";1617const expectNumericTypedArray = (18 value: unknown19): Float32Array | Float64Array | Int32Array | Uint8Array => {20 if (!isTypedArray(value) || !isNumericTypedArray(value)) {21 throw new Error("Expected numeric typed array");22 }23 return value;24};2526console.log("=".repeat(60));27console.log("Example 10: Advanced ML Models");28console.log("=".repeat(60));2930// ============================================================================31// Part 1: KMeans Clustering32// ============================================================================33console.log("\n📦 Part 1: KMeans Clustering");34console.log("-".repeat(60));3536const clusterData = tensor([37 [1, 2],38 [1.5, 1.8],39 [5, 8],40 [8, 8],41 [1, 0.6],42 [9, 11],43 [8, 2],44 [10, 2],45 [9, 3],46]);4748const kmeans = new KMeans({ nClusters: 3, randomState: 42 });49kmeans.fit(clusterData);5051const clusterLabels = kmeans.predict(clusterData);52console.log("Cluster labels:", clusterLabels.toString());53console.log("Cluster centers shape:", kmeans.clusterCenters.shape);54console.log("Inertia:", kmeans.inertia.toFixed(4));55console.log("Number of iterations:", kmeans.nIter);5657// ============================================================================58// Part 2: K-Nearest Neighbors Classification59// ============================================================================60console.log("\n📦 Part 2: K-Nearest Neighbors Classification");61console.log("-".repeat(60));6263const XClass = tensor([64 [0, 0],65 [1, 1],66 [2, 2],67 [3, 3],68 [4, 4],69 [5, 5],70 [6, 6],71 [7, 7],72]);73const yClass = tensor([0, 0, 0, 0, 1, 1, 1, 1]);7475const [XTrainKNN, XTestKNN, yTrainKNN, yTestKNN] = trainTestSplit(XClass, yClass, {76 testSize: 0.25,77 randomState: 42,78});7980const knnClassifier = new KNeighborsClassifier({ nNeighbors: 3 });81knnClassifier.fit(XTrainKNN, yTrainKNN);8283const yPredKNN = knnClassifier.predict(XTestKNN);84const knnAccuracy = accuracy(yTestKNN, yPredKNN);8586console.log("KNN Classifier trained with k=3");87console.log("Test accuracy:", `${(Number(knnAccuracy) * 100).toFixed(2)}%`);8889const probabilities = knnClassifier.predictProba(XTestKNN);90console.log("Prediction probabilities shape:", probabilities.shape);9192// ============================================================================93// Part 3: K-Nearest Neighbors Regression94// ============================================================================95console.log("\n📦 Part 3: K-Nearest Neighbors Regression");96console.log("-".repeat(60));9798const XReg = tensor([[0], [1], [2], [3], [4], [5]]);99const yReg = tensor([0, 1, 4, 9, 16, 25]);100101const knnRegressor = new KNeighborsRegressor({ nNeighbors: 2 });102knnRegressor.fit(XReg, yReg);103104const yPredReg = knnRegressor.predict(tensor([[2.5], [3.5]]));105console.log("Predictions for [2.5] and [3.5]:", yPredReg.toString());106107const r2Score = knnRegressor.score(XReg, yReg);108console.log("R² score:", r2Score.toFixed(4));109110// ============================================================================111// Part 4: PCA (Dimensionality Reduction)112// ============================================================================113console.log("\n📦 Part 4: PCA - Dimensionality Reduction");114console.log("-".repeat(60));115116const XPca = tensor([117 [2.5, 2.4, 1.1],118 [0.5, 0.7, 0.3],119 [2.2, 2.9, 1.5],120 [1.9, 2.2, 0.9],121 [3.1, 3.0, 1.8],122 [2.3, 2.7, 1.2],123 [2.0, 1.6, 0.8],124 [1.0, 1.1, 0.5],125 [1.5, 1.6, 0.7],126 [1.1, 0.9, 0.4],127]);128129const pca = new PCA({ nComponents: 2 });130pca.fit(XPca);131132const XTransformed = pca.transform(XPca);133console.log("Original shape:", XPca.shape);134console.log("Transformed shape:", XTransformed.shape);135console.log("Explained variance ratio:", pca.explainedVarianceRatio.toString());136137const varianceData = expectNumericTypedArray(pca.explainedVarianceRatio.data);138const totalVariance = Array.from(varianceData).reduce((a, b) => a + b, 0);139console.log("Total variance explained:", `${(totalVariance * 100).toFixed(2)}%`);140141// Reconstruct data142const XReconstructed = pca.inverseTransform(XTransformed);143console.log("Reconstructed shape:", XReconstructed.shape);144145// ============================================================================146// Part 5: Gaussian Naive Bayes147// ============================================================================148console.log("\n📦 Part 5: Gaussian Naive Bayes");149console.log("-".repeat(60));150151const XNB = tensor([152 [1, 2],153 [2, 3],154 [3, 4],155 [4, 5],156 [5, 6],157 [6, 7],158 [7, 8],159 [8, 9],160]);161const yNB = tensor([0, 0, 0, 0, 1, 1, 1, 1]);162163const [XTrainNB, XTestNB, yTrainNB, yTestNB] = trainTestSplit(XNB, yNB, {164 testSize: 0.25,165 randomState: 42,166});167168const nb = new GaussianNB();169nb.fit(XTrainNB, yTrainNB);170171const yPredNB = nb.predict(XTestNB);172const nbAccuracy = accuracy(yTestNB, yPredNB);173174console.log("Gaussian Naive Bayes trained");175console.log("Test accuracy:", `${(Number(nbAccuracy) * 100).toFixed(2)}%`);176177const nbProba = nb.predictProba(XTestNB);178console.log("Prediction probabilities shape:", nbProba.shape);179180// ============================================================================181// Summary182// ============================================================================183console.log("\n💡 Key Takeaways");184console.log("-".repeat(60));185console.log("• KMeans: Unsupervised clustering for grouping similar data points");186console.log("• KNN: Instance-based learning for classification and regression");187console.log("• PCA: Dimensionality reduction while preserving variance");188console.log("• Naive Bayes: Probabilistic classifier based on Bayes' theorem");189console.log("• All models follow the fit/predict/score API (fit/predict/score)");190191console.log("\n✅ Advanced ML Models Example Complete!");192console.log("=".repeat(60));193Console Output
$ npx tsx 10-advanced-ml-models/index.ts
Console output showing clustering labels, KNN accuracy, PCA variance, and Naive Bayes predictions