-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
28 lines (22 loc) · 901 Bytes
/
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
import scipy.io as sio
import numpy as np
from sklearn.utils import shuffle
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.externals import joblib
# load data file as dict object
train_data = sio.loadmat('extra_32x32.mat')
# extract the images (X) and labels (y) from the dict
X = train_data['X']
y = train_data['y']
# reshape our matrices into 1D vectors and shuffle (still maintaining the index pairings)
X = X.reshape(X.shape[0]*X.shape[1]*X.shape[2],X.shape[3]).T
y = y.reshape(y.shape[0],)
X, y = shuffle(X, y, random_state=42)
# split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.05, random_state=42)
# define classifier and fit to training data
clf = RandomForestClassifier()
clf.fit(X_train, y_train)
# save model
joblib.dump(clf, 'model.pkl')