Upload train.py
Browse files
train.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# let's import the libraries first
|
| 2 |
+
import sklearn
|
| 3 |
+
from sklearn.datasets import load_breast_cancer
|
| 4 |
+
from sklearn.tree import DecisionTreeClassifier
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from skops import card, hub_utils
|
| 7 |
+
import pickle
|
| 8 |
+
from sklearn.metrics import (ConfusionMatrixDisplay, confusion_matrix,
|
| 9 |
+
accuracy_score, f1_score)
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
# Load the data and split
|
| 14 |
+
X, y = load_breast_cancer(as_frame=True, return_X_y=True)
|
| 15 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 16 |
+
X, y, test_size=0.3, random_state=42
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Train the model
|
| 20 |
+
model = DecisionTreeClassifier().fit(X_train, y_train)
|
| 21 |
+
|
| 22 |
+
# let's save the model
|
| 23 |
+
model_path = "example.pkl"
|
| 24 |
+
local_repo = "my-awesome-model"
|
| 25 |
+
with open(model_path, mode="bw") as f:
|
| 26 |
+
pickle.dump(model, file=f)
|
| 27 |
+
|
| 28 |
+
# we will now initialize a local repository
|
| 29 |
+
hub_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
|
| 39 |
+
model_card = card.Card(model, metadata=card.metadata_from_config(Path(destination_folder)))
|
| 40 |
+
|
| 41 |
+
limitations = "This model is not ready to be used in production."
|
| 42 |
+
model_description = "This is a DecisionTreeClassifier model trained on breast cancer dataset."
|
| 43 |
+
model_card_authors = "skops_user"
|
| 44 |
+
get_started_code = "import pickle \nwith open(dtc_pkl_filename, 'rb') as file: \n clf = pickle.load(file)"
|
| 45 |
+
citation_bibtex = "bibtex\n@inproceedings{...,year={2020}}"
|
| 46 |
+
|
| 47 |
+
# we can add the information using add
|
| 48 |
+
model_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 directly
|
| 57 |
+
model_card.metadata.license = "mit"
|
| 58 |
+
|
| 59 |
+
# let's make a prediction and evaluate the model
|
| 60 |
+
y_pred = model.predict(X_test)
|
| 61 |
+
|
| 62 |
+
# we can pass metrics using add_metrics and pass details with add
|
| 63 |
+
model_card.add(eval_method="The model is evaluated using test split, on accuracy and F1 score with macro average.")
|
| 64 |
+
model_card.add_metrics(accuracy=accuracy_score(y_test, y_pred))
|
| 65 |
+
model_card.add_metrics(**{"f1 score": f1_score(y_test, y_pred, average="micro")})
|
| 66 |
+
|
| 67 |
+
# we will create a confusion matrix
|
| 68 |
+
cm = confusion_matrix(y_test, y_pred, labels=model.classes_)
|
| 69 |
+
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model.classes_)
|
| 70 |
+
disp.plot()
|
| 71 |
+
|
| 72 |
+
# save the plot
|
| 73 |
+
plt.savefig(Path(local_repo) / "confusion_matrix.png")
|
| 74 |
+
|
| 75 |
+
# the plot will be written to the model card under the name confusion_matrix
|
| 76 |
+
# we pass the path of the plot itself
|
| 77 |
+
model_card.add_plot(confusion_matrix="confusion_matrix.png")
|
| 78 |
+
|
| 79 |
+
# save the card
|
| 80 |
+
model_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 True
|
| 83 |
+
repo_id = "skops-user/my-awesome-model"
|
| 84 |
+
hub_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 |
+
|