Loading...

Classification

Build and evaluate models to predict categorical outcomes.


Overview

Classification algorithms predict categorical response variables. They are used for:

  • Binary classification – Two classes (e.g., fraud/not fraud, churn/no churn)

  • Multiclass classification – Multiple classes (e.g., flower species, product categories)

Common use cases:

  • Customer churn prediction

  • Fraud detection

  • Image classification

  • Sentiment analysis

  • Disease diagnosis


Build a Classification Model

We’ll predict flower species using the Iris dataset with a Random Forest Classifier.

Import the model:

from vastorbit.machine_learning.vast import RandomForestClassifier

Load the dataset:

from vastorbit.datasets import load_iris

iris = load_iris()
iris.head(5)

Initialize the model:

model = RandomForestClassifier(
    n_estimators = 5,
    max_depth = 3,
)

Train the model:

model.fit(
    iris,
    X=["PetalLengthCm", "SepalLengthCm"],
    y="Species",
)

Tip

All computation happens in-database. No data is moved to Python memory.


Evaluate Model Performance

Generate classification report:

model.report()
Iris-setosaIris-versicolorIris-virginicaavg_macroavg_weightedavg_micro
auc1.00.98430000000000010.98730.99053333333333340.9905333333333333[null]
prc_auc0.61666666666666670.345970695970695960.53874757341424020.50046164535053430.5004616453505343[null]
accuracy1.00.95333333333333340.95333333333333340.96888888888888890.96888888888888890.9688888888888889
log_loss0.0221175018787443350.166039283838517180.150732790276652730.11296319199797140.11296319199797142[null]
precision1.00.97777777777777770.89090909090909090.95622895622895620.95622895622895610.9533333333333334
recall1.00.880.980.95333333333333330.95333333333333340.9533333333333334
f1_score1.00.92631578947368420.93333333333333330.95321637426900590.95321637426900590.9533333333333334
mcc1.00.89495942978013310.8999734180019140.93164428259401570.93164428259401580.93
informedness1.00.87000000000000010.91999999999999990.930.930.9300000000000002
markedness1.00.92063492063492050.88038277511961720.93367256525151240.93367256525151250.9300000000000002
csi1.00.86274509803921570.8750.91258169934640520.91258169934640520.910828025477707

Key metrics:

  • Accuracy – Overall correctness (correct predictions / total predictions)

  • Precision – Of predicted positives, how many are actually positive

  • Recall – Of actual positives, how many were correctly identified

  • F1-Score – Harmonic mean of precision and recall

  • AUC – Area under ROC curve (discrimination ability)


Make Predictions

Predict class labels:

model.predict(iris, name="prediction")
123
sepallengthcm
Decimal(5,2)
100%
123
sepalwidthcm
Decimal(5,2)
100%
123
petallengthcm
Decimal(5,2)
100%
123
petalwidthcm
Decimal(5,2)
100%
Abc
species
Varchar(30)
100%
Abc
prediction
Varchar(15)
100%
15.13.51.40.2Iris-setosaIris-setosa
24.93.01.40.2Iris-setosaIris-setosa
34.73.21.30.2Iris-setosaIris-setosa
44.63.11.50.2Iris-setosaIris-setosa
55.03.61.40.2Iris-setosaIris-setosa
65.43.91.70.4Iris-setosaIris-setosa
74.63.41.40.3Iris-setosaIris-setosa
85.03.41.50.2Iris-setosaIris-setosa
94.42.91.40.2Iris-setosaIris-setosa
104.93.11.50.1Iris-setosaIris-setosa
115.43.71.50.2Iris-setosaIris-setosa
124.83.41.60.2Iris-setosaIris-setosa
134.83.01.40.1Iris-setosaIris-setosa
144.33.01.10.1Iris-setosaIris-setosa
155.84.01.20.2Iris-setosaIris-setosa
165.74.41.50.4Iris-setosaIris-setosa
175.43.91.30.4Iris-setosaIris-setosa
185.13.51.40.3Iris-setosaIris-setosa
195.73.81.70.3Iris-setosaIris-setosa
205.13.81.50.3Iris-setosaIris-setosa

Predict class probabilities:

