Bird
Raised Fist0
Snowflakecloud~5 mins

ML model training in Snowflake - Commands & Configuration

Choose your learning style10 modes available
Introduction
Training machine learning models inside Snowflake lets you use your data directly without moving it. This saves time and keeps data safe while building models.
When you want to build a prediction model using data stored in Snowflake without exporting it.
When you need to quickly test a simple machine learning model on your data inside Snowflake.
When you want to automate model training as part of your data pipeline in Snowflake.
When you want to keep your data secure by not moving it outside Snowflake during model training.
When you want to use SQL commands to create and train machine learning models.
Commands
This command creates and trains a linear regression model named 'my_model' using the features and target column from the 'training_data' table.
Terminal
CREATE OR REPLACE MODEL my_model
  OPTIONS(
    model_type = 'linear_regression',
    input_label_cols = ('target')
  ) AS
  SELECT feature1, feature2, target
  FROM training_data;
Expected OutputExpected
Model MY_MODEL created successfully.
→
model_type - Specifies the type of machine learning model to train.
→
input_label_cols - Defines the target column(s) the model will predict.
This command lists all the machine learning models currently stored in your Snowflake account.
Terminal
SHOW MODELS;
Expected OutputExpected
name | database | schema | created_on MY_MODEL | MY_DB | PUBLIC | 2024-06-01 12:00:00
This command uses the trained model 'my_model' to predict the target value for a new data point with feature1=5.1 and feature2=3.2.
Terminal
SELECT MODEL_PREDICT('my_model', OBJECT_CONSTRUCT('feature1', 5.1, 'feature2', 3.2)) AS prediction;
Expected OutputExpected
prediction 42.7
Key Concept

If you remember nothing else from this pattern, remember: Snowflake lets you train and use machine learning models directly with SQL on your data without moving it.

Common Mistakes
Not specifying the correct target column in input_label_cols option.
The model won't know which column to predict, causing training to fail or produce wrong results.
Always set input_label_cols to the exact name of the target column in your training data.
Using unsupported model types or misspelling model_type.
Snowflake will reject the command because it only supports certain model types like 'linear_regression' or 'logistic_regression'.
Check Snowflake documentation for supported model types and use exact spelling.
Passing feature values incorrectly in MODEL_PREDICT function.
MODEL_PREDICT expects an OBJECT_CONSTRUCT with feature names and values; wrong format causes errors.
Use OBJECT_CONSTRUCT with feature names as keys and their values as shown in the example.
Summary
Use CREATE MODEL with SQL SELECT to train a machine learning model inside Snowflake.
Use SHOW MODELS to list your trained models.
Use MODEL_PREDICT with OBJECT_CONSTRUCT to make predictions with your model.

Practice

(1/5)
1. What is the main purpose of the CREATE MODEL statement in Snowflake's ML feature?
easy
A. To delete an existing machine learning model
B. To export model results to an external file
C. To make predictions using a trained model
D. To define and train a machine learning model using SQL

Solution

  1. Step 1: Understand the role of CREATE MODEL

    This statement is used to define the model type, label column, and features, which means it sets up and trains the model.
  2. Step 2: Differentiate from other commands

    PREDICT is used for predictions, not CREATE MODEL. Deleting or exporting models uses other commands.
  3. Final Answer:

    To define and train a machine learning model using SQL -> Option D
  4. Quick Check:

    CREATE MODEL = train model [OK]
Hint: Remember: CREATE MODEL means build and train [OK]
Common Mistakes:
  • Confusing CREATE MODEL with PREDICT
  • Thinking CREATE MODEL deletes models
  • Assuming CREATE MODEL exports data
2. Which of the following is the correct syntax to create a logistic regression model named my_model in Snowflake?
easy
A. CREATE MODEL my_model AS logistic_regression WITH LABEL target AND FEATURES age, income;
B. CREATE MODEL my_model TYPE = 'logistic_regression' LABEL = target FEATURES = (age, income);
C. CREATE MODEL my_model USING logistic_regression LABEL target FEATURES (age, income);
D. CREATE MODEL my_model TYPE logistic_regression LABEL target FEATURES (age, income);

