CuML + Cudf (RAPIDS) 加速python数据分析脚本
如果有人在用Nvidia RAPIDS加速pandas和sklearn等库,请看我这个小示例,可以节省你大量时间。
1. 创建环境
请使用uv,而非conda/mamba。
# install uv if not yetcurl -LsSf https://astral.sh/uv/install.sh | shuv init data_gpucd data_gpuuv venv --python 3.12source .venv/bin/activate# 大的要来了# 使用阿里云开源镜像uv pip install \-i http://mirrors.aliyun.com/pypi/simple/ \--extra-index-url=https://pypi.nvidia.com \"cudf-cu12==25.4.*" "cuml-cu12==25.4.*" \"polars[pandas,numpy,pyarrow,style,plot,excel,gpu]" \polars-u64-idx scikit-learn scipy statsmodels tqdm ipykernel jupyter --prerelease=allow --index-strategy unsafe-best-match
2. 在脚本中启用
import cudf.pandas
cudf.pandas.install()
from cuml.accel.core import install as cuml_install
cuml_install(disable_uvm=False)
import pandas as pd
import polars as pl
import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
现在,pandas, sklearn, polars都有GPU加持。但是并非所有函数都受支持。具体情况自己去看官方文档。
另外,不要使用 python -m cuml.accel my_script.py
,这样你就无法向my_script.py传自己的参数。