File size: 4,111 Bytes
da7ddad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56126e3
da7ddad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import streamlit as st

st.set_page_config(page_title="Random Forest", page_icon="๐ŸŒฒ", layout="wide")

# Title
st.markdown("<h1 style='text-align:center;'>๐ŸŒฒ Random Forest Algorithm</h1>", unsafe_allow_html=True)

# Introduction
st.header("๐Ÿ“š What is Random Forest?")
st.markdown("""
Random Forest is a **supervised learning algorithm** used for both **classification** and **regression** tasks.

It works by building a large number of **decision trees** and combining their results to make a final prediction.  
Think of it like a group of people (trees) voting on an answer โ€“ this **reduces overfitting** and improves accuracy.
""")

# Real-world Uses
st.header("๐ŸŽฏ Where is Random Forest Used?")
st.markdown("""
- ๐ŸŒพ Crop disease detection  
- ๐Ÿ“Š Stock market predictions  
- ๐Ÿฅ Medical diagnosis  
- ๐Ÿ’ณ Fraud detection  
- ๐Ÿ‘ฅ Customer churn prediction  
""")

# How it works
st.header("โš™๏ธ How Random Forest Works")

with st.expander("Step 1: Bootstrapping (Sampling with Replacement)"):
    st.markdown("""
    - Randomly select multiple **subsets** of the dataset (with replacement).
    - Each subset is used to train a **separate decision tree**.
    """)

with st.expander("Step 2: Tree Building"):
    st.markdown("""
    - For each decision tree, choose a **random subset of features** at every split.
    - This randomness helps in creating **diverse trees**, reducing correlation.
    """)

with st.expander("Step 3: Aggregating Results"):
    st.markdown("""
    - **Classification**: Majority Voting (most common class wins)  
    - **Regression**: Average of all tree predictions  
    """)

# Visual Illustration
st.header("๐ŸŒณ Visual Intuition")
st.image("https://upload.wikimedia.org/wikipedia/commons/7/76/Random_forest_diagram_complete.png", caption="Random Forest Structure", use_container_width=True)

# Advantages
st.header("โœ… Why Use Random Forest?")
st.markdown("""
- Handles both classification and regression  
- Reduces overfitting compared to a single decision tree  
- Works well with large datasets and high-dimensional data  
- Robust to outliers and missing data  
""")

# Hyperparameters
st.header("๐Ÿ› ๏ธ Key Hyperparameters")

with st.expander("๐ŸŒฒ n_estimators"):
    st.markdown("Number of trees in the forest. More trees = better performance, but more computation.")

with st.expander("๐Ÿงฎ max_depth"):
    st.markdown("Maximum depth of each tree. Controls overfitting.")

with st.expander("๐Ÿ”€ max_features"):
    st.markdown("Number of features to consider at each split (auto, sqrt, log2).")

with st.expander("๐Ÿช“ min_samples_split"):
    st.markdown("Minimum samples required to split a node.")

with st.expander("๐ŸŽฏ criterion"):
    st.markdown("Function to measure the quality of a split (`gini` or `entropy` for classification).")

# Evaluation Metrics
st.header("๐Ÿ“ Evaluation Metrics")

with st.expander("โœ”๏ธ Accuracy"):
    st.latex(r"Accuracy = \frac{TP + TN}{TP + TN + FP + FN}")
    st.markdown("Overall how often the model was correct.")

with st.expander("๐ŸŽฏ Precision"):
    st.latex(r"Precision = \frac{TP}{TP + FP}")
    st.markdown("Out of all predicted positives, how many were actually positive?")

with st.expander("๐Ÿ” Recall"):
    st.latex(r"Recall = \frac{TP}{TP + FN}")
    st.markdown("Out of all actual positives, how many did we correctly identify?")

with st.expander("โš–๏ธ F1 Score"):
    st.latex(r"F1 = 2 \cdot \frac{Precision \cdot Recall}{Precision + Recall}")
    st.markdown("Harmonic mean of precision and recall.")

with st.expander("๐Ÿ“‰ ROC-AUC"):
    st.markdown("Measures the tradeoff between true positive rate and false positive rate.")

# Summary
st.header("๐Ÿ“Œ Summary")
st.markdown("""
- Random Forest is an **ensemble** of decision trees  
- Uses **bagging** and **feature randomness** to create a robust model  
- Great for **accuracy**, **stability**, and **generalization**  
- Handles missing data and avoids overfitting well  
- Best when you need strong baseline performance with minimal tuning  
""")

st.success("๐ŸŽ‰ Now youโ€™ve got a solid grip on Random Forest!")