Traktor/decisiontree.py

44 lines
959 B
Python
Raw Normal View History

2024-05-12 21:10:52 +02:00
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree
import matplotlib.pyplot as plt
import pandas as pd
2024-05-13 00:31:33 +02:00
2024-05-12 21:10:52 +02:00
data = pd.read_csv("dane.csv")
2024-05-13 00:14:29 +02:00
print(data)
2024-05-12 21:10:52 +02:00
# Wczytanie danych
X = data.drop(columns=["podlac"])
X = pd.get_dummies(X)
y = data["podlac"]
2024-05-13 00:14:29 +02:00
2024-05-12 21:10:52 +02:00
# Podział danych na zbiór treningowy i testowy
2024-05-13 02:12:36 +02:00
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
2024-05-12 21:10:52 +02:00
# Inicjalizacja i dopasowanie modelu drzewa decyzyjnego
2024-05-13 02:12:36 +02:00
model = DecisionTreeClassifier(max_depth=4)
2024-05-13 00:14:29 +02:00
model.fit(X_train, y_train)
2024-05-12 21:10:52 +02:00
2024-05-13 00:14:29 +02:00
# Wyliczenie poprawności algorytmu
accuracy = model.score(X_test, y_test)
2024-05-12 21:10:52 +02:00
print("Accuracy:", accuracy)
2024-05-13 00:14:29 +02:00
2024-05-12 21:10:52 +02:00
# Wyświetlenie drzewa decyzyjnego
2024-05-13 02:12:36 +02:00
plt.figure(figsize=(20, 20))
plot_tree(model, feature_names=X.columns, class_names=sorted(y.unique()), filled=True)
2024-05-12 21:10:52 +02:00
plt.show()
2024-05-13 00:14:29 +02:00
2024-05-13 02:12:36 +02:00
#Marchew = 1
#zmiemniaki = 2
#pomidor = 3
#salata = 4
#cebula = 5
#Papryka = 6
#Buraki = 7
#Bruksela = 8
#Rzepak = 9
#Szpinak = 10