model.predict_proba(iris, name="prob")
123
sepallengthcm
Decimal(5,2)
100%
123
sepalwidthcm
Decimal(5,2)
100%
123
petallengthcm
Decimal(5,2)
100%
123
petalwidthcm
Decimal(5,2)
100%
Abc
species
Varchar(30)
100%
Abc
prediction
Varchar(15)
100%
123
prob_irissetosa
Decimal(17,12)
100%
123
prob_irisversicolor
Decimal(17,12)
100%
123
prob_irisvirginica
Decimal(17,12)
100%
15.13.51.40.2Iris-setosaIris-setosa1.01.01.0
24.93.01.40.2Iris-setosaIris-setosa1.01.01.0
34.73.21.30.2Iris-setosaIris-setosa1.01.01.0
44.63.11.50.2Iris-setosaIris-setosa1.01.01.0
55.03.61.40.2Iris-setosaIris-setosa1.01.01.0
65.43.91.70.4Iris-setosaIris-setosa1.01.01.0
74.63.41.40.3Iris-setosaIris-setosa1.01.01.0
85.03.41.50.2Iris-setosaIris-setosa1.01.01.0
94.42.91.40.2Iris-setosaIris-setosa1.01.01.0
104.93.11.50.1Iris-setosaIris-setosa1.01.01.0
115.43.71.50.2Iris-setosaIris-setosa1.01.01.0
124.83.41.60.2Iris-setosaIris-setosa1.01.01.0
134.83.01.40.1Iris-setosaIris-setosa1.01.01.0
144.33.01.10.1Iris-setosaIris-setosa1.01.01.0
155.84.01.20.2Iris-setosaIris-setosa0.60.60.6
165.74.41.50.4Iris-setosaIris-setosa0.80.80.8
175.43.91.30.4Iris-setosaIris-setosa1.01.01.0
185.13.51.40.3Iris-setosaIris-setosa1.01.01.0
195.73.81.70.3Iris-setosaIris-setosa0.80.80.8
205.13.81.50.3Iris-setosaIris-setosa1.01.01.0

Visualize Results

ROC Curve:

model.roc_curve()

Confusion Matrix:

model.confusion_matrix()

Feature Importance:

model.features_importance()

Understanding Metrics

The Accuracy Trap

Accuracy alone can be misleading, especially with imbalanced datasets.

Example: Fraud Detection

Suppose fraudulent transactions represent only 1% of data:

# Naive model: predict "no fraud" for everything
# Accuracy: 99% ✓
# Usefulness: 0% ✗ (misses all fraud!)

Better metrics for imbalanced data:

  • ROC AUC – Measures discrimination ability across all thresholds

  • PRC AUC – Precision-Recall curve (better for rare events)

  • F1-Score – Balances precision and recall

  • Class-specific metrics – Precision/recall per class

When to use which metric:

Metric

Best For

Accuracy

Balanced datasets with equal class importance

Precision

When false positives are costly (e.g., spam detection)

Recall

When false negatives are costly (e.g., disease screening)

F1-Score

Balance between precision and recall

ROC AUC

Overall model discrimination ability

PRC AUC

Imbalanced datasets with rare positive class


Train/Test Split

The example above used the entire dataset for training. For real-world applications, always split data:

from vastorbit.machine_learning.model_selection import train_test_split

# Split data: 80% train, 20% test
train, test = iris.train_test_split(test_size=0.2)

# Train on training set
model.fit(
    train,
    X=["PetalLengthCm", "SepalLengthCm"],
    y="Species",
)

# Evaluate on test set
predictions = model.predict(test, name="prediction")
model.report()

Warning

Training and testing on the same data leads to overfitting and unrealistic performance metrics.


Advanced Techniques

Cross-validation:

from vastorbit.machine_learning.model_selection import cross_validate

# 5-fold cross-validation
scores = cross_validate(
    model,
    iris,
    X=["PetalLengthCm", "SepalLengthCm"],
    y="Species",
    cv=5,
)

Hyperparameter tuning:

# Grid search for best parameters
best_model = RandomForestClassifier(
    n_estimators = 5,
    max_depth = 3,
)

Feature engineering:

# Create interaction features
iris["petal_sepal_ratio"] = iris["PetalLengthCm"] / iris["SepalLengthCm"]

# Train with new feature
model.fit(
    iris,
    X=["PetalLengthCm", "SepalLengthCm", "petal_sepal_ratio"],
    y="Species",
)

Available Classifiers

VAST Orbit supports multiple classification algorithms:

  • RandomForestClassifier – Ensemble of decision trees

  • LogisticRegression – Linear classification

  • NaiveBayes – Probabilistic classifier

  • LinearSVC – Support Vector Classifier

  • KNeighborsClassifier – K-nearest neighbors

Each algorithm has strengths for different data types and problem characteristics.


Next Steps

Now that you understand classification, explore:

See also