Phaneendrabayi commited on
Commit
5fb3ff2
Β·
verified Β·
1 Parent(s): f73cf4b

Create Random_Forest_Algorithm.py

Browse files
Files changed (1) hide show
  1. pages/Random_Forest_Algorithm.py +116 -0
pages/Random_Forest_Algorithm.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+
4
+ st.set_page_config(page_title="Random Forest Algorithm Explorer", layout="wide")
5
+
6
+ st.title("🌳 Random Forest Algorithm - Classifier & Regressor")
7
+
8
+ # Topic selector
9
+ view = st.radio("πŸ” Select Task Type", [
10
+ "🌳 Random Forest Classifier",
11
+ "🌳 Random Forest Regressor"
12
+ ], horizontal=True)
13
+
14
+ if view == "🌳 Random Forest Classifier":
15
+ st.header("🌳 Random Forest Classifier - In Depth")
16
+ st.markdown("""
17
+ ## πŸ“˜ What is Random Forest Classifier?
18
+ A Random Forest Classifier is an ensemble learning method that constructs multiple decision trees on various sub-samples of the dataset and uses **majority voting** for classification.
19
+
20
+ ---
21
+ ## πŸ”§ Key Concepts
22
+ - **Bootstrap Aggregation (Bagging)**: Training data is sampled with replacement to build multiple trees.
23
+ - **Feature Randomness**: Each split only considers a random subset of features.
24
+ - **Majority Voting**: Predictions from all trees are combined by majority vote.
25
+
26
+ ---
27
+ ## βœ… Advantages
28
+ - Handles large datasets efficiently.
29
+ - Reduces overfitting compared to a single decision tree.
30
+ - Automatically handles missing values and maintains accuracy.
31
+
32
+ ## ❌ Disadvantages
33
+ - Not interpretable like a decision tree.
34
+ - Can be slow with a large number of trees.
35
+
36
+ ---
37
+ ## πŸ”§ Hyperparameters Explained
38
+
39
+ | Parameter | Description |
40
+ |-----------|-------------|
41
+ | `n_estimators` | Number of trees in the forest (more trees can improve performance but slow down computation). |
42
+ | `criterion` | Splitting metric (`gini`, `entropy`). |
43
+ | `max_depth` | Maximum depth of each tree (limits model complexity). |
44
+ | `min_samples_split` | Minimum samples required to split a node. |
45
+ | `min_samples_leaf` | Minimum samples required in a leaf node. |
46
+ | `max_features` | Number of features considered when splitting a node. |
47
+ | `bootstrap` | Whether bootstrap samples are used. |
48
+ | `oob_score` | Use out-of-bag samples to estimate accuracy. |
49
+
50
+ ---
51
+ ## πŸ§ͺ Optuna for Hyperparameter Tuning (Explanation)
52
+ - `Optuna` is a Python library for hyperparameter optimization using intelligent sampling.
53
+ - It builds a search space, samples combinations, and optimizes performance metrics.
54
+ - Best suited for finding optimal values of:
55
+ - `n_estimators`, `max_depth`
56
+ - `min_samples_split`, `min_samples_leaf`
57
+ - `max_features`
58
+ - Objective: **maximize classification accuracy** or **F1-score** on validation set.
59
+ """)
60
+
61
+ elif view == "🌳 Random Forest Regressor":
62
+ st.header("🌳 Random Forest Regressor - In Depth")
63
+ st.markdown("""
64
+ ## πŸ“˜ What is Random Forest Regressor?
65
+ A Random Forest Regressor is an ensemble learning model that combines predictions from multiple decision trees to produce a **continuous output** by averaging the results.
66
+
67
+ ---
68
+ ## πŸ”§ Key Concepts
69
+ - **Bootstrap Sampling**: Builds trees on random samples.
70
+ - **Feature Subsets**: Random subset of features considered at each split.
71
+ - **Averaging**: Final prediction is the **mean** of predictions from all trees.
72
+
73
+ ---
74
+ ## βœ… Advantages
75
+ - Handles complex, non-linear relationships.
76
+ - Reduces variance and improves generalization.
77
+ - Robust to noise and outliers.
78
+
79
+ ## ❌ Disadvantages
80
+ - Less interpretable.
81
+ - Slower for large datasets.
82
+
83
+ ---
84
+ ## πŸ”§ Hyperparameters Explained
85
+
86
+ | Parameter | Description |
87
+ |-----------|-------------|
88
+ | `n_estimators` | Number of regression trees. |
89
+ | `criterion` | Loss function to minimize (`squared_error`, `absolute_error`). |
90
+ | `max_depth` | Controls overfitting by limiting tree depth. |
91
+ | `min_samples_split` | Minimum samples to split a node. |
92
+ | `min_samples_leaf` | Minimum samples in a terminal node. |
93
+ | `max_features` | Fraction/number of features to consider. |
94
+ | `bootstrap` | Enable/disable bootstrapping. |
95
+ | `oob_score` | Use unused (out-of-bag) data for evaluation. |
96
+
97
+ ---
98
+ ## πŸ§ͺ Optuna for Hyperparameter Tuning (Explanation)
99
+ - `Optuna` helps search for hyperparameters by optimizing a regression loss (e.g., **MAE**, **MSE**, **RMSE**).
100
+ - Works well with Random Forest when tuning:
101
+ - `n_estimators`, `max_depth`
102
+ - `min_samples_split`, `min_samples_leaf`
103
+ - `max_features`
104
+ - Use validation error as objective for better generalization.
105
+
106
+ ---
107
+ ## πŸ“ˆ Use Cases
108
+ - Predicting prices (real estate, stocks)
109
+ - Sales forecasting
110
+ - Weather predictions
111
+ """)
112
+
113
+ st.markdown("""
114
+ ---
115
+ πŸ“Œ **Note**: Always validate model performance using cross-validation. Feature importance from Random Forest (`.feature_importances_`) gives insight into what the model has learned.
116
+ """)