Create, train and use a Snowflake ML model in an app¶
This topic provides an example of how to train a Snowflake ML model within a Snowflake Native App
using the scikit-learn Python package. The example in this topic can be used to
train models on data in the consumer or provider accounts.
Create a versioned schema to hold the stored procedures¶
Within the setup script create a versioned schema that contains the stored procedure as shown in the following example:
Create a versioned schema for the stored procedure
Create a stored procedure to create and train a model¶
Create a stored procedure for the Python function you are using to train a model as shown in the following example:
This example creates a stored procedure named
py_log_modeland declares the Python packages required to train a model usingscikit-learn:
snowflake-snowpark-python
scikit-learn
snowflake-ml-python
pandas
numpy
xgboost
After creating a stored procedure, add the following code to the body of the stored procedure:
Add Python code to the body of the stored procedure
The
log_modelfunction performs the following:
Uses
pandasandnumpyto create a DataFrame to serve a the training data for the model.Creates an instance of the XGBoost to serve as the training algorithm for the data.
Calls the
fit()function of XGBoost to create a model and train it on the dataset.Calls the
log_model()function of Snowflake Model Registry to add the model to the model registry.Note
Models created by an app must be stored in a model registry. Apps cannot access models that are stored on a stage.
Optional: To allow consumers to run the stored procedure to train the model, grant the USAGE privilege on the stored procedure:
Create a stored procedure to run a model¶
Create a stored procedure for the Python function you use to call the model.
Add the Python code you use to call the model
The
run_modelfunction does the following:
Runs the
load_iris()function to load the iris machine learning dataset (https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_iris.html).Uses
pandasto create a DataFrame based on the iris data set.Runs the
get_model()function to get the model registry.Runs the predict function on the model.
Returns the result.
Optional: To allow consumers to run the stored procedure to train the model, grant the USAGE privilege on the stored procedure:
Run the stored procedures¶
If the app grants the USAGE privilege on these stored procedures to an application role, consumers can call the stored procedures to train and run the models as shown in the following examples:
This command calls the py_log_model stored procedure to train the model.
This command calls the py_call_predict stored procedure to call the predict function on the model.