24
Tensors
Metrics
Model Evaluation Metrics
Learn to evaluate models using various performance metrics. Different metrics for classification, regression, and clustering. This example uses deepbox/ndarray, deepbox/metrics and focuses on tensor; accuracy, precision, recall, f1Score, confusionMatrix, r2Score, mse, rmse, mae, mape, silhouetteScore.
Deepbox Modules Used
deepbox/ndarraydeepbox/metricsWhat You Will Learn
- Use deepbox/ndarray for tensor.
- Use deepbox/metrics for accuracy, precision, recall, f1Score, confusionMatrix, r2Score, mse, rmse, mae, mape, silhouetteScore.
- Learn to evaluate models using various performance metrics. Different metrics for classification, regression, and clustering.
Source Files
index.ts
1/**2 * Example 24: Model Evaluation Metrics3 *4 * Learn to evaluate models using various performance metrics.5 * Different metrics for classification, regression, and clustering.6 */78import {9 // Classification metrics10 accuracy,11 confusionMatrix,12 f1Score,13 mae,14 mape,15 mse,16 precision,17 // Regression metrics18 r2Score,19 recall,20 rmse,21 // Clustering metrics22 silhouetteScore,23} from "deepbox/metrics";24import { tensor } from "deepbox/ndarray";2526console.log("=== Model Evaluation Metrics ===\n");2728// Classification Metrics29console.log("1. Classification Metrics:");30console.log("-".repeat(50));3132const y_true_class = tensor([1, 0, 1, 1, 0, 1, 0, 0, 1, 1]);33const y_pred_class = tensor([1, 0, 1, 0, 0, 1, 0, 1, 1, 1]);3435console.log("True labels:", y_true_class.toString());36console.log("Predictions:", `${y_pred_class.toString()}\n`);3738const acc = accuracy(y_true_class, y_pred_class);39const prec = precision(y_true_class, y_pred_class);40const rec = recall(y_true_class, y_pred_class);41const f1 = f1Score(y_true_class, y_pred_class);4243console.log(`Accuracy: ${(Number(acc) * 100).toFixed(2)}%`);44console.log(`Precision: ${(Number(prec) * 100).toFixed(2)}%`);45console.log(`Recall: ${(Number(rec) * 100).toFixed(2)}%`);46console.log(`F1-Score: ${(Number(f1) * 100).toFixed(2)}%\n`);4748const cm = confusionMatrix(y_true_class, y_pred_class);49console.log("Confusion Matrix:");50console.log(cm.toString());51console.log("Format: [[TN, FP], [FN, TP]]\n");5253// Regression Metrics54console.log("2. Regression Metrics:");55console.log("-".repeat(50));5657const y_true_reg = tensor([3.0, -0.5, 2.0, 7.0, 4.2]);58const y_pred_reg = tensor([2.5, 0.0, 2.1, 7.8, 4.0]);5960console.log("True values:", y_true_reg.toString());61console.log("Predictions:", `${y_pred_reg.toString()}\n`);6263const r2 = r2Score(y_true_reg, y_pred_reg);64const mseVal = mse(y_true_reg, y_pred_reg);65const rmseVal = rmse(y_true_reg, y_pred_reg);66const maeVal = mae(y_true_reg, y_pred_reg);67const mapeVal = mape(y_true_reg, y_pred_reg);6869console.log(`R² Score: ${r2.toFixed(4)}`);70console.log(`MSE: ${mseVal.toFixed(4)}`);71console.log(`RMSE: ${rmseVal.toFixed(4)}`);72console.log(`MAE: ${maeVal.toFixed(4)}`);73console.log(`MAPE: ${(mapeVal * 100).toFixed(2)}%\n`);7475// Clustering Metrics76console.log("3. Clustering Metrics:");77console.log("-".repeat(50));7879const X_cluster = tensor([80 [1, 2],81 [1.5, 1.8],82 [5, 8],83 [8, 8],84 [1, 0.6],85 [9, 11],86]);87const labels = tensor([0, 0, 1, 1, 0, 1]);8889const silhouette = silhouetteScore(X_cluster, labels);90console.log(`Silhouette Score: ${silhouette.toFixed(4)}`);91console.log("Range: [-1, 1], higher is better");92console.log("Measures how similar points are to their own cluster\n");9394console.log("Metric Selection Guide:");95console.log("• Classification: Use F1-score for imbalanced data");96console.log("• Regression: R² for variance explained, MAE for interpretability");97console.log("• Clustering: Silhouette for cluster quality");9899console.log("\n✓ Metrics evaluation complete!");100Console Output
$ npx tsx 24-metrics/index.ts
Console output showing classification metrics, regression metrics, and clustering metrics with a selection guide