import streamlit as st st.set_page_config(page_title="Decision Tree: Classification & Regression") st.title("๐ŸŒณ Understanding Decision Tree Algorithms") # Button-like radio selector view = st.radio("๐Ÿงช Select Topic Mode", ["๐Ÿ“Œ About Decision Tree Classification", "๐Ÿ“Œ About Decision Tree Regression"], horizontal=True) if view == "๐Ÿ“Œ About Decision Tree Classification": st.header("๐Ÿง  Decision Tree Classification") st.markdown(""" Decision Tree Classification is a **supervised learning algorithm** used for classifying data by learning simple decision rules inferred from the data features. ### ๐Ÿ” How Decision Tree Classification Works: 1. Select the **best feature** to split the dataset based on a criterion (e.g., Gini impurity or entropy). 2. Split the dataset into subsets. 3. Repeat recursively for each subset until the stopping condition is met (e.g., max depth, minimum samples). ### โœ… Example Use Cases: - Medical diagnosis (disease prediction) - Customer churn prediction - Loan approval classification ### ๐Ÿ”ง Important Parameters in `DecisionTreeClassifier`: - **`criterion`**: Metric to evaluate the quality of a split: - `'gini'`: Gini Impurity (default) - `'entropy'`: Information Gain - **`max_depth`**: Maximum depth of the tree - **`min_samples_split`**: Minimum number of samples to split an internal node - **`min_samples_leaf`**: Minimum number of samples in a leaf node - **`max_features`**: Number of features to consider when looking for the best split - **`random_state`**: Controls the randomness of the estimator ### ๐Ÿ“Š Pros: - Easy to understand and interpret - Requires little data preparation - Handles both numerical and categorical data ### โš ๏ธ Cons: - Prone to overfitting - Unstable with small data changes - Biased with imbalanced datasets ### ๐Ÿ”Ž Hyperparameter Tuning with Optuna (Example with Iris Dataset): ```python import optuna from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split, cross_val_score from sklearn.tree import DecisionTreeClassifier # Load and split data data = load_iris() X_train, _, y_train, _ = train_test_split(data.data, data.target, test_size=0.2, random_state=42) def objective(trial): model = DecisionTreeClassifier( criterion=trial.suggest_categorical('criterion', ['gini', 'entropy']), max_depth=trial.suggest_int('max_depth', 1, 20), min_samples_split=trial.suggest_int('min_samples_split', 2, 20), min_samples_leaf=trial.suggest_int('min_samples_leaf', 1, 20), max_features=trial.suggest_categorical('max_features', ['auto', 'sqrt', 'log2', None]), random_state=42 ) return cross_val_score(model, X_train, y_train, cv=5).mean() study = optuna.create_study(direction='maximize') study.optimize(objective, n_trials=50) ``` """) elif view == "๐Ÿ“Œ About Decision Tree Regression": st.header("๐Ÿ“Š Decision Tree Regression") st.markdown(""" Decision Tree Regression is used for **predicting continuous values** by learning decision rules that split data to minimize variance. ### ๐Ÿ” How Decision Tree Regression Works: 1. The dataset is split based on a feature that minimizes variance (MSE). 2. These splits form decision nodes and terminal leaves with average target values. 3. Recursively build the tree until the stopping condition is reached. ### โœ… Example Use Cases: - Predicting housing prices - Estimating demand or consumption - Financial forecasting ### ๐Ÿ”ง Important Parameters in `DecisionTreeRegressor`: - **`criterion`**: Function to measure the quality of a split: - `'squared_error'`, `'absolute_error'`, `'friedman_mse'`, `'poisson'` - **`max_depth`**, **`min_samples_split`**, **`min_samples_leaf`**, **`max_features`**, **`random_state`** (same as classifier) ### ๐Ÿ“Š Pros: - Simple and non-linear relationships can be captured - No need for feature scaling - Flexible to feature types ### โš ๏ธ Cons: - Overfitting is common without pruning - Sensitive to outliers and noise ### ๐Ÿ”Ž Hyperparameter Tuning with Optuna (Example with California Housing Dataset): ```python import optuna from sklearn.datasets import fetch_california_housing from sklearn.model_selection import train_test_split, cross_val_score from sklearn.tree import DecisionTreeRegressor # Load and split data data = fetch_california_housing() X_train, _, y_train, _ = train_test_split(data.data, data.target, test_size=0.2, random_state=42) def objective(trial): model = DecisionTreeRegressor( criterion=trial.suggest_categorical('criterion', ['squared_error', 'friedman_mse', 'absolute_error']), max_depth=trial.suggest_int('max_depth', 1, 20), min_samples_split=trial.suggest_int('min_samples_split', 2, 20), min_samples_leaf=trial.suggest_int('min_samples_leaf', 1, 20), max_features=trial.suggest_categorical('max_features', ['auto', 'sqrt', 'log2', None]), random_state=42 ) return cross_val_score(model, X_train, y_train, cv=5, scoring='neg_mean_squared_error').mean() study = optuna.create_study(direction='maximize') study.optimize(objective, n_trials=50) ``` """) st.markdown(""" --- ## ๐Ÿง  Classification vs Regression Summary | Feature | Decision Tree Classification | Decision Tree Regression | |--------------------|----------------------------------|------------------------------------| | Output Type | Class label (categorical) | Numeric value (continuous) | | Decision Mechanism | Gini/Entropy-based Splits | MSE/MAE-based Splits | | Metrics | Accuracy, F1, ROC-AUC | RMSE, MAE, Rยฒ Score | --- ## ๐Ÿงช Tips for Both Models - **Prune your tree** with max depth or minimum samples. - **Use GridSearchCV or Optuna** for hyperparameter tuning. - **Be cautious of overfitting**, especially on small datasets. - **Visualize trees** with `plot_tree` from `sklearn.tree`. --- โœจ Decision Trees offer an intuitive, powerful approach to both classification and regression โ€” especially when tuned and interpreted effectively! """)