the worky version
This commit is contained in:
parent
9684522da7
commit
f11a5a82e2
@ -46,10 +46,10 @@ with open(PATH, 'r') as file:
|
|||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
# Werkt niet met genormaliseerde data
|
# Werkt niet met genormaliseerde data
|
||||||
normalized = preprocessing.normalize(data, axis=0, norm='max')
|
normalized = preprocessing.normalize(data, axis=0, norm='')
|
||||||
norm = list(normalized.tolist())
|
norm = list(normalized.tolist())
|
||||||
|
|
||||||
steps = np.linspace(1e-4, 1, 20, dtype=np.float64)
|
steps = np.linspace(1, 12, 4, dtype=np.int64)
|
||||||
|
|
||||||
print("Step \t seconds/step")
|
print("Step \t seconds/step")
|
||||||
for step in steps:
|
for step in steps:
|
||||||
@ -78,11 +78,12 @@ for step in steps:
|
|||||||
# model = ensemble.ExtraTreesClassifier(
|
# model = ensemble.ExtraTreesClassifier(
|
||||||
# n_estimators=step # higher is better, but slower (def: 100)
|
# n_estimators=step # higher is better, but slower (def: 100)
|
||||||
# )
|
# )
|
||||||
# model = neighbors.KNeighborsClassifier(
|
model = neighbors.KNeighborsClassifier(
|
||||||
# algorithm='auto',
|
algorithm='auto',
|
||||||
# leaf_size=2,
|
leaf_size=2,
|
||||||
# n_neighbors=step,
|
n_neighbors=step,
|
||||||
# )
|
)
|
||||||
|
|
||||||
# model = ensemble.BaggingClassifier(
|
# model = ensemble.BaggingClassifier(
|
||||||
# n_estimators=5,
|
# n_estimators=5,
|
||||||
# max_samples=.5,
|
# max_samples=.5,
|
||||||
|
@ -1,15 +1,22 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from sklearn.preprocessing import maxabs_scale, MaxAbsScaler
|
|
||||||
from sklearn.ensemble import RandomForestClassifier
|
from sklearn.ensemble import RandomForestClassifier
|
||||||
from joblib import dump, load
|
from joblib import dump, load
|
||||||
from sklearn import tree
|
from sklearn import tree
|
||||||
|
import numpy as np
|
||||||
import csv
|
import csv
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(prog='DecisionTree CLI')
|
parser = argparse.ArgumentParser(prog='DecisionTree CLI')
|
||||||
parser.add_argument('-i', '--input', help='Input CSV file', required=True)
|
parser.add_argument('-i', '--input', help='Input CSV file', required=True)
|
||||||
parser.add_argument('-o', '--output', help='Output model file', required=True)
|
parser.add_argument('-o', '--output', help='Output model folder', required=True)
|
||||||
|
parser.add_argument(
|
||||||
|
'-m',
|
||||||
|
'--model',
|
||||||
|
help='Chosen model (\'dectree\', \'randforest\' or \'extratree\')',
|
||||||
|
required=True
|
||||||
|
)
|
||||||
|
parser.add_argument('-s', '--scaler', help='Scaler preprocesser', required=True)
|
||||||
|
|
||||||
class Tree(Enum):
|
class Tree(Enum):
|
||||||
ACCASIA = 0
|
ACCASIA = 0
|
||||||
@ -52,19 +59,18 @@ class CVSuiteTestTree:
|
|||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
# normalize data
|
# normalize data
|
||||||
if self.scaler is not None:
|
#TODO: Arne help
|
||||||
norm = self.scaler.fit(data)
|
|
||||||
for row in norm:
|
|
||||||
print(len(row))
|
|
||||||
else:
|
|
||||||
raise EnvironmentError("No scaler found")
|
|
||||||
|
|
||||||
# train model
|
# train model
|
||||||
self.train(norm, labels, output)
|
self.train(data, labels, output)
|
||||||
|
|
||||||
def addScaler(self, path) -> None:
|
def addScaler(self, path) -> None:
|
||||||
self.scaler = load(path)
|
self.scaler = load(path)
|
||||||
|
|
||||||
|
if self.scaler is None:
|
||||||
|
print("Scaler failed to load!")
|
||||||
|
exit()
|
||||||
|
|
||||||
def train(self, data, labels, output) -> None:
|
def train(self, data, labels, output) -> None:
|
||||||
print("You called the parent class, doofus")
|
print("You called the parent class, doofus")
|
||||||
|
|
||||||
@ -75,7 +81,7 @@ class CVSuiteTestTree:
|
|||||||
|
|
||||||
def predict(self, data) -> None | int:
|
def predict(self, data) -> None | int:
|
||||||
if self.model is not None:
|
if self.model is not None:
|
||||||
return self.model.predict([data])
|
return self.model.predict(data)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -110,18 +116,16 @@ class CVSuiteTestExtraTrees(CVSuiteTestTree):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
test = CVSuiteTestRandomForest()
|
|
||||||
|
if args.model == 'dectree':
|
||||||
|
test = CVSuiteTestDecisionTree()
|
||||||
|
elif args.model == 'randforest':
|
||||||
|
test = CVSuiteTestRandomForest()
|
||||||
|
elif args.model == 'extratree':
|
||||||
|
test = CVSuiteTestExtraTrees()
|
||||||
|
else:
|
||||||
|
print("Model not found!")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
test.addScaler(args.scaler)
|
||||||
test.trainCSV(args.input, args.output)
|
test.trainCSV(args.input, args.output)
|
||||||
test = CVSuiteTestDecisionTree(
|
|
||||||
"C:\\Users\\Tom\\Desktop\\Files\\Repositories\\EV5_Beeldherk_Bomen\\models\\randomforest.joblib"
|
|
||||||
)
|
|
||||||
path = "C:\\Users\\Tom\\Desktop\\Files\\Repositories\\EV5_Beeldherk_Bomen\\dataset\\csv\\result-2023-10-21T09.59.24.csv"
|
|
||||||
file = open(path, 'r')
|
|
||||||
reader = csv.reader(file, delimiter=',')
|
|
||||||
matrix = list(reader)
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
data = [float(x) for x in matrix[2][2:]]
|
|
||||||
norm = maxabs_scale(data)
|
|
||||||
|
|
||||||
print(test.predict(norm))
|
|
@ -347,8 +347,12 @@ class CVSuite:
|
|||||||
print(C_WARN, "KNN Model not configured!")
|
print(C_WARN, "KNN Model not configured!")
|
||||||
|
|
||||||
if self.test_dectree is not None:
|
if self.test_dectree is not None:
|
||||||
print(self.test_dectree.predict(data))
|
result = self.test_dectree.predict(data)
|
||||||
output.insert("end", "Decision Tree Result:\n")
|
output.insert("end", "Decision Tree Result:\n")
|
||||||
|
output.insert("end", f"\t{Tree(result).name}\n")
|
||||||
|
|
||||||
|
print(C_DBUG, "Decision Tree Result:")
|
||||||
|
print("\t\t result: \t{}".format(result))
|
||||||
else:
|
else:
|
||||||
print(C_WARN, "Decison Tree Model not configured!")
|
print(C_WARN, "Decison Tree Model not configured!")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user