File size: 814 Bytes
3b52e32 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import pickle
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
def build_and_save_model(output_path="model.pkl"):
data = load_iris()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
pipe = Pipeline([
("scaler", StandardScaler()),
("clf", RandomForestClassifier(n_estimators=50, random_state=42))
])
pipe.fit(X_train, y_train)
with open(output_path, "wb") as f:
pickle.dump(pipe, f)
print(f"Model entraîné et sauvegardé dans {output_path}")
if __name__ == "__main__":
build_and_save_model()
|