Instructions to use scikit-learn/skops-blog-example with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Scikit-learn
How to use scikit-learn/skops-blog-example with Scikit-learn:
from huggingface_hub import hf_hub_download import joblib model = joblib.load( hf_hub_download("scikit-learn/skops-blog-example", "sklearn_model.joblib") ) # only load pickle files from sources you trust # read more about it here https://skops.readthedocs.io/en/stable/persistence.html - Notebooks
- Google Colab
- Kaggle
| # let's import the libraries first | |
| import sklearn | |
| from sklearn.datasets import load_breast_cancer | |
| from sklearn.tree import DecisionTreeClassifier | |
| from sklearn.model_selection import train_test_split | |
| from skops import card, hub_utils | |
| import pickle | |
| from sklearn.metrics import (ConfusionMatrixDisplay, confusion_matrix, | |
| accuracy_score, f1_score) | |
| import matplotlib.pyplot as plt | |
| from pathlib import Path | |
| # Load the data and split | |
| X, y = load_breast_cancer(as_frame=True, return_X_y=True) | |
| X_train, X_test, y_train, y_test = train_test_split( | |
| X, y, test_size=0.3, random_state=42 | |
| ) | |
| # Train the model | |
| model = DecisionTreeClassifier().fit(X_train, y_train) | |
| # let's save the model | |
| model_path = "example.pkl" | |
| local_repo = "my-awesome-model" | |
| with open(model_path, mode="bw") as f: | |
| pickle.dump(model, file=f) | |
| # we will now initialize a local repository | |
| hub_utils.init( | |
| model=model_path, | |
| requirements=[f"scikit-learn={sklearn.__version__}"], | |
| dst=local_repo, | |
| task="tabular-classification", | |
| data=X_test, | |
| ) | |
| # create the card | |
| model_card = card.Card(model, metadata=card.metadata_from_config(Path(destination_folder))) | |
| limitations = "This model is not ready to be used in production." | |
| model_description = "This is a DecisionTreeClassifier model trained on breast cancer dataset." | |
| model_card_authors = "skops_user" | |
| get_started_code = "import pickle \nwith open(dtc_pkl_filename, 'rb') as file: \n clf = pickle.load(file)" | |
| citation_bibtex = "bibtex\n@inproceedings{...,year={2020}}" | |
| # we can add the information using add | |
| model_card.add( | |
| citation_bibtex=citation_bibtex, | |
| get_started_code=get_started_code, | |
| model_card_authors=model_card_authors, | |
| limitations=limitations, | |
| model_description=model_description, | |
| ) | |
| # we can set the metadata part directly | |
| model_card.metadata.license = "mit" | |
| # let's make a prediction and evaluate the model | |
| y_pred = model.predict(X_test) | |
| # we can pass metrics using add_metrics and pass details with add | |
| model_card.add(eval_method="The model is evaluated using test split, on accuracy and F1 score with macro average.") | |
| model_card.add_metrics(accuracy=accuracy_score(y_test, y_pred)) | |
| model_card.add_metrics(**{"f1 score": f1_score(y_test, y_pred, average="micro")}) | |
| # we will create a confusion matrix | |
| cm = confusion_matrix(y_test, y_pred, labels=model.classes_) | |
| disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model.classes_) | |
| disp.plot() | |
| # save the plot | |
| plt.savefig(Path(local_repo) / "confusion_matrix.png") | |
| # the plot will be written to the model card under the name confusion_matrix | |
| # we pass the path of the plot itself | |
| model_card.add_plot(confusion_matrix="confusion_matrix.png") | |
| # save the card | |
| model_card.save(Path(local_repo) / "README.md") | |
| # if the repository doesn't exist remotely on the Hugging Face Hub, it will be created when we set create_remote to True | |
| repo_id = "skops-user/my-awesome-model" | |
| hub_utils.push( | |
| repo_id=repo_id, | |
| source=local_repo, | |
| token=token, | |
| commit_message="pushing files to the repo from the example!", | |
| create_remote=True, | |
| ) | |