-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
55 lines (39 loc) · 1.47 KB
/
Main.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
import PIL
from PIL import Image
import numpy as np
import FlatPreProcessing
from scipy.misc import imread
import matplotlib.pyplot as plt
from pathlib import Path
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
BASE_DIR = Path('.')
if not Path('train_data.txt').is_file() and not Path('label_data.txt').is_file():
(train_data, label_data) = FlatPreProcessing.main()
# train_data.tofile('train_data.txt')
# label_data.tofile('label_data.txt')
print(label_data.shape)
else:
train_data = np.fromfile('train_data.txt')
label_data = np.fromfile('label_data.txt')
print(label_data.shape)
label_data = label_data.ravel()
train_data.shape
rf = RandomForestClassifier()
rf.fit(train_data, label_data)
# print("Prediction for 0: {} | {}".format(rf.predict(train_data[0:100]), label_data[0:100]))
# print("Prediction for 1: {} | {}".format(rf.predict(train_data[1]), label_data[1]))
print("Prediciting Data..")
predicitions = rf.predict(train_data)
print("Finished Random Forest!")
neigh = KNeighborsClassifier(n_neighbors=8)
neigh.fit(train_data,label_data)
predictions = neigh.predict()
truepos = np.sum(np.multiply(predictions, label_data))
falsepos = np.sum(predictions) - truepos
falseneg = np.sum(label_data) - truepos
precision = truepos / (truepos+falsepos)
recall = truepos / (truepos+falseneg)
dice = 2 * precision * recall / (precision + recall)
print(dice)