scikit-learn/Fish-Weight
2
1from datasets import load_dataset2import pandas as pd3 4from sklearn.ensemble import GradientBoostingRegressor5from sklearn.pipeline import make_pipeline6from sklearn.compose import make_column_transformer7from sklearn.compose import make_column_selector8from sklearn.preprocessing import OneHotEncoder9 10from skops import hub_utils11import pickle12from skops import card13from pathlib import Path14 15my_token = "your token here"16 17# Load our data18dataset = load_dataset("brendenc/Fish")19 20df = pd.DataFrame(dataset['train'][:])21target = df.Weight22df = df.drop('Weight', axis=1)23 24# One hot encode our input25one_hot_encoder = make_column_transformer(26 (27 OneHotEncoder(sparse=False, handle_unknown="ignore"),28 make_column_selector(dtype_include="object"),29 ),30 remainder="passthrough",31)32 33# Train model34pipe = make_pipeline(35 one_hot_encoder, GradientBoostingRegressor(random_state=42)36)37 38pipe.fit(df, target)39 40# Save the model41model_path = "example.pkl"42local_repo = "fish-model"43with open(model_path, mode="bw") as f:44 pickle.dump(pipe, file=f)45 46# we will now initialize a local repository47hub_utils.init(48 model=model_path, 49 requirements=[f"scikit-learn={sklearn.__version__}"], 50 dst=local_repo,51 task="tabular-regression",52 data=df,53)54 55# create the card 56model_card = card.Card(pipe, metadata=card.metadata_from_config(Path('fish-model')))57 58limitations = "This model is intended for educational purposes."59model_description = "This is a GradientBoostingRegressor on a fish dataset."60model_card_authors = "Brenden Connors"61 62 63# we can add the information using add64model_card.add(65 model_card_authors=model_card_authors,66 limitations=limitations,67 model_description=model_description,68)69 70# we can set the metadata part directly71model_card.metadata.license = "mit"72 73model_card.save(Path(local_repo) / "README.md")74 75# Push to the hub76repo_id = "scikit-learn/Fish-Weight/Fish-Weight"77hub_utils.push(78 repo_id=repo_id,79 source=local_repo,80 token=my_token,81 commit_message="Adding model files",82 create_remote=True,83)