| """ |
| Train a Random Forest Classifier on the Iris dataset. |
| Dataset source: UCI Machine Learning Repository |
| https://archive.ics.uci.edu/dataset/53/iris |
| """ |
|
|
| import joblib |
| import numpy as np |
| from sklearn.datasets import load_iris |
| from sklearn.ensemble import RandomForestClassifier |
| from sklearn.model_selection import train_test_split |
| from sklearn.metrics import accuracy_score, classification_report |
| from sklearn.preprocessing import StandardScaler |
|
|
| |
| iris = load_iris() |
| X, y = iris.data, iris.target |
|
|
| |
| X_train, X_test, y_train, y_test = train_test_split( |
| X, y, test_size=0.2, random_state=42, stratify=y |
| ) |
|
|
| |
| scaler = StandardScaler() |
| X_train_scaled = scaler.fit_transform(X_train) |
| X_test_scaled = scaler.transform(X_test) |
|
|
| |
| model = RandomForestClassifier(n_estimators=100, random_state=42) |
| model.fit(X_train_scaled, y_train) |
|
|
| |
| y_pred = model.predict(X_test_scaled) |
| accuracy = accuracy_score(y_test, y_pred) |
| print(f"Accuracy: {accuracy * 100:.2f}%") |
| print(classification_report(y_test, y_pred, target_names=iris.target_names)) |
|
|
| |
| joblib.dump(model, "model/iris_model.pkl") |
| joblib.dump(scaler, "model/scaler.pkl") |
| joblib.dump(iris.target_names.tolist(), "model/class_names.pkl") |
| print("Model, scaler, and class names saved.") |
|
|