-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
34 lines (24 loc) · 854 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
33
34
import joblib
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
seed = 42
#Read the dataset.
iris_df = pd.read_csv('inputs/iris.csv')
iris_df.sample(frac = 1, random_state=seed)
#Highlight the features and the target.
features = iris_df.iloc[:, :4]
target = iris_df.iloc[:, 4]
X_train, X_test, y_train, y_test = train_test_split(features, target, random_state = seed)
#Create an instance of the random forest classifier.
clf = RandomForestClassifier(n_estimators = 100)
#train the classifier.
clf.fit(X_train, y_train)
# predict on the test set.
y_pred = clf.predict(X_test)
# calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
#Save the model.
joblib.dump(clf, "models/rf_model.sav")