from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
import matplotlib.pyplot as plt
# 1. Data [Tinggi, Berat] -> [Gender: 0=Pria, 1=Wanita]
X = [[180, 80], [175, 75], [160, 50], [155, 45]]
y = ['Pria', 'Pria', 'Wanita', 'Wanita']
# 2. Model
# criterion='gini' (default) atau 'entropy'
clf = DecisionTreeClassifier(criterion='gini', max_depth=3)
# 3. Training
clf.fit(X, y)
# 4. Prediksi (170cm, 60kg)
print("Prediksi:", clf.predict([[170, 60]]))
# 5. Visualisasi Teks
text_representation = tree.export_text(clf)
print(text_representation)
# Output:
# |--- feature_1 <= 62.50
# | |--- class: Wanita
# |--- feature_1 > 62.50
# | |--- class: Pria