Download scikit-learn/isolation_forest/predict.py from activeeon/models-repository: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/activeeon/models-repository/resolve/main/scikit-learn/isolation_forest/predict.py
- Command line
-
hf download hf://activeeon/models-repository/scikit-learn/isolation_forest/predict.py
-
curl -L -o predict.py https://huggingface.co/activeeon/models-repository/resolve/main/scikit-learn/isolation_forest/predict.py
1.07 kB
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| import joblib | |
| import numpy as np | |
| from sklearn.datasets import fetch_openml | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.metrics import accuracy_score | |
| # Load the model from disk | |
| model = joblib.load('isolation_forest.joblib') | |
| # Set the random seed | |
| random_seed = 0 | |
| np.random.seed(random_seed) | |
| # Load the dataset | |
| dataset_name = "cardiotocography" | |
| dataset = fetch_openml(name=dataset_name, version=1, as_frame=False) | |
| X, y = dataset.data, dataset.target | |
| s = y == "3" | |
| y = s.astype(int) | |
| # Split the dataset into training and testing sets | |
| _, X_test, _, y_test = train_test_split(X, y, test_size=0.25, random_state=random_seed) | |
| print(f'X_test:\n{X_test[0]}') | |
| print(f'y_test:\n{y_test[0]}') | |
| # Use the model to make predictions on the test data | |
| y_pred = model.predict(X_test) | |
| # Mapping 1->0 and -1->1 | |
| y_pred = np.where(y_pred == 1, 0, 1) | |
| print(f'y_pred:\n{y_pred[0]}') | |
| # Score the model using accuracy classification score | |
| acc = accuracy_score(y_test, y_pred) | |
| print(f'Accuracy classification score: {acc}') | |