decision treees
This commit is contained in:
parent
4206edd60f
commit
f9374d02fa
@ -1,8 +1,13 @@
|
|||||||
from sklearn import tree
|
from sklearn import tree
|
||||||
|
from sklearn import metrics
|
||||||
|
from sklearn import preprocessing
|
||||||
|
from sklearn.ensemble import RandomForestClassifier
|
||||||
# from ...helpers.treenum import Tree
|
# from ...helpers.treenum import Tree
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
import csv
|
import csv
|
||||||
import random
|
import random
|
||||||
|
from matplotlib import pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
SIFT_PATH = "..\\algorithms\\data\\sift.csv"
|
SIFT_PATH = "..\\algorithms\\data\\sift.csv"
|
||||||
|
|
||||||
@ -20,34 +25,49 @@ class Tree(Enum):
|
|||||||
# [tree1_label, tree2_label]
|
# [tree1_label, tree2_label]
|
||||||
|
|
||||||
labels = []
|
labels = []
|
||||||
dialect = csv.Dialect
|
|
||||||
i = 0
|
i = 0
|
||||||
done = False
|
done = False
|
||||||
test_index = random.randint(0, 102)
|
|
||||||
print(test_index)
|
|
||||||
|
|
||||||
with open(SIFT_PATH, 'r') as file:
|
with open(SIFT_PATH, 'r') as file:
|
||||||
reader = csv.reader(file, delimiter= ',')
|
reader = csv.reader(file, delimiter= ',')
|
||||||
matrix = list(reader)
|
matrix = list(reader)
|
||||||
|
|
||||||
data = [[] for x in range(len(matrix)-1)]
|
data = [[] for x in range(len(matrix)-1)]
|
||||||
for row in matrix[1:]:
|
for row in matrix[1:]:
|
||||||
## Remove test case
|
|
||||||
if i == test_index and done == False:
|
|
||||||
done = True
|
|
||||||
data.pop(i)
|
|
||||||
continue
|
|
||||||
|
|
||||||
## append data to lists
|
## append data to lists
|
||||||
labels.append(Tree[row[0].upper()].value)
|
labels.append(Tree[row[0].upper()].value)
|
||||||
for element in row[1:]:
|
for element in row[1:]:
|
||||||
data[i].append(element)
|
data[i].append(float(element))
|
||||||
|
|
||||||
## iterator
|
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
clf = tree.DecisionTreeClassifier()
|
# Werkt niet met genormaliseerde data
|
||||||
clf = clf.fit(data, labels)
|
normalized = preprocessing.normalize(data, axis=0, norm='max')
|
||||||
# tree.plot_tree(clf)
|
norm = list(normalized.tolist())
|
||||||
print(Tree[matrix[test_index][0].upper()])
|
|
||||||
result = clf.predict([matrix[test_index][1:]])
|
actual = []
|
||||||
print(Tree(result[0]).name)
|
predicted = []
|
||||||
|
for i in range(75):
|
||||||
|
test_index = random.randint(1, 101)
|
||||||
|
temp_data = data.pop(test_index)
|
||||||
|
temp_label = labels.pop(test_index)
|
||||||
|
|
||||||
|
# dec_tree = tree.DecisionTreeClassifier(
|
||||||
|
# criterion='entropy',
|
||||||
|
# splitter='best')
|
||||||
|
dec_tree = RandomForestClassifier(max_depth=None)
|
||||||
|
dec_tree = dec_tree.fit(data, labels)
|
||||||
|
result = dec_tree.predict([matrix[test_index][1:]])
|
||||||
|
|
||||||
|
# normalized_list.append(temp_data)
|
||||||
|
data.append(temp_data)
|
||||||
|
labels.append(temp_label)
|
||||||
|
|
||||||
|
actual.append(temp_label)
|
||||||
|
predicted.append(result[0])
|
||||||
|
|
||||||
|
c_matrix = metrics.confusion_matrix(actual, predicted)
|
||||||
|
cm_display = metrics.ConfusionMatrixDisplay(confusion_matrix=c_matrix)
|
||||||
|
cm_display.plot()
|
||||||
|
plt.show(block=False)
|
||||||
|
# print("Testdata: \t" + Tree[matrix[test_index][0].upper()].name)
|
||||||
|
# print("Predicted: \t" + Tree(result[0]).name)
|
Loading…
Reference in New Issue
Block a user