What you will be able to do
- Prepare labeled training data whose feature types will be interpreted the way you intend
- Train a classification model and read the class and probability returned by PREDICT
- Evaluate a classifier with its SHOW_* methods and retrain it, given that models are immutable
- Choose between REGR_ functions, FORECAST and CLASSIFICATION for a prediction question
1.What a classifier learns from: labels, features and their types
Some predictions are categories, not numbers: will this customer churn, is this transaction fraud, which of three actions will a shopper take next. SNOWFLAKE.ML.CLASSIFICATION learns these from rows whose outcome is already known. It handles binary problems (two classes) and multi-class problems (more than two). Like forecasting, it is a model object you train once and then call. The forecasting lesson walks through that train-then-call pattern in detail.
The training data needs one target column holding each row's label, plus at least one feature column. The label needs at least two distinct values and fewer distinct values than there are rows, with a maximum of 255 classes. Features and labels can be numeric, Boolean or string. A TIMESTAMP_NTZ column turns into derived epoch, day, week and month features. Columns of any other type must be cast to one of these first.
A column's type decides how the classifier reads it. Strings and Booleans are categorical. High-cardinality strings such as job titles are supported, but free text such as sentences is not. Numbers are continuous. The documentation's sample data casts user_interest_score to VARCHAR on purpose so that it is treated as categories, and leaves user_rating numeric. You have no other control over how features are interpreted: the gradient boosting algorithm is fixed, and its parameters cannot be set.
Checkpoint 1 of 4· Match them up
Match each training column to how SNOWFLAKE.ML.CLASSIFICATION handles it
Tap a term, then the definition that fits it.
The column's type decides how the classifier reads it, not how many distinct values it has. Numbers are continuous, strings are categorical even at high cardinality, free text is not supported, and TIMESTAMP_NTZ is turned into derived time features.
“Numeric features are treated as continuous. To treat numeric features as categorical, cast them to strings.”Source: docs.snowflake.com
Sources1
2.Training the classifier and reading PREDICT output
Training needs only a reference to the data and the name of the target column. Every other column in the input becomes a feature. Training runs with limited privileges and cannot read your tables directly, so you pass a reference that carries your role's access. The reference can be SYSTEM$REFERENCE for a table or view, or a query reference. The creating role needs the CREATE SNOWFLAKE.ML.CLASSIFICATION privilege on the schema.
CREATE OR REPLACE SNOWFLAKE.ML.CLASSIFICATION model_binary(
INPUT_DATA => SYSTEM$REFERENCE('view', 'binary_classification_view'),
TARGET_COLNAME => 'label'
);PREDICT is called once per row, inside a SELECT. This is different from forecasting, which returns a table. Its INPUT_DATA is an object that maps feature names to values, and {*} builds that object from every column of the row. The column names and types must match the training data. Columns the model never saw are ignored. A category value that wasn't in the training data is not an error.
SELECT model_binary!PREDICT(INPUT_DATA => {*})
AS prediction FROM prediction_purchase_data;Each prediction is an object with three keys:
class: the predicted label, which is whichever class has the highest probabilityprobability: an object with one key per class name and its predicted probability as the valuelogs: any error or warning messages
In the documentation's binary example, one row comes back as class "True" with probability 0.998 for True. Use the probability, not just the class, when a decision needs a confidence cutoff. A churn campaign might only contact customers whose churn probability is high.
Checkpoint 2 of 4· Check yourself
An analyst has trained model_binary and now wants a predicted label for every row in prediction_purchase_data. Which statement produces those predictions?
Classification PREDICT runs once per row inside a SELECT. Its INPUT_DATA is an object of feature names and values, and {*} builds that object from each row. The TABLE(...) and FORECASTING_PERIODS forms belong to forecasting, and a dot is not method syntax. Only the ! form works.
“To run inference (prediction) on a dataset, use the model’s PREDICT method. SELECT <model_name>!PREDICT(...);”Source: docs.snowflake.com
3.Evaluating, retraining and sizing a classifier
Classifier evaluation withholds rows and compares predicted classes with their real labels, in the same spirit as the forecast model's self-scoring. Where it differs is the sampling. By default the held-out set tries to include at least 500 rows of the minority class and is capped at 20% of the data, so a rare outcome such as fraud is still represented in the test. The evaluation_config key test_fraction sets the held-out share yourself, and evaluate => FALSE in CONFIG_OBJECT skips evaluation.
CALL <model_name>!SHOW_EVALUATION_METRICS();
CALL <model_name>!SHOW_GLOBAL_EVALUATION_METRICS();
CALL <model_name>!SHOW_THRESHOLD_METRICS();
CALL <model_name>!SHOW_CONFUSION_MATRIX();When new labeled data arrives, the model cannot be updated in place: you train a replacement, and CREATE OR REPLACE does that in one statement. Plan around one more limit. Classification instances cannot be cloned, and a cloned or replicated database skips them.
Training is the step that uses the most memory, so choose the warehouse by data size. For prototyping, an X-Small standard warehouse keeps costs low. Standard warehouses have a lower memory limit that does not grow with size, so for production-size data use a Medium Snowpark-optimized warehouse. Snowflake tested that size with up to 1,000 columns and 600 million rows. A dedicated warehouse without other workloads gives the best training performance.
Checkpoint 3 of 4· Check yourself
Each month a churn team receives a new batch of customers with known outcomes. They want their classifier, model_churn, to learn from these rows too. What should they do?
Classification models are immutable, so new data means training a replacement model, and CREATE OR REPLACE does that in one statement. Classification instances cannot be cloned, and their parameters cannot be set.
“Models are immutable and cannot be updated in place. To update a model, drop the existing model and train a new one.”Source: docs.snowflake.com
4.REGR_, FORECAST or CLASSIFICATION: matching the tool to the question
The general guide to picking an ML function is in Choosing the right ML function for a question. For prediction questions, two things decide it: the shape of your data and how much accuracy you need. If the answer is a category, the only option is a classifier. If the answer is a number at a future time, the choice is between a one-line regression and a trained forecast.
| Question shape | Tool | When it wins |
|---|---|---|
| Numeric y against a numeric x; a rough trend rate is enough | REGR_SLOPE, REGR_INTERCEPT, REGR_R2 | Runs in a single aggregate query with no model object, and R² shows quickly whether a straight line is adequate |
| Numeric metric over regular timestamps, one or many series | SNOWFLAKE.ML.FORECAST | You need future values with a prediction interval, seasonality, per-series models, exogenous features or cross-validated error metrics |
| Labeled rows; the answer is one of two or more classes | SNOWFLAKE.ML.CLASSIFICATION | Churn, fraud or spam-style questions where each row needs a class plus class probabilities |
A low REGR_R2 is the usual signal to move from a straight line to FORECAST, because the line is explaining little of the variation. The reverse also holds. If REGR_R2 is close to 1 and nobody needs an uncertainty range, a trained model adds privileges, compute and retraining work without adding information.
Checkpoint 4 of 4· Match them up
Three requests come to the analytics team. Match each one to the prediction tool that fits it best.
Tap a term, then the definition that fits it.
If the answer is a category, you need a classifier. A future number with intervals or per-series models calls for FORECAST. A trend that a straight line already fits well (R² near 1), with no range needed, gains nothing from a trained model, so the REGR_ functions are enough.
“Forecasting uses a machine learning algorithm that predicts future numeric data from historical time series data.”Source: docs.snowflake.com
Practise it for real
Train a binary classifier on generated purchase data, score unlabeled rows, and inspect its evaluation results.
1.Create the training_purchase_data and prediction_purchase_data tables with the GENERATOR-based CREATE TABLE statements from the classification documentation, then create binary_classification_view selecting user_interest_score, user_rating and label.
Why: The view leaves out the multi-class column, so label is the only target and the other two columns are the features.
You should see: A sample SELECT on the view shows string interest scores, numeric ratings and True/False labels.
2.Run CREATE OR REPLACE SNOWFLAKE.ML.CLASSIFICATION model_binary with INPUT_DATA => SYSTEM$REFERENCE('view', 'binary_classification_view') and TARGET_COLNAME => 'label'.
Why: The reference passes your role's access to the training process, which otherwise runs with limited privileges.
You should see: The statement succeeds, and SHOW SNOWFLAKE.ML.CLASSIFICATION lists model_binary.
3.Run SELECT *, model_binary!PREDICT(INPUT_DATA => {*}) AS predictions FROM prediction_purchase_data.
Why: {*} builds the feature object from each row's columns, and selecting * keeps the inputs next to their prediction.
You should see: Each row has an object with class, logs and probability. High interest and rating values come back as "True" with a probability near 1.
4.Run CALL model_binary!SHOW_CONFUSION_MATRIX(); and CALL model_binary!SHOW_FEATURE_IMPORTANCE();
Why: These show how the held-out predictions compared with the real labels, and which feature the model relied on.
You should see: Evaluation output from the held-out rows, and a ranking of user_interest_score and user_rating.
Stuck? Get a nudge
If PREDICT returns odd results, check that the scoring table's columns have the same names and types as the training view. A user_interest_score left numeric would no longer match the VARCHAR feature the model was trained on.
Sources
(4)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“Classification uses machine learning algorithms to sort data into different classes using patterns detected in training data.”
↩︎ What a classifier learns from: labels, features and their types“include a target column representing the labeled class of each data point and at least one feature column”
↩︎ What a classifier learns from: labels, features and their types“must be greater than one and less than the number of rows in the dataset”
↩︎ What a classifier learns from: labels, features and their types“Your target column must contain no more than 255 distinct classes.”
↩︎ What a classifier learns from: labels, features and their types“It does not support full free text, like sentences or paragraphs.”
↩︎ What a classifier learns from: labels, features and their types“SNOWFLAKE.ML.CLASSIFICATION runs using limited privileges, so by default, it does not have access to your data.”
↩︎ Training the classifier and reading PREDICT output“Inference data must have the same feature names and types as training data.”
↩︎ Training the classifier and reading PREDICT output“In the evaluation process, an additional model is trained on the original data but with some data points withheld.”
↩︎ Evaluating, retraining and sizing a classifier“To minimize costs, we recommend using an X-Small standard warehouse for prototyping.”
↩︎ Evaluating, retraining and sizing a classifier“SNOWFLAKE.ML.CLASSIFICATION instances cannot be cloned.”
↩︎ Evaluating, retraining and sizing a classifier“Common use cases of classification include customer churn prediction, credit card fraud detection, and spam detection.”
↩︎ REGR_, FORECAST or CLASSIFICATION: matching the tool to the question“Numeric features are treated as continuous.”
↩︎ Exam trap 1“Models are immutable and cannot be updated in place.”
↩︎ Exam trap 2“Numeric features are treated as continuous. To treat numeric features as categorical, cast them to strings.”
↩︎ Prediction“To run inference (prediction) on a dataset, use the model’s PREDICT method. SELECT <model_name>!PREDICT(...);”
↩︎ Checkpoint“Models are immutable and cannot be updated in place. To update a model, drop the existing model and train a new one.”
↩︎ Checkpoint - 2.
“For each class, the key is the class name, and the value is the predicted probability of the class.”
↩︎ Training the classifier and reading PREDICT output“The predicted label with the highest probability.”
↩︎ Training the classifier and reading PREDICT output - 3.https://docs.snowflake.com/en/sql-reference/classes/classification/commands/create-classificationOfficial docs
“test_fraction (FLOAT): The fraction of the dataset that should be used as test (evaluation) data.”
↩︎ Evaluating, retraining and sizing a classifier - 4.
“The forecasting function is built to predict any numeric time series data into the future.”
↩︎ REGR_, FORECAST or CLASSIFICATION: matching the tool to the question“Forecasting uses a machine learning algorithm that predicts future numeric data from historical time series data.”
↩︎ Checkpoint