-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
32 lines (26 loc) · 1018 Bytes
/
train_model.py
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
30
31
32
import pickle
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
# Load the iris dataset
iris = load_iris()
X, y = iris.data, iris.target
# Split the dataset (80% training, 20% testing)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Build a pipeline: scaler + classifier (this ensures any future input is processed in the same way)
pipeline = Pipeline([
('scaler', StandardScaler()),
('classifier', RandomForestClassifier(random_state=42))
])
# Train the model
pipeline.fit(X_train, y_train)
# Evaluate the model
y_pred = pipeline.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Test Accuracy: {accuracy:.2f}")
# Save the entire pipeline to disk
with open("iris_pipeline.pkl", "wb") as f:
pickle.dump(pipeline, f)