Spaces:
Sleeping
Sleeping
upload files
Browse files- app.py +121 -0
- requirements.txt +5 -0
- winequality-red.csv +0 -0
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
from sklearn.decomposition import PCA
|
| 7 |
+
from sklearn.preprocessing import StandardScaler
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
import seaborn as sns
|
| 10 |
+
|
| 11 |
+
# Streamlit page setup
|
| 12 |
+
st.set_page_config(page_title="PCA Explorer - Wine Quality", page_icon="π·", layout="wide")
|
| 13 |
+
|
| 14 |
+
# Title and short description
|
| 15 |
+
st.title("π· Principal Component Analysis (PCA) on Wine Quality Dataset")
|
| 16 |
+
st.write("""
|
| 17 |
+
This app demonstrates **Principal Component Analysis (PCA)** for dimensionality reduction and visualization of the **Wine Quality Dataset**.
|
| 18 |
+
""")
|
| 19 |
+
|
| 20 |
+
# Load Wine Quality dataset (local file)
|
| 21 |
+
@st.cache_data
|
| 22 |
+
def load_data():
|
| 23 |
+
data = pd.read_csv("winequality-red.csv") # Make sure the dataset is named correctly
|
| 24 |
+
return data
|
| 25 |
+
|
| 26 |
+
data = load_data()
|
| 27 |
+
|
| 28 |
+
# Sidebar settings
|
| 29 |
+
st.sidebar.header("Settings")
|
| 30 |
+
n_components = st.sidebar.slider("Select number of PCA components", 2, min(data.shape[1], 10), 2)
|
| 31 |
+
|
| 32 |
+
# Features selection (all numeric columns except 'quality')
|
| 33 |
+
features = data.drop(columns=['quality'])
|
| 34 |
+
|
| 35 |
+
# Standardize the data
|
| 36 |
+
scaler = StandardScaler()
|
| 37 |
+
scaled_features = scaler.fit_transform(features)
|
| 38 |
+
|
| 39 |
+
# Perform PCA
|
| 40 |
+
pca = PCA(n_components=n_components)
|
| 41 |
+
principal_components = pca.fit_transform(scaled_features)
|
| 42 |
+
|
| 43 |
+
# Create DataFrame for PCA result
|
| 44 |
+
pca_df = pd.DataFrame(
|
| 45 |
+
data=principal_components,
|
| 46 |
+
columns=[f"PC{i+1}" for i in range(n_components)]
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Add the 'quality' column to the PCA DataFrame
|
| 50 |
+
pca_df['Quality'] = data['quality']
|
| 51 |
+
|
| 52 |
+
# Tabs
|
| 53 |
+
tab1, tab2, tab3, tab4 = st.tabs(["π Raw Dataset", "π PCA Scatter Plot", "π Explained Variance", "π₯ Download Reduced Dataset"])
|
| 54 |
+
|
| 55 |
+
with tab1:
|
| 56 |
+
st.subheader("π Raw Dataset")
|
| 57 |
+
st.dataframe(data)
|
| 58 |
+
|
| 59 |
+
with tab2:
|
| 60 |
+
st.subheader("π PCA Scatter Plot")
|
| 61 |
+
if n_components >= 2:
|
| 62 |
+
fig, ax = plt.subplots()
|
| 63 |
+
sns.scatterplot(
|
| 64 |
+
x="PC1",
|
| 65 |
+
y="PC2",
|
| 66 |
+
data=pca_df,
|
| 67 |
+
hue="Quality",
|
| 68 |
+
palette="viridis",
|
| 69 |
+
s=70,
|
| 70 |
+
edgecolor="black",
|
| 71 |
+
alpha=0.7
|
| 72 |
+
)
|
| 73 |
+
ax.set_xlabel("Principal Component 1")
|
| 74 |
+
ax.set_ylabel("Principal Component 2")
|
| 75 |
+
ax.set_title("PCA - First Two Components")
|
| 76 |
+
st.pyplot(fig)
|
| 77 |
+
|
| 78 |
+
st.write("""
|
| 79 |
+
The scatter plot above shows how the wine samples are distributed in the space of the first two principal components.
|
| 80 |
+
Points are colored based on their **wine quality**, which ranges from 3 (poor) to 8 (excellent).
|
| 81 |
+
- **Clusters**: Notice how wines of similar quality tend to group together in the plot.
|
| 82 |
+
- **Separation**: High-quality wines (higher quality scores) tend to be more spread out, while lower-quality wines are often more tightly clustered.
|
| 83 |
+
""")
|
| 84 |
+
|
| 85 |
+
else:
|
| 86 |
+
st.warning("Please select at least 2 components to plot a scatter plot.")
|
| 87 |
+
|
| 88 |
+
with tab3:
|
| 89 |
+
st.subheader("π Explained Variance Ratio")
|
| 90 |
+
exp_var = pca.explained_variance_ratio_
|
| 91 |
+
fig2, ax2 = plt.subplots()
|
| 92 |
+
sns.barplot(x=[f"PC{i+1}" for i in range(n_components)], y=exp_var, color="skyblue", ax=ax2)
|
| 93 |
+
ax2.set_ylabel('Explained Variance Ratio')
|
| 94 |
+
ax2.set_xlabel('Principal Components')
|
| 95 |
+
ax2.set_title('Variance Explained by Each Principal Component')
|
| 96 |
+
st.pyplot(fig2)
|
| 97 |
+
|
| 98 |
+
st.markdown(f"**Total Variance Explained:** {np.sum(exp_var):.2f}")
|
| 99 |
+
st.write("""
|
| 100 |
+
The bar plot shows the **explained variance ratio** of each principal component.
|
| 101 |
+
- **Higher variance** means that component carries more information.
|
| 102 |
+
- In this case, the first few components explain the majority of the variance in the dataset, with later components contributing less.
|
| 103 |
+
- By selecting fewer components, we reduce dimensionality but still retain most of the data's information.
|
| 104 |
+
""")
|
| 105 |
+
|
| 106 |
+
with tab4:
|
| 107 |
+
st.subheader("π₯ Download Reduced Dataset")
|
| 108 |
+
st.write("You can download the PCA-reduced dataset as a CSV file.")
|
| 109 |
+
|
| 110 |
+
# Create a CSV for the PCA-reduced data
|
| 111 |
+
pca_reduced = pca_df.to_csv(index=False)
|
| 112 |
+
st.download_button(
|
| 113 |
+
label="Download PCA Reduced Data",
|
| 114 |
+
data=pca_reduced,
|
| 115 |
+
file_name="pca_reduced_wine_quality.csv",
|
| 116 |
+
mime="text/csv"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Footer
|
| 120 |
+
st.markdown("---")
|
| 121 |
+
st.caption("Made with β€οΈ using Streamlit")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
scikit-learn
|
| 4 |
+
seaborn
|
| 5 |
+
matplotlib
|
winequality-red.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|