-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrain.py
52 lines (40 loc) · 1.36 KB
/
train.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import numpy as np
import sys
import matplotlib.pyplot as plt
from sklearn.externals import joblib
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, cross_val_score
# Configure file paths
MODEL=sys.argv[1]
DATA='model/data_600.npy'
LABEL='model/label_600.npy'
# Load data and labels
data=np.load(DATA)
label=np.load(LABEL)
# Split the data into train and test sets
x_train, x_test, y_train, y_test = train_test_split(data, label, test_size=0.20, random_state=42)
# Initialize a random forest classifier
print("\n<====Model====>\n")
clf = RandomForestClassifier(n_estimators=200,n_jobs=-1,verbose=1)
print(clf)
print("\n<====Training====>\n")
# Train the model
clf.fit(x_train, y_train)
# Check accuracy on test data
y_pred = clf.predict(x_test)
y_true = y_test
print("\nAccuracy:", accuracy_score(y_true,y_pred))
# Compute the confusion matrix
print("\n<====Confusion Matrix====>\n")
cf=confusion_matrix(y_true, y_pred)
print(cf)
print("\n<====Cross validation====>\n")
# Evaluate the model using five fold cross validation
scores = cross_val_score(clf, x_train, y_train, cv=5)
print("\nAccuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))
# Save the trained model
joblib.dump(clf, MODEL)
'''
Sample run: python train.py model/rfclassifier_600.sav
'''