适用于 Snowflake ML 的 Python APIs¶
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 these APIs 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 (https://quickstarts.snowflake.com/guide/intro_to_machine_learning_with_snowpark_ml_for_python/#0) for an example of an end-to-end workflow using this library.
在 Snowflake 笔记本中使用 Snowflake ML¶
Snowflake Notebooks provide an easy-to-use notebook interface for your data
work, blending Python, SQL, and Markdown. To use Snowflake ML features in notebooks, choose the Anaconda package
snowflake-ml-python using the Packages menu at the top of the notebook.
笔记本同时支持 CPU 和 GPU 运行时选项。许多类型的模型都需要或受益于可用的 GPU。
Important
The snowflake-ml-python package and its dependencies must be allowed by your organization’s
package policy.
在 Snowsight 工作表中使用 Snowflake ML¶
Snowsight Worksheets provide a powerful and versatile method for running
Python code. To use Snowflake ML features 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.
在本地使用 Snowflake ML¶
You must install the snowflake-ml-python package to develop on your own workstation or elsewhere outside Snowflake.
All Snowflake ML features are available in a single package, snowflake-ml-python. You can install the package
from the Snowflake conda channel using the conda command or from the Python Package Index (PyPI) using
pip. Conda is preferred.
从 Snowflake Conda 通道安装¶
-
创建将安装 Snowflake ML 的 Conda 环境。如果您希望使用现有环境,请跳过此步骤。
-
激活 Conda 环境:
-
Install
snowflake-ml-pythonfrom the Snowflake conda channel
Tip
请尽可能从 Snowflake Conda 通道来安装包,以确保您收到的包已经过 Snowflake ML 验证。
从 PyPI 安装¶
You can install snowflake-ml-python 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.
-
创建并激活 Python 虚拟环境:
-
Install the
snowflake-ml-pythonpackage:
安装可选依赖项
Some APIs require dependencies that are not installed as dependencies of snowflake-ml-python.
By default, scikit-learn is installed. Other packages such as lightgbm, xgboost, keras, pytorch, and others are
optional dependencies.
If you plan to use the snowflake.ml.modeling.lightgbm module, install lightgbm. Use the following
commands to activate your conda environment and install lightgbm from the Snowflake conda channel.
Use the following commands to activate your virtual environment and install lightgbm using pip.
设置 Snowpark Python¶
Snowpark Python is a dependency of snowflake-ml-python and is installed automatically with it. 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.
连接到 Snowflake¶
Before using Snowflake ML features in Python, 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:
现在,您可以将会话传递给任何需要它的内容。
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.
指定仓库
Many Snowflake ML features, for example model training or 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.
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.
API 参考¶
The Snowflake 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: