# Code examples from: Decision Trees in Python: A Model You Can Actually Read
# https://www.atulvij.com/decision-tree-scikit-learn
# Review and adapt before running; examples use sample values.

# ---- Example 1 (python) ----
# pip install scikit-learn pandas
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, export_text

X, y = load_iris(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42, stratify=y)

tree = DecisionTreeClassifier(max_depth=3, random_state=42).fit(X_train, y_train)
print("test accuracy:", round(tree.score(X_test, y_test), 3))
print(export_text(tree, feature_names=list(X.columns)))

# ---- Example 2 (text) ----
|--- petal length (cm) <= 2.45
|   |--- class: 0
|--- petal length (cm) >  2.45
|   |--- petal width (cm) <= 1.75
|   |   |--- class: 1

# ---- Example 3 (python) ----
for depth in [1, 2, 3, 5, None]:
    t = DecisionTreeClassifier(max_depth=depth, random_state=42).fit(X_train, y_train)
    print(depth, "train", round(t.score(X_train, y_train), 3), "test", round(t.score(X_test, y_test), 3))

# ---- Example 4 (python) ----
import pandas as pd
df = pd.read_excel("loans.xlsx")                 # e.g. Income, Age, Existing_EMI, Approved
X = pd.get_dummies(df.drop(columns="Approved"))  # text columns -> 0/1 columns
y = df["Approved"]

# ---- Example 5 (python) ----
for name, imp in sorted(zip(X.columns, tree.feature_importances_), key=lambda t: -t[1]):
    print(f"{name:25s} {imp:.2f}")

