Spaces:
Sleeping
Sleeping
Update pages/Random_Forest.py
Browse files- pages/Random_Forest.py +111 -0
pages/Random_Forest.py
CHANGED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
st.set_page_config(page_title="Random Forest", page_icon="๐ฒ", layout="wide")
|
| 4 |
+
|
| 5 |
+
# Title
|
| 6 |
+
st.markdown("<h1 style='text-align:center;'>๐ฒ Random Forest Algorithm</h1>", unsafe_allow_html=True)
|
| 7 |
+
|
| 8 |
+
# Introduction
|
| 9 |
+
st.header("๐ What is Random Forest?")
|
| 10 |
+
st.markdown("""
|
| 11 |
+
Random Forest is a **supervised learning algorithm** used for both **classification** and **regression** tasks.
|
| 12 |
+
|
| 13 |
+
It works by building a large number of **decision trees** and combining their results to make a final prediction.
|
| 14 |
+
Think of it like a group of people (trees) voting on an answer โ this **reduces overfitting** and improves accuracy.
|
| 15 |
+
""")
|
| 16 |
+
|
| 17 |
+
# Real-world Uses
|
| 18 |
+
st.header("๐ฏ Where is Random Forest Used?")
|
| 19 |
+
st.markdown("""
|
| 20 |
+
- ๐พ Crop disease detection
|
| 21 |
+
- ๐ Stock market predictions
|
| 22 |
+
- ๐ฅ Medical diagnosis
|
| 23 |
+
- ๐ณ Fraud detection
|
| 24 |
+
- ๐ฅ Customer churn prediction
|
| 25 |
+
""")
|
| 26 |
+
|
| 27 |
+
# How it works
|
| 28 |
+
st.header("โ๏ธ How Random Forest Works")
|
| 29 |
+
|
| 30 |
+
with st.expander("Step 1: Bootstrapping (Sampling with Replacement)"):
|
| 31 |
+
st.markdown("""
|
| 32 |
+
- Randomly select multiple **subsets** of the dataset (with replacement).
|
| 33 |
+
- Each subset is used to train a **separate decision tree**.
|
| 34 |
+
""")
|
| 35 |
+
|
| 36 |
+
with st.expander("Step 2: Tree Building"):
|
| 37 |
+
st.markdown("""
|
| 38 |
+
- For each decision tree, choose a **random subset of features** at every split.
|
| 39 |
+
- This randomness helps in creating **diverse trees**, reducing correlation.
|
| 40 |
+
""")
|
| 41 |
+
|
| 42 |
+
with st.expander("Step 3: Aggregating Results"):
|
| 43 |
+
st.markdown("""
|
| 44 |
+
- **Classification**: Majority Voting (most common class wins)
|
| 45 |
+
- **Regression**: Average of all tree predictions
|
| 46 |
+
""")
|
| 47 |
+
|
| 48 |
+
# Visual Illustration
|
| 49 |
+
st.header("๐ณ Visual Intuition")
|
| 50 |
+
st.image("https://upload.wikimedia.org/wikipedia/commons/7/76/Random_forest_diagram_complete.png", caption="Random Forest Structure", use_column_width=True)
|
| 51 |
+
|
| 52 |
+
# Advantages
|
| 53 |
+
st.header("โ
Why Use Random Forest?")
|
| 54 |
+
st.markdown("""
|
| 55 |
+
- Handles both classification and regression
|
| 56 |
+
- Reduces overfitting compared to a single decision tree
|
| 57 |
+
- Works well with large datasets and high-dimensional data
|
| 58 |
+
- Robust to outliers and missing data
|
| 59 |
+
""")
|
| 60 |
+
|
| 61 |
+
# Hyperparameters
|
| 62 |
+
st.header("๐ ๏ธ Key Hyperparameters")
|
| 63 |
+
|
| 64 |
+
with st.expander("๐ฒ n_estimators"):
|
| 65 |
+
st.markdown("Number of trees in the forest. More trees = better performance, but more computation.")
|
| 66 |
+
|
| 67 |
+
with st.expander("๐งฎ max_depth"):
|
| 68 |
+
st.markdown("Maximum depth of each tree. Controls overfitting.")
|
| 69 |
+
|
| 70 |
+
with st.expander("๐ max_features"):
|
| 71 |
+
st.markdown("Number of features to consider at each split (auto, sqrt, log2).")
|
| 72 |
+
|
| 73 |
+
with st.expander("๐ช min_samples_split"):
|
| 74 |
+
st.markdown("Minimum samples required to split a node.")
|
| 75 |
+
|
| 76 |
+
with st.expander("๐ฏ criterion"):
|
| 77 |
+
st.markdown("Function to measure the quality of a split (`gini` or `entropy` for classification).")
|
| 78 |
+
|
| 79 |
+
# Evaluation Metrics
|
| 80 |
+
st.header("๐ Evaluation Metrics")
|
| 81 |
+
|
| 82 |
+
with st.expander("โ๏ธ Accuracy"):
|
| 83 |
+
st.latex(r"Accuracy = \frac{TP + TN}{TP + TN + FP + FN}")
|
| 84 |
+
st.markdown("Overall how often the model was correct.")
|
| 85 |
+
|
| 86 |
+
with st.expander("๐ฏ Precision"):
|
| 87 |
+
st.latex(r"Precision = \frac{TP}{TP + FP}")
|
| 88 |
+
st.markdown("Out of all predicted positives, how many were actually positive?")
|
| 89 |
+
|
| 90 |
+
with st.expander("๐ Recall"):
|
| 91 |
+
st.latex(r"Recall = \frac{TP}{TP + FN}")
|
| 92 |
+
st.markdown("Out of all actual positives, how many did we correctly identify?")
|
| 93 |
+
|
| 94 |
+
with st.expander("โ๏ธ F1 Score"):
|
| 95 |
+
st.latex(r"F1 = 2 \cdot \frac{Precision \cdot Recall}{Precision + Recall}")
|
| 96 |
+
st.markdown("Harmonic mean of precision and recall.")
|
| 97 |
+
|
| 98 |
+
with st.expander("๐ ROC-AUC"):
|
| 99 |
+
st.markdown("Measures the tradeoff between true positive rate and false positive rate.")
|
| 100 |
+
|
| 101 |
+
# Summary
|
| 102 |
+
st.header("๐ Summary")
|
| 103 |
+
st.markdown("""
|
| 104 |
+
- Random Forest is an **ensemble** of decision trees
|
| 105 |
+
- Uses **bagging** and **feature randomness** to create a robust model
|
| 106 |
+
- Great for **accuracy**, **stability**, and **generalization**
|
| 107 |
+
- Handles missing data and avoids overfitting well
|
| 108 |
+
- Best when you need strong baseline performance with minimal tuning
|
| 109 |
+
""")
|
| 110 |
+
|
| 111 |
+
st.success("๐ Now youโve got a solid grip on Random Forest!")
|