0
0
Snowflakecloud~10 mins

ML model training in Snowflake - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - ML model training in Snowflake
Prepare Data
Create Model
Train Model
Evaluate Model
Use Model for Prediction
This flow shows the steps to train a machine learning model in Snowflake: prepare data, create the model, train it, evaluate, and then use it for predictions.
Execution Sample
Snowflake
CREATE OR REPLACE MODEL my_model
  OPTIONS (model_type='linear_regression') AS
SELECT feature1, feature2, target FROM training_data;
This code creates and trains a linear regression model named 'my_model' using features and target from 'training_data' table.
Process Table
StepActionSQL CommandResult
1Prepare DataSELECT feature1, feature2, target FROM training_data;Data selected for training
2Create and Train ModelCREATE OR REPLACE MODEL my_model OPTIONS (model_type='linear_regression') AS SELECT feature1, feature2, target FROM training_data;Model 'my_model' created and trained
3Evaluate ModelSELECT * FROM TABLE(EVALUATE_MODEL('my_model', 'training_data'));Evaluation metrics returned
4Use Model for PredictionSELECT feature1, feature2, PREDICT('my_model', feature1, feature2) AS prediction FROM new_data;Predictions generated for new data
💡 All steps completed successfully; model trained and predictions ready
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
training_datatable with raw datadata selected with features and targetused to train modelused to evaluate modelunchanged
my_modelnot creatednot createdcreated and trainedevaluatedused for prediction
predictionsnonenonenonenonegenerated for new_data
Key Moments - 3 Insights
Why do we use SELECT inside CREATE MODEL?
The SELECT statement defines the data used to train the model. It tells Snowflake which features and target to use, as shown in execution_table step 2.
What happens if the model already exists?
Using CREATE OR REPLACE MODEL replaces the existing model with the new one, ensuring the latest training data and parameters are applied, as in step 2.
How do we check if the model is good?
We run EVALUATE_MODEL function (step 3) which returns metrics like accuracy or error to understand model performance.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 2?
AModel 'my_model' created and trained
BData selected for training
CPredictions generated for new data
DEvaluation metrics returned
💡 Hint
Check the 'Result' column in execution_table row with Step 2
At which step do we generate predictions for new data?
AStep 1
BStep 3
CStep 4
DStep 2
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for prediction generation
If we change the model type to 'decision_tree', which step changes?
AStep 1 - Prepare Data
BStep 2 - Create and Train Model
CStep 3 - Evaluate Model
DStep 4 - Use Model for Prediction
💡 Hint
Model type is specified in the CREATE MODEL command in Step 2
Concept Snapshot
ML Model Training in Snowflake:
- Use CREATE MODEL with SELECT to define training data
- Specify model_type in OPTIONS
- Evaluate model with EVALUATE_MODEL function
- Use PREDICT function for new data
- CREATE OR REPLACE MODEL updates existing model
Full Transcript
This visual execution shows how to train a machine learning model in Snowflake. First, data is prepared by selecting features and target columns. Then, the CREATE OR REPLACE MODEL command creates and trains the model using that data. After training, the model is evaluated using the EVALUATE_MODEL function to get performance metrics. Finally, the trained model is used to generate predictions on new data with the PREDICT function. Variables like training_data and my_model change state through these steps, reflecting data selection, model creation, evaluation, and prediction. Key points include understanding the SELECT inside CREATE MODEL, replacing existing models, and checking model quality with evaluation. The quiz tests understanding of these steps and their results.