-
Notifications
You must be signed in to change notification settings - Fork 1
/
RandomForest.py
178 lines (110 loc) · 3.88 KB
/
RandomForest.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python
# coding: utf-8
# # Importing all the necessary modules
# In[1]:
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
import pickle
import gzip
from sklearn.svm import SVC
from PIL import Image
import os
from sklearn.datasets import fetch_mldata
from numpy import arange
from sklearn.metrics import confusion_matrix
# # Load MNIST data
# In[2]:
filename = 'mnist.pkl.gz'
f = gzip.open(filename, 'rb')
training_data, validation_data, test_data = pickle.load(f, encoding='latin1')
f.close()
# # Confusion Matrix Customization
# In[3]:
def plot_confusion_matrix(cm,
title='Confusion matrix',
cmap=None,
normalize=True):
import matplotlib.pyplot as plt
import numpy as np
import itertools
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
if cmap is None:
cmap = plt.get_cmap('Blues')
plt.figure(figsize=(8, 6))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
thresh = cm.max() / 1.5 if normalize else cm.max() / 2
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
if normalize:
plt.text(j, i, "{:0.4f}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
else:
plt.text(j, i, "{:,}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
plt.show()
# # Validating and Testing on MNIST dataset
# In[4]:
classifier2 = RandomForestClassifier(n_estimators=200, max_features=50)
model_fit = classifier2.fit(training_data[0], training_data[1])
print(' Validation Accuracy')
print(classifier2.score(validation_data[0], validation_data[1]))
print(' Testing Accuracy')
print(classifier2.score(test_data[0], test_data[1]))
# # Confusion matrix on MNIST Dataset
# In[5]:
RandomForest_prediction_mnist=model_fit.predict(test_data[0])
conf_mat = confusion_matrix(test_data[1], RandomForest_prediction_mnist)
plot_confusion_matrix(cm = conf_mat,
normalize = False,
title = "Confusion Matrix")
# # Testing on USPS Dataset
# In[6]:
image_size = 28
num_labels = 10
USPSMat = []
USPSTar = []
curPath = 'USPSdata/Numerals'
savedImg = []
for j in range(0,10):
curFolderPath = curPath + '/' + str(j)
imgs = os.listdir(curFolderPath)
for img in imgs:
curImg = curFolderPath + '/' + img
if curImg[-3:] == 'png':
img = Image.open(curImg,'r')
img = img.resize((28, 28))
savedImg = img
imgdata = (255-np.array(img.getdata()))/255
USPSMat.append(imgdata)
USPSTar.append(j)
def reformat(labels):
labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
return labels
USPSMat = np.array(USPSMat)
USPSTar= np.array(USPSTar)
rf=reformat(USPSTar)
print('USPS dataset Testing accuracy')
y=classifier2.score(USPSMat, USPSTar)
print(y)
# # Confusion matrix on USPS dataset
# In[7]:
RandomForest_prediction_usps=model_fit.predict(USPSMat)
conf_mat1 = confusion_matrix(USPSTar, RandomForest_prediction_usps)
plot_confusion_matrix(cm = conf_mat1,
normalize = False,
title = "Confusion Matrix")
# In[8]:
from sklearn.metrics import classification_report
print(classification_report(RandomForest_prediction_usps,
USPSTar))
# In[ ]: