forked from s474139/Inteligentny_Wozek
59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
import graphviz
|
|
import pandas as pd
|
|
from sklearn.tree import DecisionTreeClassifier
|
|
from sklearn.tree import export_graphviz
|
|
|
|
def make_tree():
|
|
plikZPrzecinkami = open("training_data.txt", 'w')
|
|
|
|
with open('DecisionTree/200permutations_table.txt', 'r') as plik:
|
|
for linia in plik:
|
|
liczby = linia.strip()
|
|
wiersz = ""
|
|
licznik = 0
|
|
for liczba in liczby:
|
|
wiersz += liczba
|
|
wiersz += ";"
|
|
wiersz = wiersz[:-1]
|
|
wiersz += '\n'
|
|
plikZPrzecinkami.write(wiersz)
|
|
|
|
plikZPrzecinkami.close()
|
|
|
|
x = pd.read_csv('DecisionTree/training_data.txt', delimiter=';',
|
|
names=['wielkosc', 'waga,', 'priorytet', 'ksztalt', 'kruchosc', 'dolna', 'gorna', 'g > d'])
|
|
y = pd.read_csv('DecisionTree/decisions.txt', names=['polka'])
|
|
# X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=1) # 70% treningowe and 30% testowe
|
|
|
|
# Tworzenie instancji klasyfikatora ID3
|
|
clf = DecisionTreeClassifier(criterion='entropy')
|
|
|
|
# Trenowanie klasyfikatora
|
|
clf.fit(x.values, y.values)
|
|
# clf.fit(X_train, y_train)
|
|
return clf
|
|
|
|
# # Predykcja na nowych danych
|
|
# new_data = [[2, 2, 1, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
|
|
# predictions = clf.predict(new_data)
|
|
# # y_pred = clf.predict(X_test)
|
|
|
|
|
|
# print(predictions)
|
|
# # print("Accuracy:", clf.score(new_data, predictions))
|
|
# # print("Accuracy:", metrics.accuracy_score(y_test, y_pred))
|
|
|
|
|
|
# Wygenerowanie pliku .dot reprezentującego drzewo
|
|
# dot_data = export_graphviz(clf, out_file=None, feature_names=list(x.columns), class_names=['0', '1'], filled=True,
|
|
# rounded=True)
|
|
|
|
# # Tworzenie obiektu graphviz z pliku .dot
|
|
# graph = graphviz.Source(dot_data)
|
|
|
|
# # Wyświetlanie drzewa
|
|
# graph.view()
|
|
|
|
# z = pd.concat([x, y], axis=1)
|
|
# z.to_csv('dane.csv', index=False)
|