49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
import pandas as pd
|
|
from sklearn.tree import DecisionTreeClassifier, plot_tree
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.metrics import accuracy_score
|
|
import matplotlib.pyplot as plt
|
|
headers = ['adult','active_time','ill','season','guests','hunger','wet_food','dry_food']
|
|
# Wczytanie danych
|
|
data = pd.read_csv('dane.csv', header=0)
|
|
X = data[headers]
|
|
Y = data['decision']
|
|
X = pd.get_dummies(data=X, columns=['season'])
|
|
clf = DecisionTreeClassifier(max_depth=6)
|
|
X1, X2, Y1, Y2 = train_test_split(X, Y, train_size=0.8)
|
|
clf = clf.fit(X1, Y1)
|
|
pred = clf.predict(X2)
|
|
accuracy = accuracy_score(Y2, pred)
|
|
print("Dokładność:", accuracy)
|
|
|
|
|
|
|
|
#zapisanie drzewa do pliku
|
|
plt.figure(figsize=(50,30))
|
|
plot_tree(clf, filled=True, feature_names=X.columns.tolist(), class_names=['nie karmi', 'karmi mokrą karmą', 'karmi suchą karmą'])
|
|
plt.savefig('tree.png')
|
|
# dane do decyzji
|
|
def feed_decision(adult,active_time,ill,season,guests,hunger,dry_food,wet_food):
|
|
|
|
X_new = pd.DataFrame({
|
|
'adult': [adult],
|
|
'active_time': [active_time],
|
|
'ill': [ill],
|
|
'season': [season],
|
|
'guests':[guests],
|
|
'hunger': [hunger],
|
|
'wet_food': [wet_food],
|
|
'dry_food': [dry_food]
|
|
})
|
|
X_new = pd.get_dummies(X_new)
|
|
missing_columns = set(X.columns) - set(X_new)
|
|
for col in missing_columns:
|
|
X_new[col] = False
|
|
X_new = X_new.reindex(columns=X.columns, fill_value=0)
|
|
print("Atrybuty zwierzęcia:", adult,active_time,ill,season,guests,hunger,wet_food,dry_food)
|
|
return (clf.predict(X_new))
|
|
|
|
|
|
|
|
|