-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage classification using Random forest.py
More file actions
112 lines (58 loc) · 1.85 KB
/
image classification using Random forest.py
File metadata and controls
112 lines (58 loc) · 1.85 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# coding: utf-8
# In[1]:
## Image classification using RandomForest: An example in Python using CIFAR10 Dataset
def Snippet_349():
print()
print(format('Image classification using RandomForest: An example in Python using CIFAR10 Dataset','*^88'))
# In[2]:
import warnings
warnings.filterwarnings("ignore")
# In[3]:
# load libraries
from keras.datasets import cifar10
from sklearn import metrics
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
# In[2]:
import time
start_time = time.time()
# In[6]:
# data: shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
# X_train is 50000 rows of 3x32x32 values --> reshaped in 50000 x 3072
RESHAPED = 3072
# In[7]:
X_train = X_train.reshape(50000, RESHAPED)
X_test = X_test.reshape(10000, RESHAPED)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# In[ ]:
y_train = y_train.flatten()
y_test = y_test.flatten()
# In[ ]:
# normalize the datasets
X_train /= 255.
X_test /= 255.
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# In[ ]:
# fit a RandomForest model to the data
model = RandomForestClassifier(n_estimators = 10)
cv_results = cross_val_score(model, X_train, y_train,
cv = 2, scoring='accuracy', n_jobs = -1, verbose = 1)
model.fit(X_train, y_train)
print(); print(cv_results)
print(); print(model)
# In[ ]:
# make predictions
expected_y = y_test
predicted_y = model.predict(X_test)
# In[ ]:
# summarize the fit of the model
print(); print(metrics.classification_report(expected_y, predicted_y))
print(); print(metrics.confusion_matrix(expected_y, predicted_y))
print()
print("Execution Time %s seconds: " % (time.time() - start_time))
Snippet_349()
# In[ ]: