Model Training, Evaluation & Inference
Guide for Model Training, Evaluation & Inference in Snowflake (with Snowpark), focusing on:
- ✅ Model training (in or outside Snowflake)
- ✅ Model evaluation
- ✅ Model inference (deployment and scoring)
🧠 Model Training, Evaluation & Inference in Snowflake
📌 Overview
Snowflake’s Snowpark framework allows you to perform model training, evaluation, and inference for machine learning tasks inside or outside Snowflake.
- Training & Evaluation: Usually done outside Snowflake (e.g., in Python, Jupyter, or a CI/CD pipeline)
- Inference: Can be done inside Snowflake using Snowpark stored procedures and UDFs
1️⃣ Model Training
🚀 Typical Process (Outside Snowflake)
Train your ML model using popular Python libraries:
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
# Load data from Snowflake (example using Snowflake Connector or Snowpark)
# Here, we assume you've already fetched a pandas DataFrame `df`
X = df[["feature1", "feature2"]]
y = df["label"]
# Train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train model
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)
2️⃣ Model Evaluation
Evaluate your model’s performance on test data:
from sklearn.metrics import accuracy_score, classification_report
# Make predictions
y_pred = model.predict(X_test)
# Evaluate
accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred)
print(f"Accuracy: {accuracy}")
print(report)
3️⃣ Model Serialization & Storage
Save the trained model to a file for deployment:
import joblib
joblib.dump(model, "rf_model.pkl")
Then upload it to a Snowflake stage:
PUT file://rf_model.pkl @my_stage;
4️⃣ Model Inference (Scoring in Snowflake)
Use Snowpark to load data and apply your trained model.
✅ Example Snowpark Inference Script
import pandas as pd
import joblib
from snowflake.snowpark import Session
def score_model(session: Session) -> str:
# Load data from a Snowflake table
df = session.table("CUSTOMER_DATA").to_pandas()
# Load trained model from stage
session.file.get("@my_stage/rf_model.pkl", "rf_model.pkl")
model = joblib.load("rf_model.pkl")
# Make predictions
features = df[["feature1", "feature2"]]
predictions = model.predict(features)
df["PREDICTION"] = predictions
# Save predictions back to Snowflake
session.create_dataframe(df).write.mode("overwrite").save_as_table("PREDICTIONS")
return "Inference complete. Predictions stored in PREDICTIONS table."
# Local test run
if __name__ == "__main__":
from snowflake.snowpark import Session
connection_parameters = {
"account": "<account>",
"user": "<user>",
"password": "<password>",
"warehouse": "<warehouse>",
"database": "<database>",
"schema": "<schema>",
"role": "<role>"
}
session = Session.builder.configs(connection_parameters).create()
print(score_model(session))
5️⃣ Evaluation & Monitoring in Production
- ✅ You can add performance tracking by comparing model predictions with actual data (if available).
- ✅ Save evaluation metrics to a monitoring table in Snowflake:
CREATE TABLE model_metrics (
model_version STRING,
run_date TIMESTAMP,
accuracy FLOAT,
notes STRING
);
Then log the metrics:
monitor_df = pd.DataFrame({
"model_version": ["v1.0"],
"run_date": [pd.Timestamp.now()],
"accuracy": [accuracy],
"notes": ["Initial deployment"]
})
session.create_dataframe(monitor_df).write.mode("append").save_as_table("MODEL_METRICS")
6️⃣ Summary
| Step | Tool/Location | Notes |
|---|---|---|
| Model training | Outside Snowflake (Jupyter, Python) | Use sklearn, xgboost, etc. |
| Model evaluation | Outside Snowflake | Evaluate with test data and metrics |
| Model storage | Snowflake stage | Use PUT to upload .pkl or other serialized formats |
| Inference | Inside Snowflake (Snowpark) | Use Python stored procedures or scripts |
| Monitoring | Snowflake tables | Save metrics for continuous monitoring and drift detection |