Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,14 +17,17 @@ def generate_2d_regression(n_points, noise):
|
|
| 17 |
|
| 18 |
def generate_3d_regression(n_points, noise):
|
| 19 |
n_points = int(n_points)
|
|
|
|
| 20 |
x1 = np.linspace(0, 10, n_points)
|
| 21 |
x2 = np.linspace(0, 10, n_points)
|
| 22 |
X1, X2 = np.meshgrid(x1, x2)
|
|
|
|
| 23 |
Z = 3 * X1 + 2 * X2 + 10 + np.random.randn(*X1.shape) * noise
|
| 24 |
|
| 25 |
X_flat = np.column_stack((X1.ravel(), X2.ravel()))
|
| 26 |
Z_flat = Z.ravel()
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
def generate_classification(n_points, noise):
|
|
@@ -52,7 +55,6 @@ def rf_2d_view(n_points, noise, n_estimators, max_depth):
|
|
| 52 |
mse = mean_squared_error(y, y_pred)
|
| 53 |
|
| 54 |
fig = go.Figure()
|
| 55 |
-
|
| 56 |
fig.add_scatter(x=X.flatten(), y=y, mode="markers", name="Data")
|
| 57 |
fig.add_scatter(x=X.flatten(), y=y_pred, mode="lines", name="RF Prediction")
|
| 58 |
|
|
@@ -67,14 +69,14 @@ def rf_2d_view(n_points, noise, n_estimators, max_depth):
|
|
| 67 |
|
| 68 |
|
| 69 |
# ------------------------------------------------
|
| 70 |
-
# 3D RANDOM FOREST REGRESSION (ROTATING
|
| 71 |
# ------------------------------------------------
|
| 72 |
|
| 73 |
def rf_3d_view(n_points, noise, n_estimators, max_depth):
|
| 74 |
n_estimators = int(n_estimators)
|
| 75 |
max_depth = int(max_depth)
|
| 76 |
|
| 77 |
-
|
| 78 |
|
| 79 |
rf = RandomForestRegressor(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
|
| 80 |
rf.fit(X_flat, Z_flat)
|
|
@@ -83,18 +85,18 @@ def rf_3d_view(n_points, noise, n_estimators, max_depth):
|
|
| 83 |
mse = mean_squared_error(Z_flat, Z_pred.ravel())
|
| 84 |
|
| 85 |
fig = go.Figure()
|
| 86 |
-
|
| 87 |
fig.add_surface(x=X1, y=X2, z=Z_pred, colorscale="Blues", opacity=0.9)
|
| 88 |
|
| 89 |
-
# Smooth
|
| 90 |
frames = []
|
| 91 |
for angle in range(0, 360, 10):
|
| 92 |
-
frames.append(go.Frame(layout=dict(scene_camera=dict(
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
| 98 |
fig.frames = frames
|
| 99 |
|
| 100 |
fig.update_layout(
|
|
@@ -105,10 +107,11 @@ def rf_3d_view(n_points, noise, n_estimators, max_depth):
|
|
| 105 |
updatemenus=[dict(
|
| 106 |
type="buttons",
|
| 107 |
showactive=False,
|
| 108 |
-
buttons=[dict(
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
| 112 |
)]
|
| 113 |
)
|
| 114 |
|
|
@@ -116,7 +119,7 @@ def rf_3d_view(n_points, noise, n_estimators, max_depth):
|
|
| 116 |
|
| 117 |
|
| 118 |
# ------------------------------------------------
|
| 119 |
-
# CLASSIFICATION VIEW
|
| 120 |
# ------------------------------------------------
|
| 121 |
|
| 122 |
def classification_view(n_points, noise, n_estimators, max_depth):
|
|
@@ -128,21 +131,19 @@ def classification_view(n_points, noise, n_estimators, max_depth):
|
|
| 128 |
rf = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
|
| 129 |
rf.fit(X, y)
|
| 130 |
|
| 131 |
-
#
|
| 132 |
xx, yy = np.meshgrid(
|
| 133 |
np.linspace(X[:, 0].min() - 1, X[:, 0].max() + 1, 100),
|
| 134 |
np.linspace(X[:, 1].min() - 1, X[:, 1].max() + 1, 100)
|
| 135 |
)
|
| 136 |
|
| 137 |
Z = rf.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)
|
| 138 |
-
|
| 139 |
acc = accuracy_score(y, rf.predict(X))
|
| 140 |
|
| 141 |
fig = go.Figure()
|
| 142 |
-
|
| 143 |
fig.add_contour(x=xx[0], y=yy[:, 0], z=Z, showscale=False, opacity=0.4, colorscale="Blues")
|
| 144 |
-
|
| 145 |
-
|
| 146 |
|
| 147 |
fig.update_layout(
|
| 148 |
title="Random Forest Classification Boundary",
|
|
@@ -155,7 +156,7 @@ def classification_view(n_points, noise, n_estimators, max_depth):
|
|
| 155 |
|
| 156 |
|
| 157 |
# ------------------------------------------------
|
| 158 |
-
# GRADIO UI
|
| 159 |
# ------------------------------------------------
|
| 160 |
|
| 161 |
with gr.Blocks() as demo:
|
|
@@ -195,4 +196,4 @@ with gr.Blocks() as demo:
|
|
| 195 |
[plot2d, mse2d, plot3d, mse3d, plot_cls, acc_cls])
|
| 196 |
|
| 197 |
|
| 198 |
-
demo.launch(theme=gr.themes.Soft(primary_hue="blue"))
|
|
|
|
| 17 |
|
| 18 |
def generate_3d_regression(n_points, noise):
|
| 19 |
n_points = int(n_points)
|
| 20 |
+
|
| 21 |
x1 = np.linspace(0, 10, n_points)
|
| 22 |
x2 = np.linspace(0, 10, n_points)
|
| 23 |
X1, X2 = np.meshgrid(x1, x2)
|
| 24 |
+
|
| 25 |
Z = 3 * X1 + 2 * X2 + 10 + np.random.randn(*X1.shape) * noise
|
| 26 |
|
| 27 |
X_flat = np.column_stack((X1.ravel(), X2.ravel()))
|
| 28 |
Z_flat = Z.ravel()
|
| 29 |
+
|
| 30 |
+
return X1, X2, X_flat, Z_flat
|
| 31 |
|
| 32 |
|
| 33 |
def generate_classification(n_points, noise):
|
|
|
|
| 55 |
mse = mean_squared_error(y, y_pred)
|
| 56 |
|
| 57 |
fig = go.Figure()
|
|
|
|
| 58 |
fig.add_scatter(x=X.flatten(), y=y, mode="markers", name="Data")
|
| 59 |
fig.add_scatter(x=X.flatten(), y=y_pred, mode="lines", name="RF Prediction")
|
| 60 |
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
# ------------------------------------------------
|
| 72 |
+
# 3D RANDOM FOREST REGRESSION (ROTATING)
|
| 73 |
# ------------------------------------------------
|
| 74 |
|
| 75 |
def rf_3d_view(n_points, noise, n_estimators, max_depth):
|
| 76 |
n_estimators = int(n_estimators)
|
| 77 |
max_depth = int(max_depth)
|
| 78 |
|
| 79 |
+
X1, X2, X_flat, Z_flat = generate_3d_regression(n_points, noise)
|
| 80 |
|
| 81 |
rf = RandomForestRegressor(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
|
| 82 |
rf.fit(X_flat, Z_flat)
|
|
|
|
| 85 |
mse = mean_squared_error(Z_flat, Z_pred.ravel())
|
| 86 |
|
| 87 |
fig = go.Figure()
|
|
|
|
| 88 |
fig.add_surface(x=X1, y=X2, z=Z_pred, colorscale="Blues", opacity=0.9)
|
| 89 |
|
| 90 |
+
# Smooth rotation frames
|
| 91 |
frames = []
|
| 92 |
for angle in range(0, 360, 10):
|
| 93 |
+
frames.append(go.Frame(layout=dict(scene_camera=dict(
|
| 94 |
+
eye=dict(
|
| 95 |
+
x=np.cos(np.radians(angle)) * 2,
|
| 96 |
+
y=np.sin(np.radians(angle)) * 2,
|
| 97 |
+
z=1.2
|
| 98 |
+
)
|
| 99 |
+
))))
|
| 100 |
fig.frames = frames
|
| 101 |
|
| 102 |
fig.update_layout(
|
|
|
|
| 107 |
updatemenus=[dict(
|
| 108 |
type="buttons",
|
| 109 |
showactive=False,
|
| 110 |
+
buttons=[dict(
|
| 111 |
+
label="Rotate 3D",
|
| 112 |
+
method="animate",
|
| 113 |
+
args=[None, {"frame": {"duration": 60, "redraw": True}, "fromcurrent": True}]
|
| 114 |
+
)]
|
| 115 |
)]
|
| 116 |
)
|
| 117 |
|
|
|
|
| 119 |
|
| 120 |
|
| 121 |
# ------------------------------------------------
|
| 122 |
+
# CLASSIFICATION VIEW
|
| 123 |
# ------------------------------------------------
|
| 124 |
|
| 125 |
def classification_view(n_points, noise, n_estimators, max_depth):
|
|
|
|
| 131 |
rf = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
|
| 132 |
rf.fit(X, y)
|
| 133 |
|
| 134 |
+
# decision boundary
|
| 135 |
xx, yy = np.meshgrid(
|
| 136 |
np.linspace(X[:, 0].min() - 1, X[:, 0].max() + 1, 100),
|
| 137 |
np.linspace(X[:, 1].min() - 1, X[:, 1].max() + 1, 100)
|
| 138 |
)
|
| 139 |
|
| 140 |
Z = rf.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)
|
|
|
|
| 141 |
acc = accuracy_score(y, rf.predict(X))
|
| 142 |
|
| 143 |
fig = go.Figure()
|
|
|
|
| 144 |
fig.add_contour(x=xx[0], y=yy[:, 0], z=Z, showscale=False, opacity=0.4, colorscale="Blues")
|
| 145 |
+
fig.add_scatter(x=X[:, 0], y=X[:, 1], mode="markers",
|
| 146 |
+
marker=dict(color=y, colorscale="Blues"), name="Data")
|
| 147 |
|
| 148 |
fig.update_layout(
|
| 149 |
title="Random Forest Classification Boundary",
|
|
|
|
| 156 |
|
| 157 |
|
| 158 |
# ------------------------------------------------
|
| 159 |
+
# GRADIO UI
|
| 160 |
# ------------------------------------------------
|
| 161 |
|
| 162 |
with gr.Blocks() as demo:
|
|
|
|
| 196 |
[plot2d, mse2d, plot3d, mse3d, plot_cls, acc_cls])
|
| 197 |
|
| 198 |
|
| 199 |
+
demo.launch(theme=gr.themes.Soft(primary_hue="blue"))
|