Snowpark ML: Python APIs for Snowflake ML¶
Snowpark ML (the snowflake-ml-python
Python package) provides Python APIs that connect to the various Snowflake ML
workflow components and also includes APIs for building and training your own models. You can use Snowpark ML in your
favorite Python IDE on your own workstation, in Snowsight worksheets, or in Snowflake notebooks.
Tip
See Introduction to Machine Learning with Snowpark ML for an example of an end-to-end workflow in Snowpark ML.
Using Snowpark ML in Snowflake Notebooks¶
Snowflake Notebooks provide an easy-to-use notebook interface for your data
work, blending Python, SQL, and Markdown. To use Snowpark ML in notebooks, choose the Anaconda package
snowflake-ml-python
using the Packages menu at the top of the notebook.
Notebooks support both CPU and GPU runtime options. Many kinds of models require, or benefit from, having a GPU available.
Important
The snowflake-ml-python
package and its dependencies must be allowed by your organization’s
package policy.
Using Snowpark ML in Snowsight Worksheets¶
Snowsight Worksheets provide a powerful and versatile method for running
Python code. To use Snowpark ML in worksheets, choose the Anaconda package snowflake-ml-python
using the Packages menu
at the top of the worksheet.
Important
The snowflake-ml-python
package and its dependencies must be allowed by your organization’s
package policy.
Using Snowpark ML Locally¶
You must install Snowpark ML to develop on your own workstation or elsewhere outside Snowflake. All Snowpark ML features
are available in a single package, snowflake-ml-python
. You can install Snowpark ML from the Snowflake conda
channel using the conda
command or from the Python Package Index (PyPI) using pip
. Conda is preferred.
Installing Snowpark ML from the Snowflake conda Channel¶
Important
Installing Snowpark ML from conda on an arm-based Mac (with M1 or M2 chip) requires specifying the system architecture when
creating the conda environment. To do this, include CONDA_SUBDIR=osx-arm64
in the conda create
command:
CONDA_SUBDIR=osx-arm64 conda create --name snowpark-ml
.
Create the conda environment where you will install Snowpark ML. If you prefer to use an existing environment, skip this step.
conda create --name snowpark-ml
Activate the conda environment:
conda activate snowpark-ml
Install Snowpark ML from the Snowflake conda channel:
conda install --override-channels --channel https://repo.anaconda.com/pkgs/snowflake/ snowflake-ml-python
Tip
Install packages from the Snowflake conda channel whenever possible to ensure that you receive packages that have been validated with Snowpark ML.
Installing Snowpark ML from PyPI¶
You can install the Snowpark ML package from the Python Package Index (PyPI) by using the standard Python package manager,
pip
.
Warning
Do not use this installation procedure if you are using a conda environment. Use the conda instructions instead.
Change to your project directory and activate your Python virtual environment:
cd ~/projects/ml source .venv/bin/activate
Install the Snowpark ML package:
python -m pip install snowflake-ml-python
Installing Optional Modeling Dependencies¶
Some Snowpark ML Modeling APIs require dependencies that are not installed as dependencies of Snowpark ML. The
scikit-learn and xgboost packages are installed by default when you install Snowpark ML, but lightgbm is an
optional dependency. If you plan to use classes in the snowflake.ml.modeling.lightgbm
namespace, install
lightgbm yourself.
Use the following commands to activate your conda environment and install lightgbm from the Snowflake conda channel.
conda activate snowpark-ml
conda install --override-channels --channel https://repo.anaconda.com/pkgs/snowflake/ lightgbm
Use the following commands to activate your virtual environment and install lightgbm using pip
.
.venv/bin/activate
python -m pip install 'snowflake-ml-python[lightgbm]'
Snowflake might add additional optional dependencies to Snowpark ML from time to time. To install all optional dependencies using pip:
.venv/bin/activate
python -m pip install 'snowflake-ml-python[all]'
Setting Up Snowpark Python¶
Snowpark Python is a dependency of Snowpark ML and is installed automatically with Snowpark ML. If Snowpark Python is not already set up on your system, you might need to perform additional configuration steps. See Setting Up Your Development Environment for Snowpark Python for Snowpark Python setup instructions.
Connecting to Snowflake¶
Snowpark ML requires that you connect to Snowflake using a Snowpark Session
object. Use the
SnowflakeLoginOptions
function in the snowflake.ml.utils.connection_params
module to get the
configuration settings to create the session. The function can read the connection settings from a named connection in
your SnowSQL configuration file or from environment variables that you set. It
returns a dictionary containing these parameters, which can be used to create a connection.
The following examples read the connection parameters from the named connection myaccount
in the SnowSQL
configuration file. To create a Snowpark Python session, create a builder for the Session
class, and pass the
connection information to the builder’s configs
method:
from snowflake.snowpark import Session
from snowflake.ml.utils import connection_params
params = connection_params.SnowflakeLoginOptions("myaccount")
sp_session = Session.builder.configs(params).create()
You can now pass the session to any Snowpark ML function that needs it.
Tip
To create a Snowpark Python session from a Snowflake Connector for Python connection, pass the connection object to
the session builder. Here, connection
is the Snowflake Connector for Python connection.
session = Session.builder.configs({"connection": connection}).create()
Specifying a Warehouse¶
Many parts of Snowpark ML, for example training a model or running inference, run code in a Snowflake warehouse. These
operations run in the warehouse specified by the session you use to connect. For example, if you create a session from a
named connection in your SnowSQL configuration file, you can specify a warehouse
using the warehousename
parameter in the named configuration.
You can add the warehouse setting when creating the Session
object, as shown here, if it does not already
exist in the configuration.
from snowflake.snowpark import Session
from snowflake.ml.utils import connection_params
# Get named connection from SnowQSL configuration file
params = connection_params.SnowflakeLoginOptions("myaccount")
# Add warehouse name for model method calls if it's not already present
if "warehouse" not in params:
params["warehouse"] = "mlwarehouse"
sp_session = Session.builder.configs(params).create()
If no warehouse is specified in the session, or if you want to use a different warehouse, call the session’s
use_warehouse
method to specify a warehouse.
sp_session.use_warehouse("mlwarehouse")
API Reference¶
The Snowpark ML API reference includes documentation on
all publicly-released functionality. You can also obtain detailed API documentation for any API by using Python’s
help
function in an interactive Python session. For example:
from snowflake.ml.modeling.preprocessing import OneHotEncoder
help(OneHotEncoder)