drzewo decyzyjne i implementacja klasyfikatora

This commit is contained in:
s473632 2023-06-07 21:59:11 +02:00
parent 3a4fcf611e
commit f96f8229a9
4 changed files with 149 additions and 3 deletions

View File

@ -61,7 +61,7 @@ class HousePOI:
self.truck_fullness = Interface.truck_fullness.fullness
self.payment = payment
self.bin_fullness = bin_fullness
self.trash_type = guessed_trash_type
self.trash_type = guessed_trash_type[0]
if self.season == 'autumn':
season_autumn = 1
@ -114,11 +114,11 @@ class HousePOI:
if outcome == 1:
Interface.truck_fullness.fullness[trash_type] += 0.25 # 0.25
debug(dec_tree, outcome, trash_type, guessed_trash_type, self.season, self.day, self.bin_fullness,
debug(dec_tree, outcome, trash_type, guessed_trash_type[0], guessed_trash_type[1], self.season, self.day, self.bin_fullness,
self.truck_fullness)
def debug(dec_tree, outcome, trash_type, guessed_trash_type, season, day, bin_fullness, truck_fullness):
def debug(dec_tree, outcome, trash_type, guessed_trash_type, filename, season, day, bin_fullness, truck_fullness):
HOUSE_NUMBER['House_number'] += 1
print("Domek nr: " + str(HOUSE_NUMBER['House_number']))
if outcome == 1:
@ -128,6 +128,7 @@ def debug(dec_tree, outcome, trash_type, guessed_trash_type, season, day, bin_fu
print("Pora: " + season)
print("Dzień: " + day)
print("Typ śmieci: " + trash_type)
print("Nazwa pliku: " + trash_type + str(filename))
print("Zaobserwowane: " + guessed_trash_type)
print("Zapełnienie domu: " + str(bin_fullness))
print(truck_fullness)

30
src/decisiontree.py Normal file
View File

@ -0,0 +1,30 @@
import pandas as pd
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
def encode_category(category, categories):
encoded = pd.get_dummies(categories).astype(int)
if category in encoded.columns:
return encoded[category].values.tolist()
else:
return [0] * len(encoded.columns)
data = pd.read_csv('tree_data.csv')
data_encoded = pd.get_dummies(data, columns=['trash_types', 'season', 'day'])
X = data_encoded.drop('decision', axis=1)
y = data_encoded['decision']
clf = DecisionTreeClassifier()
decision_tree = clf.fit(X, y)
with open('./tree_in_txt.txt', "w") as file:
file.write(tree.export_text(decision_tree, feature_names=X.columns.tolist()))
def decision(conditions):
new_conditions = pd.DataFrame(conditions)
new_decision = decision_tree.predict(new_conditions)
return new_decision

48
src/trashtype.py Normal file
View File

@ -0,0 +1,48 @@
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
import PIL
import random
from PIL import Image
def trash_type_def(trash_type):
img_num = 0
img_height, img_width = 180, 180
if trash_type == 'biological':
img_num = random.randint(1, 985)
if trash_type == 'glass':
img_num = random.randint(1, 501)
if trash_type == 'paper':
img_num = random.randint(1, 594)
if trash_type == 'plastic':
img_num = random.randint(1, 482)
filename = trash_type + str(img_num)
class_names = ['biological', 'glass', 'paper', 'plastic']
trash_classification_model = tf.keras.models.load_model('../trash-classification/m_with_data_augmentation.h5')
test_image_path = "../trash-classification/trash/test/"+trash_type+"/"+filename+".jpg"
img = tf.keras.utils.load_img(
test_image_path, target_size=(img_height, img_width)
)
img_array = tf.keras.utils.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
predictions = trash_classification_model.predict(img_array)
score = tf.nn.softmax(predictions[0])
name = class_names[np.argmax(score)]
file_path = "../trash-classification/trash/test/"+trash_type+"/"+filename+".jpg"
img_show = Image.open(file_path)
img_numpy_format = np.asarray(img_show)
plt.imshow(img_numpy_format)
plt.suptitle(filename)
plt.draw()
plt.pause(1)
plt.close()
return [name, img_num]

67
src/tree.py Normal file
View File

@ -0,0 +1,67 @@
import csv
import random
import pandas as pd
truck_fullness = [0, 1]
trash_types = ['paper', 'plastic', 'glass', 'mixed']
payment = [0, 1]
bin_fullness = [0, 1]
day = ['monday', 'wednesday', 'friday']
season = ['spring', 'summer', 'autumn', 'winter']
dump_fullness = [0, 1]
trash = [0, 1]
# spring: monday = plastic, wednesday = glass, friday = biological & paper
# summer: monday = glass, wednesday = biological & plastic, friday = paper
# autumn: monday = biological & plastic, wednesday = glass, friday = paper
# winter: monday = paper, wednesday = biological & plastic, friday = glass
with open('tree_data.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(
['dump_fullness', 'truck_fullness', 'trash', 'payment', 'bin_fullness', 'trash_types', 'season', 'day',
'decision'])
rows = set()
positives = [(0, 0, 1, 1, 1, 'glass', 'spring', 'wednesday', 1),
(0, 0, 1, 1, 1, 'glass', 'summer', 'monday', 1),
(0, 0, 1, 1, 1, 'glass', 'autumn', 'wednesday', 1),
(0, 0, 1, 1, 1, 'glass', 'winter', 'friday', 1),
(0, 0, 1, 1, 1, 'paper', 'spring', 'friday', 1),
(0, 0, 1, 1, 1, 'paper', 'summer', 'friday', 1),
(0, 0, 1, 1, 1, 'paper', 'autumn', 'friday', 1),
(0, 0, 1, 1, 1, 'paper', 'winter', 'monday', 1),
(0, 0, 1, 1, 1, 'plastic', 'spring', 'monday', 1),
(0, 0, 1, 1, 1, 'plastic', 'summer', 'wednesday', 1),
(0, 0, 1, 1, 1, 'plastic', 'autumn', 'monday', 1),
(0, 0, 1, 1, 1, 'plastic', 'winter', 'wednesday', 1),
(0, 0, 1, 1, 1, 'mixed', 'spring', 'friday', 1),
(0, 0, 1, 1, 1, 'mixed', 'summer', 'wednesday', 1),
(0, 0, 1, 1, 1, 'mixed', 'autumn', 'monday', 1),
(0, 0, 1, 1, 1, 'mixed', 'winter', 'wednesday', 1)]
for positive in positives:
rows.add(positive)
while len(rows) < 200:
rand_1 = random.choice(dump_fullness)
rand_2 = random.choice(truck_fullness)
rand_3 = random.choice(trash)
rand_4 = random.choice(payment)
rand_5 = random.choice(bin_fullness)
rand_6 = random.choice(trash_types)
rand_7 = random.choice(season)
rand_8 = random.choice(day)
rows.add((rand_1, rand_2, rand_3, rand_4, rand_5, rand_6, rand_7, rand_8, 0))
rows_tuple = tuple(rows)
for elem in rows_tuple:
writer.writerow(elem)
dt = pd.read_csv('tree_data.csv')
print(dt.head())