Commit
·
c2f5dff
1
Parent(s):
a7e91f1
Upload app files
Browse filesUpload app.py file and images needed by the application.
- .gitattributes +1 -0
- Iris_flower_dimensions.jpg +0 -0
- app.py +99 -0
- iris_dataset_info.png +3 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
iris_dataset_info.png filter=lfs diff=lfs merge=lfs -text
|
Iris_flower_dimensions.jpg
ADDED
![]() |
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Code source: Gaël Varoquaux
|
2 |
+
# License: BSD 3 clause
|
3 |
+
|
4 |
+
# This code is a MOD with Gradio Demo
|
5 |
+
import numpy as np
|
6 |
+
import matplotlib.pyplot as plt
|
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)
|
19 |
+
|
20 |
+
## PCA
|
21 |
+
def PCA_Pred(x1, x2, x3, x4):
|
22 |
+
#Load Data from iris dataset:
|
23 |
+
iris = datasets.load_iris()
|
24 |
+
X = iris.data
|
25 |
+
y = iris.target
|
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)
|
33 |
+
|
34 |
+
#Fit model and transform (decrease dimensions) iris dataset:
|
35 |
+
pca.fit(X)
|
36 |
+
X = pca.transform(X)
|
37 |
+
|
38 |
+
#Set labels to data clusters
|
39 |
+
for name, label in [("Setosa", 0), ("Versicolour", 1), ("Virginica", 2)]:
|
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 |
+
ax.scatter(pc_output[0, 0], pc_output[0, 1], pc_output[0, 2], c='r', marker='*')
|
57 |
+
|
58 |
+
ax.xaxis.set_ticklabels([])
|
59 |
+
ax.yaxis.set_ticklabels([])
|
60 |
+
ax.zaxis.set_ticklabels([])
|
61 |
+
|
62 |
+
return [pc_output, fig]
|
63 |
+
|
64 |
+
title = "🌺 PCA example with Iris Data-set"
|
65 |
+
with gr.Blocks(title=title) as demo:
|
66 |
+
gr.Markdown(f"## {title}")
|
67 |
+
gr.Markdown(
|
68 |
+
"""
|
69 |
+
## The following app is a demo for PCA decomposition. It takes 4 dimensions as input, in reference \
|
70 |
+
to the Iris flower image (left), and returns the transformed first 3 principal components (feature \
|
71 |
+
reduction) taken from a pre-trained model with Iris dataset (Right).
|
72 |
+
""")
|
73 |
+
with gr.Row():
|
74 |
+
with gr.Column():
|
75 |
+
html1 = (
|
76 |
+
"<div >"
|
77 |
+
"<img src='file/iris_flower_dimensions.jpg' width='597' height='460' alt='image One'>"
|
78 |
+
+ "</div>"
|
79 |
+
)
|
80 |
+
gr.HTML(html1)
|
81 |
+
inp1 = gr.Slider(0, 5, value=1, step=0.1, label="Sepal Length (cm)")
|
82 |
+
inp2 = gr.Slider(0, 5, value=1, step=0.1, label="Sepal Width (cm)")
|
83 |
+
inp3 = gr.Slider(0, 5, value=1, step=0.1, label="Petal Length (cm)")
|
84 |
+
inp4 = gr.Slider(0, 5, value=1, step=0.1, label="Petal Width (cm)")
|
85 |
+
output = gr.Textbox(label="PCA Axes")
|
86 |
+
with gr.Column():
|
87 |
+
html2 = (
|
88 |
+
"<div >"
|
89 |
+
"<img src='file/iris_dataset_info.png' alt='image two'>"
|
90 |
+
+ "</div>"
|
91 |
+
)
|
92 |
+
gr.HTML(html2)
|
93 |
+
plot = gr.Plot(label="PCA 3D Space")
|
94 |
+
|
95 |
+
Reduction = gr.Button("PCA Transform")
|
96 |
+
Reduction.click(fn=PCA_Pred, inputs=[inp1, inp2, inp3, inp4], outputs=[output, plot])
|
97 |
+
demo.load(fn=PCA_Pred, inputs=[inp1, inp2, inp3, inp4], outputs=[output, plot])
|
98 |
+
|
99 |
+
demo.launch()
|
iris_dataset_info.png
ADDED
![]() |
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
numpy==1.24.2
|
2 |
+
matplotlib==3.7.1
|
3 |
+
scikit-learn==1.2.2
|