Solution

  1. Step 1: Recall Snowflake's CREATE MODEL syntax

    The correct syntax uses TYPE = 'model_type', LABEL = label_column, and FEATURES = (feature_list) with equal signs and parentheses.
  2. Step 2: Compare options

    CREATE MODEL my_model TYPE = 'logistic_regression' LABEL = target FEATURES = (age, income); matches the correct syntax exactly. Others have wrong keywords or missing equal signs or parentheses.
  3. Final Answer:

    CREATE MODEL my_model TYPE = 'logistic_regression' LABEL = target FEATURES = (age, income); -> Option B
  4. Quick Check:

    Correct syntax uses TYPE =, LABEL =, FEATURES = ( ) [OK]
Hint: Look for equal signs and parentheses in syntax [OK]
Common Mistakes:
  • Omitting equal signs (=) in parameters
  • Missing parentheses around features
  • Using wrong keywords like AS or WITH
3. Given the following Snowflake SQL code:
CREATE MODEL churn_model TYPE = 'random_forest' LABEL = churn FEATURES = (age, balance, transactions);
SELECT * FROM PREDICT(churn_model, (SELECT age, balance, transactions FROM customers_test));

What will be the output of the PREDICT statement?
medium
A. The original customers_test table without predictions
B. An error because PREDICT cannot use SELECT statements
C. A table with predicted churn values for each row in customers_test
D. A summary report of model accuracy

Solution

  1. Step 1: Understand the PREDICT statement

    PREDICT applies the trained model to new data and returns predicted values for each input row.
  2. Step 2: Analyze the code

    The SELECT statement provides features from customers_test. PREDICT will output predictions for each row based on these features.
  3. Final Answer:

    A table with predicted churn values for each row in customers_test -> Option C
  4. Quick Check:

    PREDICT outputs predictions per input row [OK]
Hint: PREDICT returns predictions for each input row [OK]
Common Mistakes:
  • Thinking PREDICT returns original data only
  • Assuming PREDICT gives accuracy metrics
  • Believing PREDICT cannot use SELECT
4. You run this Snowflake command:
CREATE MODEL sales_model TYPE = 'linear_regression' LABEL = sales_amount FEATURES = (store_size, location);

But get an error saying "Invalid feature column: location". What is the most likely cause?
medium
A. The column 'location' is not numeric or not suitable as a feature
B. The LABEL column 'sales_amount' does not exist
C. The model type 'linear_regression' is not supported
D. The FEATURES list must be empty for linear regression

Solution

  1. Step 1: Understand feature requirements

    Features must be numeric or convertible to numeric for linear regression models.
  2. Step 2: Analyze the error message

    "Invalid feature column" suggests 'location' is likely categorical or text, which linear regression cannot use directly.
  3. Final Answer:

    The column 'location' is not numeric or not suitable as a feature -> Option A
  4. Quick Check:

    Features must be numeric for linear regression [OK]
Hint: Check feature columns are numeric for regression models [OK]
Common Mistakes:
  • Assuming LABEL column causes this error
  • Thinking model type is unsupported
  • Believing features list can be empty
5. You want to train a classification model in Snowflake to predict customer churn. You have a dataset with columns: customer_id, age, monthly_spend, churned (1 if churned, 0 otherwise), and signup_date. Which CREATE MODEL statement correctly trains a model ignoring customer_id and signup_date?
hard
A. CREATE MODEL churn_model TYPE = 'logistic_regression' LABEL = churned FEATURES = (age, monthly_spend);
B. CREATE MODEL churn_model TYPE = 'logistic_regression' LABEL = customer_id FEATURES = (age, monthly_spend, churned);
C. CREATE MODEL churn_model TYPE = 'logistic_regression' LABEL = churned FEATURES = (customer_id, age, monthly_spend, signup_date);
D. CREATE MODEL churn_model TYPE = 'linear_regression' LABEL = churned FEATURES = (age, monthly_spend);

Solution

  1. Step 1: Identify correct label and features

    The label is churned because it indicates churn. Features should exclude customer_id and signup_date as they are identifiers or dates not useful for prediction.
  2. Step 2: Choose appropriate model type

    Since churn is a binary outcome, logistic regression is suitable, not linear regression.
  3. Final Answer:

    CREATE MODEL churn_model TYPE = 'logistic_regression' LABEL = churned FEATURES = (age, monthly_spend); -> Option A
  4. Quick Check:

    Label = churned, features exclude IDs/dates, logistic regression for classification [OK]
Hint: Use label as target, exclude IDs/dates from features [OK]
Common Mistakes:
  • Using ID or date columns as features
  • Choosing wrong label column
  • Using linear regression for classification
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard