CoolFace
Modelpublic

scikit-learn/skops-blog-example

sourceHugging Faceupdated 4y agoView on Hugging Face
1likes
train.py92 linesDownload Raw Back to root
1# let's import the libraries first2import sklearn3from sklearn.datasets import load_breast_cancer4from sklearn.tree import DecisionTreeClassifier5from sklearn.model_selection import train_test_split6from skops import card, hub_utils7import pickle8from sklearn.metrics import (ConfusionMatrixDisplay, confusion_matrix,9                            accuracy_score, f1_score)10import matplotlib.pyplot as plt11from pathlib import Path12 13# Load the data and split14X, y = load_breast_cancer(as_frame=True, return_X_y=True)15X_train, X_test, y_train, y_test = train_test_split(16    X, y, test_size=0.3, random_state=4217)18 19# Train the model20model = DecisionTreeClassifier().fit(X_train, y_train)21 22# let's save the model23model_path = "example.pkl"24local_repo = "my-awesome-model"25with open(model_path, mode="bw") as f:26    pickle.dump(model, file=f)27 28# we will now initialize a local repository29hub_utils.init(30    model=model_path, 31    requirements=[f"scikit-learn={sklearn.__version__}"], 32    dst=local_repo,33    task="tabular-classification",34    data=X_test,35)36 37 38# create the card 39model_card = card.Card(model, metadata=card.metadata_from_config(Path(destination_folder)))40 41limitations = "This model is not ready to be used in production."42model_description = "This is a DecisionTreeClassifier model trained on breast cancer dataset."43model_card_authors = "skops_user"44get_started_code = "import pickle \nwith open(dtc_pkl_filename, 'rb') as file: \n    clf = pickle.load(file)"45citation_bibtex = "bibtex\n@inproceedings{...,year={2020}}"46 47# we can add the information using add48model_card.add(49    citation_bibtex=citation_bibtex,50    get_started_code=get_started_code,51    model_card_authors=model_card_authors,52    limitations=limitations,53    model_description=model_description,54)55 56# we can set the metadata part directly57model_card.metadata.license = "mit"58 59# let's make a prediction and evaluate the model60y_pred = model.predict(X_test)61 62# we can pass metrics using add_metrics and pass details with add63model_card.add(eval_method="The model is evaluated using test split, on accuracy and F1 score with macro average.")64model_card.add_metrics(accuracy=accuracy_score(y_test, y_pred))65model_card.add_metrics(**{"f1 score": f1_score(y_test, y_pred, average="micro")})66 67# we will create a confusion matrix68cm = confusion_matrix(y_test, y_pred, labels=model.classes_)69disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model.classes_)70disp.plot()71 72# save the plot73plt.savefig(Path(local_repo) / "confusion_matrix.png")74 75# the plot will be written to the model card under the name confusion_matrix76# we pass the path of the plot itself77model_card.add_plot(confusion_matrix="confusion_matrix.png")78 79# save the card80model_card.save(Path(local_repo) / "README.md")81 82# if the repository doesn't exist remotely on the Hugging Face Hub, it will be created when we set create_remote to True83repo_id = "skops-user/my-awesome-model"84hub_utils.push(85    repo_id=repo_id,86    source=local_repo,87    token=token,88    commit_message="pushing files to the repo from the example!",89    create_remote=True,90)91 92