MulaiMulai sekarang secara gratis

Decision trees vs. neural networks

Build a decision tree classifier to classify income levels based on multiple features including age, education level, and hours worked per week, and extract the learned rules that explain the decision. Then, compare its performance with an MLPClassifier trained on the same data.

X_train, X_test, y_train, and y_test are pre-loaded for you. The accuracy_score and export_text functions are also imported for you.

Latihan ini adalah bagian dari kursus

Explainable AI in Python

Lihat Kursus

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

model = DecisionTreeClassifier(random_state=42, max_depth=2)
model.fit(X_train, y_train)

# Extract the rules
rules = ____
print(rules)

y_pred = model.predict(X_test)

# Compute accuracy
accuracy = ____
print(f"Accuracy: {accuracy:.2f}")
Edit dan Jalankan Kode