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