Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +60 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
|
| 6 |
+
from sklearn.preprocessing import StandardScaler
|
| 7 |
+
from sklearn.decomposition import PCA
|
| 8 |
+
|
| 9 |
+
st.title('Principal Component Analysis Demo App 🤖')
|
| 10 |
+
st.subheader('Credit: @BenjaminJack')
|
| 11 |
+
st.subheader('Raw Data')
|
| 12 |
+
|
| 13 |
+
iris = px.data.iris().drop('species_id',axis=1)
|
| 14 |
+
st.write(iris)
|
| 15 |
+
|
| 16 |
+
xvar = st.selectbox('Select x-axis',iris.columns[:-1])
|
| 17 |
+
yvar = st.selectbox('Select y-axis',iris.columns[:-1])
|
| 18 |
+
|
| 19 |
+
st.write(px.scatter(iris, x=xvar, y=yvar, color='species'))
|
| 20 |
+
iris_scaled = StandardScaler().fit_transform(iris.drop('species',axis=1))
|
| 21 |
+
iris_pca = PCA()
|
| 22 |
+
|
| 23 |
+
iris_transformed = iris_pca.fit_transform(iris_scaled)
|
| 24 |
+
col_names = [f'component{i+1}' for i in range(iris_transformed.shape[1])]
|
| 25 |
+
st.write(col_names)
|
| 26 |
+
|
| 27 |
+
iris_transformed_df = pd.DataFrame(iris_transformed, columns=col_names)
|
| 28 |
+
iris_transformed_df = pd.concat([iris_transformed_df, iris['species']],axis=1)
|
| 29 |
+
|
| 30 |
+
st.subheader('Transformed Data')
|
| 31 |
+
|
| 32 |
+
st.write(iris_transformed_df)
|
| 33 |
+
|
| 34 |
+
st.subheader('Explore principal components')
|
| 35 |
+
xvar = st.selectbox('Select x-axis:', iris_transformed_df.columns[:-1])
|
| 36 |
+
yvar = st.selectbox('Select y-axis:', iris_transformed_df.columns[:-1])
|
| 37 |
+
|
| 38 |
+
st.write(px.scatter(iris_transformed_df,x=xvar, y=yvar, color = 'species'))
|
| 39 |
+
|
| 40 |
+
st.subheader('Explore Loadings')
|
| 41 |
+
|
| 42 |
+
loadings = iris_pca.components_.T * np.sqrt(iris_pca.explained_variance_)
|
| 43 |
+
|
| 44 |
+
loadings_df = pd.DataFrame(loadings,columns=col_names)
|
| 45 |
+
loadings_df = pd.concat([loadings_df,
|
| 46 |
+
pd.Series(iris.columns[0:4],name='var')],
|
| 47 |
+
axis=1)
|
| 48 |
+
|
| 49 |
+
component =st.selectbox('Select Component:',loadings_df.columns[0:4])
|
| 50 |
+
|
| 51 |
+
bar_chart = px.bar(loadings_df[['var',component]].sort_values(component),
|
| 52 |
+
y='var',
|
| 53 |
+
x=component,
|
| 54 |
+
orientation='h',
|
| 55 |
+
range_x=[-1,1]
|
| 56 |
+
)
|
| 57 |
+
st.write(bar_chart)
|
| 58 |
+
st.write(loadings_df)
|
| 59 |
+
|
| 60 |
+
#Credits : benjaminjack
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
plotly
|