Font size
  • A-
  • A
  • A+
Site color
  • R
  • A
  • A
  • A
Skip to main content
AI4VET AI4VET
  • Home
  • Calendar
  • More
You are currently using guest access
Log in
AI4VET
Home Calendar
Expand all Collapse all
  1. AI/ML Fundamentals
  2. AIML-SR
  3. 3. Modeli učenja (SR)
  4. Vežba 7: Stablo odlučivanja

Vežba 7: Stablo odlučivanja

colab

Evo jednostavne vežbe Jupiter Notebook za učenike da nauče o stablima odlučivanja. Ova vežba će ih voditi kroz učitavanje skupa podataka, obuku modela stabla odlučivanja i procenu njegovog učinka.


Vežba: Klasifikacija pomoću stabla odlučivanja

Cilj: Učitajte skup podataka. Obučite model stabla odlučivanja. Procenite performanse modela.

Preduslovi:Instalirajte biblioteku scikit-learn. Koristite jednostavan skup podataka kao što je Iris skup podataka.

Korak 1: Instalirajte potrebne biblioteke:

%pip install scikit-learn

Korak 2: Uvoz biblioteka:

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn import tree

Korak 3: Učitajte skup podataka:

# Load the Iris dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target

Korak 4: Podijelite skup podataka:

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

Korak 5: Obučite model stabla odlučivanja:

# Initialize and train the decision tree model
model = DecisionTreeClassifier()
model.fit(X_train, y_train)

Korak 6: Napravite predviđanja:

# Make predictions on the test set
y_pred = model.predict(X_test)

Korak 7: Procenite model:

# Evaluate the model's performance
accuracy = accuracy_score(y_test, y_pred)
cm = confusion_matrix(y_test, y_pred)
cr = classification_report(y_test, y_pred)
print(f'Accuracy: {accuracy}')
print('Confusion Matrix:')
print(cm)
print('Classification Report:')
print(cr)

Korak 8: Vizualizujte stablo odlučivanja:

# Visualize the decision tree
plt.figure(figsize=(20,10))
tree.plot_tree(model, filled=True, feature_names=iris.feature_names, class_names=iris.target_names, rounded=True)
plt.show()

Uputstvo za studente:

1. Pratite korake da biste instalirali potrebne biblioteke i učitali skup podataka.

2. Obučite model stabla odlučivanja i procenite njegove performanse.

3. Izmenite kod da biste koristili različite skupove podataka ili dodali više funkcija modelu.

4. Istražite uticaj različitih hiperparametara na performanse modela.

Completion requirements:
  • Make a submission
Previous activity Vežba 6: Logistička regresija
Next activity Vežba 8: Algoritam k-najbližih suseda
You are currently using guest access (Log in)
Data retention summary
Get the mobile app
Get the mobile app
Play Store App Store
Powered by Moodle

This theme was proudly developed by

Conecti.me