Mohammad Haizad
commited on
Commit
Β·
c6aadc4
1
Parent(s):
b604531
initial commit
Browse files- README.md +2 -2
- app.py +86 -0
- requirements.txt +2 -0
README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
emoji: π
|
4 |
colorFrom: green
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
|
|
1 |
---
|
2 |
+
title: Comparing random forests and the multi-output meta estimator
|
3 |
emoji: π
|
4 |
colorFrom: green
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.27.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from sklearn.ensemble import RandomForestRegressor
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
from sklearn.multioutput import MultiOutputRegressor
|
6 |
+
import matplotlib
|
7 |
+
matplotlib.use("Agg")
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
|
10 |
+
def compare(max_depth,n_estimators):
|
11 |
+
rng = np.random.RandomState(1)
|
12 |
+
X = np.sort(200 * rng.rand(600, 1) - 100, axis=0)
|
13 |
+
y = np.array([np.pi * np.sin(X).ravel(), np.pi * np.cos(X).ravel()]).T
|
14 |
+
y += 0.5 - rng.rand(*y.shape)
|
15 |
+
|
16 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
17 |
+
X, y, train_size=400, test_size=200, random_state=4
|
18 |
+
)
|
19 |
+
|
20 |
+
regr_multirf = MultiOutputRegressor(
|
21 |
+
RandomForestRegressor(n_estimators=n_estimators, max_depth=max_depth, random_state=0)
|
22 |
+
)
|
23 |
+
regr_multirf.fit(X_train, y_train)
|
24 |
+
|
25 |
+
regr_rf = RandomForestRegressor(n_estimators=n_estimators, max_depth=max_depth, random_state=2)
|
26 |
+
regr_rf.fit(X_train, y_train)
|
27 |
+
|
28 |
+
# Predict on new data
|
29 |
+
y_multirf = regr_multirf.predict(X_test)
|
30 |
+
y_rf = regr_rf.predict(X_test)
|
31 |
+
|
32 |
+
# Plot the results
|
33 |
+
fig, ax = plt.subplots()
|
34 |
+
s = 50
|
35 |
+
a = 0.4
|
36 |
+
ax.scatter(
|
37 |
+
y_test[:, 0],
|
38 |
+
y_test[:, 1],
|
39 |
+
edgecolor="k",
|
40 |
+
c="navy",
|
41 |
+
s=s,
|
42 |
+
marker="s",
|
43 |
+
alpha=a,
|
44 |
+
label="Data",
|
45 |
+
)
|
46 |
+
ax.scatter(
|
47 |
+
y_multirf[:, 0],
|
48 |
+
y_multirf[:, 1],
|
49 |
+
edgecolor="k",
|
50 |
+
c="cornflowerblue",
|
51 |
+
s=s,
|
52 |
+
alpha=a,
|
53 |
+
label="Multi RF score=%.2f" % regr_multirf.score(X_test, y_test),
|
54 |
+
)
|
55 |
+
ax.scatter(
|
56 |
+
y_rf[:, 0],
|
57 |
+
y_rf[:, 1],
|
58 |
+
edgecolor="k",
|
59 |
+
c="c",
|
60 |
+
s=s,
|
61 |
+
marker="^",
|
62 |
+
alpha=a,
|
63 |
+
label="RF score=%.2f" % regr_rf.score(X_test, y_test),
|
64 |
+
)
|
65 |
+
ax.set_xlim([-6, 6])
|
66 |
+
ax.set_ylim([-6, 6])
|
67 |
+
ax.set_xlabel("target 1")
|
68 |
+
ax.set_ylabel("target 2")
|
69 |
+
ax.set_title("Comparing random forests and the multi-output meta estimator")
|
70 |
+
ax.legend()
|
71 |
+
return fig
|
72 |
+
|
73 |
+
title = "Comparing random forests and the multi-output meta estimator"
|
74 |
+
with gr.Blocks(title=title) as demo:
|
75 |
+
gr.Markdown(f"## {title}")
|
76 |
+
gr.Markdown("This app demonstrates random forests and the multi-output meta estimator comparison")
|
77 |
+
|
78 |
+
max_depth = gr.Slider(minimum=10, maximum=50, step=1, label = "Maximum Depth")
|
79 |
+
n_estimators = gr.Slider(minimum=50, maximum=300, step=1, label = "Number of Estimators")
|
80 |
+
|
81 |
+
plot = gr.Plot(label=title)
|
82 |
+
n_estimators.change(fn=compare, inputs=[max_depth,n_estimators], outputs=[plot])
|
83 |
+
max_depth.change(fn=compare, inputs=[max_depth,n_estimators], outputs=[plot])
|
84 |
+
|
85 |
+
demo.launch()
|
86 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
scikit-learn==1.2.2
|
2 |
+
matplotlib==3.7.1
|