Classification with Snowflake ML
What Is Classification?
Classification predicts a categorical label for each row of data. Snowflake ML supports both binary classification (two outcomes) and multi-class classification (three or more outcomes). The function automatically selects and tunes an ensemble of models during training.
Training Data Requirements
The training dataset must include:
- A target column containing the label to predict (e.g.,
CHURNED,FRAUD_FLAG,CATEGORY). - One or more feature columns that the model uses to learn patterns.
- Rows with NULL values in the target column are excluded from training.
| Requirement | Detail |
|---|---|
| Minimum rows | At least 100 recommended; more data improves accuracy |
| Target column type | VARCHAR, BOOLEAN, or NUMBER (treated as categorical) |
| Feature types | NUMBER, VARCHAR, BOOLEAN, DATE, TIMESTAMP |
| NULL handling | NULLs in features are handled automatically |
Creating a Classification Model
-- Prepare training data
CREATE OR REPLACE VIEW training_data AS
SELECT
customer_id,
tenure_months,
monthly_charges,
total_charges,
contract_type,
payment_method,
churned -- target: TRUE or FALSE
FROM customer_features
WHERE churned IS NOT NULL;
-- Train the classification model
CREATE OR REPLACE SNOWFLAKE.ML.CLASSIFICATION churn_model(
INPUT_DATA => SYSTEM$REFERENCE('VIEW', 'TRAINING_DATA'),
TARGET_COLNAME => 'CHURNED'
);
Training time depends on dataset size and warehouse size. A medium warehouse handles most datasets under a few million rows within minutes.
Generating Predictions
-- Predict on new data
SELECT
customer_id,
churn_model!PREDICT(
INPUT_DATA => OBJECT_CONSTRUCT(
'TENURE_MONTHS', tenure_months,
'MONTHLY_CHARGES', monthly_charges,
'TOTAL_CHARGES', total_charges,
'CONTRACT_TYPE', contract_type,
'PAYMENT_METHOD', payment_method
)
) AS prediction
FROM customers_to_score;
The output includes the predicted class and a probability for each class:
{
"class": "TRUE",
"probability": { "TRUE": 0.82, "FALSE": 0.18 }
}
Extracting Prediction Components
SELECT
customer_id,
prediction['class']::VARCHAR AS predicted_churn,
prediction['probability']['TRUE']::FLOAT AS churn_probability
FROM (
SELECT
customer_id,
churn_model!PREDICT(
INPUT_DATA => OBJECT_CONSTRUCT(*)
) AS prediction
FROM customers_to_score
);
Evaluating Model Performance
After generating predictions on a holdout set, compute standard metrics:
-- Build a confusion matrix
SELECT
actual_label,
predicted_label,
COUNT(*) AS cnt
FROM evaluation_results
GROUP BY actual_label, predicted_label
ORDER BY actual_label, predicted_label;
| Metric | Formula | Meaning |
|---|---|---|
| Accuracy | (TP + TN) / Total | Overall correctness |
| Precision | TP / (TP + FP) | Of predicted positives, how many are correct |
| Recall | TP / (TP + FN) | Of actual positives, how many were found |
| F1 Score | 2 * (Precision * Recall) / (Precision + Recall) | Harmonic mean of precision and recall |
Practical Example: Fraud Detection
-- Train a fraud detection model
CREATE OR REPLACE SNOWFLAKE.ML.CLASSIFICATION fraud_model(
INPUT_DATA => SYSTEM$REFERENCE('TABLE', 'TRANSACTION_TRAINING'),
TARGET_COLNAME => 'IS_FRAUD'
);
-- Score live transactions
CREATE OR REPLACE TABLE scored_transactions AS
SELECT
transaction_id,
amount,
merchant_category,
prediction['class']::VARCHAR AS fraud_prediction,
prediction['probability']['TRUE']::FLOAT AS fraud_score
FROM (
SELECT
transaction_id,
amount,
merchant_category,
fraud_model!PREDICT(
INPUT_DATA => OBJECT_CONSTRUCT(
'AMOUNT', amount,
'MERCHANT_CATEGORY', merchant_category,
'TRANSACTION_HOUR', transaction_hour,
'COUNTRY_CODE', country_code,
'CARD_TYPE', card_type
)
) AS prediction
FROM pending_transactions
);
-- Flag high-risk transactions for review
SELECT * FROM scored_transactions WHERE fraud_score > 0.7;
Best Practices
- Balance your training data — If one class dominates (e.g., 99% non-fraud), the model may underperform on the minority class. Consider stratified sampling.
- Use a holdout set — Reserve 20-30% of labeled data for evaluation rather than training.
- Retrain periodically — Customer behavior and fraud patterns shift over time. Schedule retraining monthly or quarterly.
- Threshold tuning — The default decision boundary is 0.5. Adjust based on the cost of false positives vs false negatives.
- Feature selection — Exclude columns that leak the target (e.g., a
cancellation_datecolumn when predicting churn).