-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(machine-learning): add scikit-learn and cuML (#22720)
- Loading branch information
1 parent
268aebf
commit c10f203
Showing
15 changed files
with
1,603 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
uv-install-python:: | ||
uv python install | ||
uv-update-lock-file: | ||
uv lock | ||
uv-install-dependencies: | ||
uv sync --dev | ||
|
||
uv-run-dev: | ||
uv run poe dev | ||
uv-run-test: | ||
uv run poe test | ||
uv-run-test-coverage: | ||
uv run poe test-coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[project] | ||
name = "hm-cuml" | ||
version = "1.0.0" | ||
requires-python = "~=3.12.0" | ||
dependencies = [ | ||
"cudf-cu12==24.12.0", | ||
"cuml-cu12==24.12.0", | ||
"scikit-learn==1.6.1", | ||
] | ||
|
||
[dependency-groups] | ||
dev = [ | ||
"poethepoet==0.32.1", | ||
"pytest==8.3.4", | ||
"pytest-cov==6.0.0", | ||
] | ||
|
||
[tool.uv] | ||
package = false | ||
|
||
[tool.poe.tasks] | ||
dev = "python src/main.py" | ||
test = "pytest --verbose --verbose" | ||
test-coverage = "pytest --cov=. --cov-report=xml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class TestDummy: | ||
def test_dummy(self) -> None: | ||
assert 1 + 1 == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import logging | ||
|
||
import cudf | ||
from cuml.ensemble import RandomForestClassifier | ||
from cuml.preprocessing import StandardScaler | ||
from sklearn.datasets import load_iris | ||
from sklearn.metrics import accuracy_score, classification_report | ||
from sklearn.model_selection import train_test_split | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def main() -> None: | ||
# Load the iris dataset | ||
iris = load_iris() | ||
x = iris.data | ||
y = iris.target | ||
|
||
# Split the data | ||
x_train, x_test, y_train, y_test = train_test_split( | ||
x, | ||
y, | ||
test_size=0.2, | ||
random_state=42, | ||
) | ||
|
||
# Convert to cuDF DataFrames | ||
x_train_cudf = cudf.DataFrame(x_train) | ||
x_test_cudf = cudf.DataFrame(x_test) | ||
y_train_cudf = cudf.Series(y_train) | ||
y_test_cudf = cudf.Series(y_test) | ||
|
||
# Scale the features | ||
scaler = StandardScaler() | ||
x_train_scaled = scaler.fit_transform(x_train_cudf) | ||
x_test_scaled = scaler.transform(x_test_cudf) | ||
|
||
# Create and train the model | ||
rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42) | ||
rf_classifier.fit(x_train_scaled, y_train_cudf) | ||
|
||
# Make predictions | ||
y_pred_cudf = rf_classifier.predict(x_test_scaled) | ||
|
||
# Convert predictions back to CPU for evaluation | ||
y_pred = y_pred_cudf.values_host | ||
y_test = y_test_cudf.values_host | ||
|
||
# Print results | ||
logger.info("cuML Results:") | ||
logger.info(f"Accuracy: {accuracy_score(y_test, y_pred)}") | ||
logger.info("Classification Report:") | ||
logger.info(classification_report(y_test, y_pred, target_names=iris.target_names)) | ||
|
||
|
||
if __name__ == "__main__": | ||
logging.basicConfig( | ||
level=logging.INFO, | ||
format="%(asctime)s - %(levelname)s - %(message)s", | ||
) | ||
main() |
Oops, something went wrong.