selva1909 commited on
Commit
114cfce
·
verified ·
1 Parent(s): 6cdcbd9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+ from sklearn.ensemble import RandomForestRegressor
5
+ from sklearn.metrics import mean_squared_error
6
+ from mpl_toolkits.mplot3d import Axes3D
7
+
8
+ # ------------------------------------------------
9
+ # Random Forest 2D
10
+ # ------------------------------------------------
11
+ def rf_2d(n_points, noise, n_estimators, max_depth):
12
+ X = np.linspace(0, 10, n_points).reshape(-1, 1)
13
+ y = 2.5 * X.flatten() + 5 + np.random.randn(n_points) * noise
14
+
15
+ model = RandomForestRegressor(
16
+ n_estimators=n_estimators,
17
+ max_depth=max_depth,
18
+ random_state=42
19
+ )
20
+ model.fit(X, y)
21
+ y_pred = model.predict(X)
22
+
23
+ mse = mean_squared_error(y, y_pred)
24
+
25
+ fig, ax = plt.subplots(figsize=(5, 4))
26
+ ax.scatter(X, y, s=20, color="orange", label="Data")
27
+ ax.plot(X, y_pred, color="blue", linewidth=2, label="RF Prediction")
28
+ ax.set_title("2D Random Forest Regression")
29
+ ax.legend()
30
+
31
+ return fig, f"MSE: {mse:.4f}"
32
+
33
+
34
+ # ------------------------------------------------
35
+ # Random Forest 3D
36
+ # ------------------------------------------------
37
+ def rf_3d(n_points, noise, n_estimators, max_depth):
38
+ x1 = np.linspace(0, 10, n_points)
39
+ x2 = np.linspace(0, 10, n_points)
40
+ X1, X2 = np.meshgrid(x1, x2)
41
+
42
+ Z = 3 * X1 + 2 * X2 + 10 + np.random.randn(*X1.shape) * noise
43
+
44
+ X_flat = np.column_stack((X1.ravel(), X2.ravel()))
45
+ Z_flat = Z.ravel()
46
+
47
+ model = RandomForestRegressor(
48
+ n_estimators=n_estimators,
49
+ max_depth=max_depth,
50
+ random_state=42
51
+ )
52
+ model.fit(X_flat, Z_flat)
53
+
54
+ Z_pred = model.predict(X_flat).reshape(X1.shape)
55
+ mse = mean_squared_error(Z_flat, Z_pred.ravel())
56
+
57
+ fig = plt.figure(figsize=(5, 4))
58
+ ax = fig.add_subplot(111, projection="3d")
59
+
60
+ idx = np.random.choice(len(Z_flat), 400, replace=False)
61
+ ax.scatter(
62
+ X_flat[idx, 0],
63
+ X_flat[idx, 1],
64
+ Z_flat[idx],
65
+ s=8,
66
+ alpha=0.3,
67
+ color="orange"
68
+ )
69
+
70
+ ax.plot_surface(X1, X2, Z_pred, alpha=0.7, color="blue")
71
+ ax.set_title("3D Random Forest Surface")
72
+
73
+ return fig, f"MSE: {mse:.4f}"
74
+
75
+
76
+ # ------------------------------------------------
77
+ # Gradio UI
78
+ # ------------------------------------------------
79
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
80
+ gr.Markdown(
81
+ """
82
+ # 🌲 Random Forest Regression Visualizer
83
+ Interactive **2D & 3D Random Forest** playground
84
+ """
85
+ )
86
+
87
+ with gr.Row():
88
+ mode = gr.Radio(
89
+ ["2D Regression", "3D Regression"],
90
+ value="2D Regression",
91
+ label="Mode"
92
+ )
93
+
94
+ with gr.Row():
95
+ n_points = gr.Slider(20, 200, value=80, step=10, label="Data Points")
96
+ noise = gr.Slider(0.0, 5.0, value=1.0, label="Noise Level")
97
+
98
+ with gr.Row():
99
+ n_estimators = gr.Slider(10, 200, value=50, step=10, label="Trees")
100
+ max_depth = gr.Slider(2, 20, value=8, step=1, label="Max Depth")
101
+
102
+ run_btn = gr.Button("🌲 Train Random Forest")
103
+
104
+ plot = gr.Plot()
105
+ metric = gr.Markdown()
106
+
107
+ def run(mode, n_points, noise, n_estimators, max_depth):
108
+ if mode == "2D Regression":
109
+ return rf_2d(n_points, noise, n_estimators, max_depth)
110
+ else:
111
+ return rf_3d(n_points, noise, n_estimators, max_depth)
112
+
113
+ run_btn.click(
114
+ run,
115
+ inputs=[mode, n_points, noise, n_estimators, max_depth],
116
+ outputs=[plot, metric]
117
+ )
118
+
119
+ demo.launch()