$npx -y skills add langchain-ai/deepagents --skill cuml-machine-learningUse for GPU-accelerated machine learning on tabular data using NVIDIA cuML. Triggers when tasks involve classification, regression, clustering, dimensionality reduction, or model training on datasets.
| 1 | # cuML Machine Learning Skill |
| 2 | |
| 3 | GPU-accelerated machine learning using NVIDIA RAPIDS cuML. cuML provides a scikit-learn-compatible API that runs on NVIDIA GPUs, enabling massive speedups on large datasets. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | Use this skill when: |
| 8 | - Training classification models (predict categories, detect fraud, classify text) |
| 9 | - Training regression models (forecast values, predict prices, estimate quantities) |
| 10 | - Clustering data (segment customers, group documents, find patterns) |
| 11 | - Dimensionality reduction (visualize high-dimensional data, compress features) |
| 12 | - Preprocessing and feature engineering on large datasets |
| 13 | - Any ML task on datasets with 10K+ rows where GPU acceleration helps |
| 14 | |
| 15 | ## Initialization (REQUIRED) |
| 16 | |
| 17 | Always start every script with this boilerplate. It tests actual GPU ML operations. |
| 18 | |
| 19 | ```python |
| 20 | import pandas as pd |
| 21 | import numpy as np |
| 22 | |
| 23 | try: |
| 24 | import cudf |
| 25 | import cuml |
| 26 | # Smoke-test: verify GPU ML works end-to-end |
| 27 | _test_data = cudf.DataFrame({'a': [1.0, 2.0, 3.0, 4.0], 'b': [5.0, 6.0, 7.0, 8.0]}) |
| 28 | _km = cuml.cluster.KMeans(n_clusters=2, n_init=1, random_state=42) |
| 29 | _km.fit(_test_data) |
| 30 | assert len(_km.labels_) == 4 |
| 31 | GPU = True |
| 32 | except Exception as e: |
| 33 | print(f"[GPU] cuml unavailable, falling back to scikit-learn: {e}") |
| 34 | GPU = False |
| 35 | |
| 36 | def read_csv(path): |
| 37 | return cudf.read_csv(path) if GPU else pd.read_csv(path) |
| 38 | |
| 39 | def to_pd(df): |
| 40 | """Convert cuML/cuDF output to pandas. Use this instead of .to_pandas() directly.""" |
| 41 | if not GPU: |
| 42 | return df |
| 43 | try: |
| 44 | return df.to_pandas() |
| 45 | except Exception as e: |
| 46 | print(f"[GPU] .to_pandas() failed, using Arrow fallback: {e}") |
| 47 | return df.to_arrow().to_pandas() |
| 48 | ``` |
| 49 | |
| 50 | ## Import Patterns |
| 51 | |
| 52 | ```python |
| 53 | # GPU mode |
| 54 | if GPU: |
| 55 | from cuml.cluster import KMeans, DBSCAN, HDBSCAN |
| 56 | from cuml.ensemble import RandomForestClassifier, RandomForestRegressor |
| 57 | from cuml.linear_model import LinearRegression, Ridge, Lasso, LogisticRegression |
| 58 | from cuml.neighbors import KNeighborsClassifier, KNeighborsRegressor |
| 59 | from cuml.svm import SVC, SVR |
| 60 | from cuml.decomposition import PCA, TruncatedSVD |
| 61 | from cuml.manifold import UMAP, TSNE |
| 62 | from cuml.preprocessing import StandardScaler, MinMaxScaler, LabelEncoder |
| 63 | from cuml.model_selection import train_test_split |
| 64 | from cuml.metrics import accuracy_score, r2_score, mean_squared_error |
| 65 | # CPU fallback |
| 66 | else: |
| 67 | from sklearn.cluster import KMeans, DBSCAN, HDBSCAN |
| 68 | from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor |
| 69 | from sklearn.linear_model import LinearRegression, Ridge, Lasso, LogisticRegression |
| 70 | from sklearn.neighbors import KNeighborsClassifier, KNeighborsRegressor |
| 71 | from sklearn.svm import SVC, SVR |
| 72 | from sklearn.decomposition import PCA, TruncatedSVD |
| 73 | from sklearn.manifold import TSNE |
| 74 | from sklearn.preprocessing import StandardScaler, MinMaxScaler, LabelEncoder |
| 75 | from sklearn.model_selection import train_test_split |
| 76 | from sklearn.metrics import accuracy_score, r2_score, mean_squared_error |
| 77 | # UMAP not in sklearn — skip or pip install umap-learn |
| 78 | ``` |
| 79 | |
| 80 | ## Quick Reference |
| 81 | |
| 82 | ### Train/Test Split (Start Here) |
| 83 | |
| 84 | ```python |
| 85 | X = df[["feature1", "feature2", "feature3"]].astype("float32") |
| 86 | y = df["target"] |
| 87 | |
| 88 | X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
| 89 | ``` |
| 90 | |
| 91 | ### Classification |
| 92 | |
| 93 | ```python |
| 94 | model = RandomForestClassifier(n_estimators=100, max_depth=10, random_state=42) |
| 95 | model.fit(X_train, y_train) |
| 96 | |
| 97 | predictions = model.predict(X_test) |
| 98 | accuracy = float(accuracy_score(to_pd(y_test), to_pd(predictions))) |
| 99 | print(f"Accuracy: {accuracy:.4f}") |
| 100 | |
| 101 | # Feature importances (tree models only) |
| 102 | importances = to_pd(model.feature_importances_) |
| 103 | for name, imp in zip(feature_names, importances): |
| 104 | print(f" {name}: {imp:.4f}") |
| 105 | ``` |
| 106 | |
| 107 | ### Regression |
| 108 | |
| 109 | ```python |
| 110 | model = Ridge(alpha=1.0) |
| 111 | model.fit(X_train, y_train) |
| 112 | |
| 113 | predictions = model.predict(X_test) |
| 114 | r2 = float(r2_score(to_pd(y_test), to_pd(predictions))) |
| 115 | mse = float(mean_squared_error(to_pd(y_test), to_pd(predictions))) |
| 116 | print(f"R² Score: {r2:.4f}") |
| 117 | print(f"MSE: {mse:.4f}") |
| 118 | |
| 119 | # Coefficients |
| 120 | coeffs = to_pd(model.coef_) |
| 121 | print(f"Intercept: {float(model.intercept_):.4f}") |
| 122 | ``` |
| 123 | |
| 124 | ### Clustering (KMeans) |
| 125 | |
| 126 | ```python |
| 127 | X = df[["feature1", "feature2"]].astype("float32") |
| 128 | |
| 129 | model = KMeans(n_clusters=4, n_init=10, random_state=42) |
| 130 | model.fit(X) |
| 131 | |
| 132 | labels = to_pd(model.labels_) |
| 133 | centroids = to_pd(model.cluster_centers_) |
| 134 | inertia = float(model.inertia_) |
| 135 | |
| 136 | print(f"Inertia: {inertia:.2f}") |
| 137 | print(f"Cluster sizes: {labels.value_counts().sort_index().to_dict()}") |
| 138 | print(f"Centroids:\n{centroids}") |
| 139 | ``` |
| 140 | |
| 141 | ### Dimensionality Reduction (PCA) |
| 142 | |
| 143 | ```python |
| 144 | scaler = StandardScaler() |
| 145 | X_scaled = scaler.fit_transform(X.astype("float32")) |
| 146 | |
| 147 | pca = PCA(n_components=3) |
| 148 | X_ |