Commit
·
3f45c69
1
Parent(s):
e328c80
Update app.py with plotly figures
Browse filesIn this example push code with plotly figures
app.py
CHANGED
@@ -3,16 +3,11 @@
|
|
3 |
|
4 |
# This code is a MOD with Gradio Demo
|
5 |
import numpy as np
|
6 |
-
import
|
7 |
-
import matplotlib
|
8 |
|
9 |
from sklearn import decomposition
|
10 |
from sklearn import datasets
|
11 |
|
12 |
-
# unused but required import for doing 3d projections with matplotlib < 3.2
|
13 |
-
import mpl_toolkits.mplot3d # noqa: F401
|
14 |
-
matplotlib.use('agg')
|
15 |
-
|
16 |
import gradio as gr
|
17 |
|
18 |
np.random.seed(5)
|
@@ -22,11 +17,8 @@ def PCA_Pred(x1, x2, x3, x4):
|
|
22 |
#Load Data from iris dataset:
|
23 |
iris = datasets.load_iris()
|
24 |
X = iris.data
|
25 |
-
|
26 |
-
|
27 |
-
fig, ax = plt.subplots(1, subplot_kw={'projection': '3d', 'elev': 48, 'azim': 134})
|
28 |
-
ax.set_position([0, 0, 0.95, 1])
|
29 |
-
plt.cla()
|
30 |
|
31 |
#Create the model with 3 principal components:
|
32 |
pca = decomposition.PCA(n_components=3)
|
@@ -35,29 +27,45 @@ def PCA_Pred(x1, x2, x3, x4):
|
|
35 |
pca.fit(X)
|
36 |
X = pca.transform(X)
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
-
ax.text3D(
|
41 |
-
X[y == label, 0].mean(),
|
42 |
-
X[y == label, 1].mean() + 1.5,
|
43 |
-
X[y == label, 2].mean(),
|
44 |
-
name,
|
45 |
-
horizontalalignment="center",
|
46 |
-
bbox=dict(alpha=0.5, edgecolor="w", facecolor="w"),
|
47 |
-
)
|
48 |
-
# Reorder the labels to have colors matching the cluster results
|
49 |
-
y = np.choose(y, [1, 2, 0]).astype(float)
|
50 |
-
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=y, cmap=plt.cm.nipy_spectral, edgecolor="k")
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
user_iris_data = np.array([[x1, x2, x3, x4]], ndmin=2)
|
53 |
|
54 |
#Perform reduction to user data
|
55 |
pc_output = pca.transform(user_iris_data)
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
return [pc_output, fig]
|
63 |
|
|
|
3 |
|
4 |
# This code is a MOD with Gradio Demo
|
5 |
import numpy as np
|
6 |
+
import plotly.graph_objects as go
|
|
|
7 |
|
8 |
from sklearn import decomposition
|
9 |
from sklearn import datasets
|
10 |
|
|
|
|
|
|
|
|
|
11 |
import gradio as gr
|
12 |
|
13 |
np.random.seed(5)
|
|
|
17 |
#Load Data from iris dataset:
|
18 |
iris = datasets.load_iris()
|
19 |
X = iris.data
|
20 |
+
Y = iris.target
|
21 |
+
label_data = [("Setosa", 0), ("Versicolour", 1), ("Virginica", 2)]
|
|
|
|
|
|
|
22 |
|
23 |
#Create the model with 3 principal components:
|
24 |
pca = decomposition.PCA(n_components=3)
|
|
|
27 |
pca.fit(X)
|
28 |
X = pca.transform(X)
|
29 |
|
30 |
+
#Create figure with plotly
|
31 |
+
fig = go.Figure()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
for name, label in label_data:
|
34 |
+
fig.add_trace(go.Scatter3d(
|
35 |
+
x=X[Y == label, 0],
|
36 |
+
y=X[Y == label, 1],
|
37 |
+
z=X[Y == label, 2],
|
38 |
+
mode='markers',
|
39 |
+
marker=dict(
|
40 |
+
size=8,
|
41 |
+
color=label,
|
42 |
+
colorscale='Viridis',
|
43 |
+
opacity=0.8),
|
44 |
+
name=name
|
45 |
+
))
|
46 |
+
|
47 |
user_iris_data = np.array([[x1, x2, x3, x4]], ndmin=2)
|
48 |
|
49 |
#Perform reduction to user data
|
50 |
pc_output = pca.transform(user_iris_data)
|
51 |
+
fig.add_traces([go.Scatter3d(
|
52 |
+
x=np.array(pc_output[0, 0]),
|
53 |
+
y=np.array(pc_output[0, 1]),
|
54 |
+
z=np.array(pc_output[0, 2]),
|
55 |
+
mode='markers',
|
56 |
+
marker=dict(
|
57 |
+
size=12,
|
58 |
+
color=4, # set color
|
59 |
+
colorscale='Viridis', # choose a colorscale
|
60 |
+
opacity=0.8),
|
61 |
+
name="User data"
|
62 |
+
)])
|
63 |
+
fig.update_layout(scene = dict(
|
64 |
+
xaxis_title="1st PCA Axis",
|
65 |
+
yaxis_title="2nd PCA Axis",
|
66 |
+
zaxis_title="3th PCA Axis"),
|
67 |
+
legend_title="Species"
|
68 |
+
)
|
69 |
|
70 |
return [pc_output, fig]
|
71 |
|