-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathRndmForest.py
62 lines (51 loc) · 1.72 KB
/
RndmForest.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
import sys
import os
import numpy as np
from PIL import Image
from operator import itemgetter
path = "train1"
folder_names = [name for name in os.listdir(path) if os.path.isdir(os.path.join(path, name))]
#print(folder_names)
# Each row is an image
img = np.zeros([94257, 2025], dtype = float)
labels = np.zeros([94257])
j = 0
for i in range(10):
for image in os.listdir(path + "/" + folder_names[i]):
train_image = path + "/" + folder_names[i] + "/" + image
img[j] = np.asarray(Image.open(train_image).convert('L').resize((45,45), Image.ANTIALIAS)).flatten()
img[j] = img[j] / 255.0
labels[j] = folder_names[i]
j += 1
y=labels
X=img
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
from sklearn.ensemble import RandomForestClassifier
"""
path = "train"
folder_names = [name for name in os.listdir(path) if os.path.isdir(os.path.join(path, name))]
#print(folder_names)
# Each row is an image
img = np.zeros([160, 2025], dtype = float)
labels = np.zeros([160])
j = 0
for i in range(10):
for image in os.listdir(path + "/" + folder_names[i]):
train_image = path + "/" + folder_names[i] + "/" + image
img[j] = np.asarray(Image.open(train_image).convert('L').resize((45,45), Image.ANTIALIAS)).flatten()
#img[j] = img[j] / 255.0
labels[j] = folder_names[i]
j += 1
valData=img
valLabels=labels
"""
accuracies=[]
k=10
# train the k-Nearest Neighbor classifier with the current value of `k`
model = RandomForestClassifier(n_jobs=-1,n_estimators= 10)
model.fit(X_train, y_train)
# evaluate the model and update the accuracies list
score = model.score(X_test, y_test)
print("k=%d, accuracy=%.2f%%" % (k, score * 100))
accuracies.append(score)