Create train_model.py
Browse files- train_model.py +30 -0
train_model.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sklearn.datasets import load_iris
|
2 |
+
from sklearn.model_selection import train_test_split
|
3 |
+
from sklearn.ensemble import RandomForestClassifier
|
4 |
+
from sklearn.metrics import accuracy_score, classification_report
|
5 |
+
import joblib
|
6 |
+
|
7 |
+
# Load the iris dataset
|
8 |
+
iris = load_iris()
|
9 |
+
X, y = iris.data, iris.target
|
10 |
+
|
11 |
+
# Split the data into training and testing sets
|
12 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
13 |
+
X, y, test_size=0.2, random_state=42
|
14 |
+
)
|
15 |
+
|
16 |
+
# Train a RandomForest classifier
|
17 |
+
clf = RandomForestClassifier(n_estimators=100, random_state=42)
|
18 |
+
clf.fit(X_train, y_train)
|
19 |
+
|
20 |
+
# Evaluate the model
|
21 |
+
y_pred = clf.predict(X_test)
|
22 |
+
accuracy = accuracy_score(y_test, y_pred)
|
23 |
+
report = classification_report(y_test, y_pred, target_names=iris.target_names)
|
24 |
+
|
25 |
+
print(f"Model Accuracy: {accuracy}")
|
26 |
+
print("Classification Report:")
|
27 |
+
print(report)
|
28 |
+
|
29 |
+
# Save the trained model to a file
|
30 |
+
joblib.dump(clf, "iris_model.pkl")
|