Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import time
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
from sklearn.datasets import load_iris
|
| 7 |
+
from sklearn.decomposition import PCA, IncrementalPCA
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
theme = gr.themes.Monochrome(
|
| 11 |
+
primary_hue="indigo",
|
| 12 |
+
secondary_hue="blue",
|
| 13 |
+
neutral_hue="slate",
|
| 14 |
+
)
|
| 15 |
+
model_card = f"""
|
| 16 |
+
## Description
|
| 17 |
+
|
| 18 |
+
**Incremental principal component analysis (IPCA)** is a suitable alternative to **Principal component analysis (PCA)** when the dataset to be analyzed is too large to fit in memory.
|
| 19 |
+
**IPCA** generates a low-rank representation of the input data utilizing a fixed amount of memory that is not reliant on the number of input data samples.
|
| 20 |
+
|
| 21 |
+
In this demo, you can play around with different ``number of components`` and ``number of samples`` to explore the performance of IPCA and PCA, including a comparison of their respective outputs and running times.
|
| 22 |
+
**Note**: Incremental PCA is comparatively slower to regular PCA, as it processes partial data sets sequentially.
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
## Dataset
|
| 26 |
+
|
| 27 |
+
Iris dataset
|
| 28 |
+
"""
|
| 29 |
+
iris = load_iris()
|
| 30 |
+
X = iris.data
|
| 31 |
+
y = iris.target
|
| 32 |
+
|
| 33 |
+
def plot_pca(n_components, batch_size):
|
| 34 |
+
# Create linkage matrix and then plot the dendrogram
|
| 35 |
+
colors = ["navy", "turquoise", "darkorange"]
|
| 36 |
+
|
| 37 |
+
ipca = IncrementalPCA(n_components=n_components, batch_size=batch_size)
|
| 38 |
+
t1 = time.time()
|
| 39 |
+
X_ipca = ipca.fit_transform(X)
|
| 40 |
+
ipca_time = time.time() - t1
|
| 41 |
+
|
| 42 |
+
pca = PCA(n_components=n_components)
|
| 43 |
+
t2 = time.time()
|
| 44 |
+
X_pca = pca.fit_transform(X)
|
| 45 |
+
pca_time = time.time() - t2
|
| 46 |
+
|
| 47 |
+
fig1, axes1 = plt.subplots()
|
| 48 |
+
for color, i, target_name in zip(colors, [0, 1, 2], iris.target_names):
|
| 49 |
+
axes1.scatter(
|
| 50 |
+
X_ipca[y == i, 0],
|
| 51 |
+
X_ipca[y == i, 1],
|
| 52 |
+
color=color,
|
| 53 |
+
lw=2,
|
| 54 |
+
label=target_name,
|
| 55 |
+
)
|
| 56 |
+
err = np.abs(np.abs(X_pca) - np.abs(X_ipca)).mean()
|
| 57 |
+
axes1.set_title(f"Incremental PCA of iris dataset")
|
| 58 |
+
axes1.axis([-4, 4, -1.5, 1.5])
|
| 59 |
+
axes1.legend(loc="best", shadow=False, scatterpoints=1)
|
| 60 |
+
|
| 61 |
+
fig2, axes2 = plt.subplots()
|
| 62 |
+
for color, i, target_name in zip(colors, [0, 1, 2], iris.target_names):
|
| 63 |
+
axes2.scatter(
|
| 64 |
+
X_pca[y == i, 0],
|
| 65 |
+
X_pca[y == i, 1],
|
| 66 |
+
color=color,
|
| 67 |
+
lw=2,
|
| 68 |
+
label=target_name,
|
| 69 |
+
)
|
| 70 |
+
axes2.set_title("PCA of iris dataset")
|
| 71 |
+
axes2.axis([-4, 4, -1.5, 1.5])
|
| 72 |
+
axes2.legend(loc="best", shadow=False, scatterpoints=1)
|
| 73 |
+
|
| 74 |
+
text = f"PCA runing time : {pca_time:.6f} seconds. Incremental PCA runing time : {ipca_time:.6f} seconds. Mean absolute unsigned error {err*100:.6f}%"
|
| 75 |
+
|
| 76 |
+
return fig1, fig2, text
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
with gr.Blocks(theme=theme) as demo:
|
| 81 |
+
gr.Markdown('''
|
| 82 |
+
<div>
|
| 83 |
+
<h1 style='text-align: center'>Incremental PCA</h1>
|
| 84 |
+
</div>
|
| 85 |
+
''')
|
| 86 |
+
gr.Markdown(model_card)
|
| 87 |
+
gr.Markdown("Author: <a href=\"https://huggingface.co/vumichien\">Vu Minh Chien</a>. Based on the example from <a href=\"https://scikit-learn.org/stable/auto_examples/decomposition/plot_incremental_pca.html#sphx-glr-auto-examples-decomposition-plot-incremental-pca-py\">scikit-learn</a>")
|
| 88 |
+
n_components = gr.Slider(minimum=2, maximum=4, step=1, value=2, label="Number of components to keep")
|
| 89 |
+
batch_size = gr.Slider(minimum=10, maximum=50, step=10, value=10, label="The number of samples to use for each batch")
|
| 90 |
+
|
| 91 |
+
with gr.Row():
|
| 92 |
+
with gr.Column():
|
| 93 |
+
plot_1 = gr.Plot(label="Incremental PCA")
|
| 94 |
+
with gr.Column():
|
| 95 |
+
plot_2 = gr.Plot(label="PCA")
|
| 96 |
+
with gr.Row():
|
| 97 |
+
resutls = gr.Textbox(label="Results")
|
| 98 |
+
|
| 99 |
+
n_components.change(fn=plot_pca, inputs=[n_components, batch_size], outputs=[plot_1, plot_2, resutls])
|
| 100 |
+
batch_size.change(fn=plot_pca, inputs=[n_components, batch_size], outputs=[plot_1, plot_2, resutls])
|
| 101 |
+
|
| 102 |
+
demo.launch()
|