Update pages/3_EDA_and_Feature_Engineering.py
Browse files
pages/3_EDA_and_Feature_Engineering.py
CHANGED
|
@@ -1,99 +1,99 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import seaborn as sns
|
| 3 |
import matplotlib.pyplot as plt
|
|
|
|
| 4 |
|
| 5 |
# Page Title
|
| 6 |
-
st.title("Complete EDA
|
| 7 |
|
| 8 |
st.markdown("""
|
| 9 |
-
|
| 10 |
-
This page uses the previously uploaded dataset and provides comprehensive analysis, including advanced visualizations and insights.
|
| 11 |
---
|
| 12 |
""")
|
| 13 |
|
| 14 |
# Check if dataset exists in session state
|
| 15 |
if 'df' in st.session_state:
|
| 16 |
-
|
| 17 |
-
st.success("Dataset loaded successfully
|
| 18 |
|
| 19 |
# Display Dataset Overview
|
| 20 |
st.write("### Dataset Overview")
|
| 21 |
-
st.
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
st.write(f"
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
st.
|
| 96 |
-
st.write(data.describe(include='all'))
|
| 97 |
|
| 98 |
else:
|
| 99 |
-
st.error("No dataset found. Please upload a dataset on the
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
import seaborn as sns
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
+
from sklearn.preprocessing import LabelEncoder, PolynomialFeatures
|
| 6 |
|
| 7 |
# Page Title
|
| 8 |
+
st.title("Complete EDA and Feature Engineering")
|
| 9 |
|
| 10 |
st.markdown("""
|
| 11 |
+
This page provides advanced Exploratory Data Analysis (EDA) and Feature Engineering using the dataset loaded in memory.
|
|
|
|
| 12 |
---
|
| 13 |
""")
|
| 14 |
|
| 15 |
# Check if dataset exists in session state
|
| 16 |
if 'df' in st.session_state:
|
| 17 |
+
df = st.session_state['df']
|
| 18 |
+
st.success("Dataset loaded successfully.")
|
| 19 |
|
| 20 |
# Display Dataset Overview
|
| 21 |
st.write("### Dataset Overview")
|
| 22 |
+
st.dataframe(df.head())
|
| 23 |
+
|
| 24 |
+
# Shape of Dataset
|
| 25 |
+
st.write(f"The dataset has {df.shape[0]} rows and {df.shape[1]} columns.")
|
| 26 |
+
|
| 27 |
+
# Univariate Analysis
|
| 28 |
+
st.write("### Univariate Analysis")
|
| 29 |
+
|
| 30 |
+
# Product Category Distribution
|
| 31 |
+
st.write("#### Product Category Distribution")
|
| 32 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 33 |
+
sns.countplot(x='Category', data=df, palette='viridis', ax=ax)
|
| 34 |
+
ax.set_title("Product Category Distribution")
|
| 35 |
+
ax.set_xlabel("Product Category")
|
| 36 |
+
ax.set_ylabel("Count")
|
| 37 |
+
plt.xticks(rotation=45)
|
| 38 |
+
st.pyplot(fig)
|
| 39 |
+
|
| 40 |
+
# Product Price Distribution
|
| 41 |
+
st.write("#### Product Price Distribution")
|
| 42 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 43 |
+
sns.histplot(df['Price'], kde=True, color='orange', ax=ax)
|
| 44 |
+
ax.set_title("Product Price Distribution")
|
| 45 |
+
ax.set_xlabel("Product Price")
|
| 46 |
+
ax.set_ylabel("Count")
|
| 47 |
+
st.pyplot(fig)
|
| 48 |
+
|
| 49 |
+
# Bivariate Analysis: Price vs Category
|
| 50 |
+
st.write("### Price vs Category")
|
| 51 |
+
fig, ax = plt.subplots(figsize=(12, 8))
|
| 52 |
+
sns.boxplot(x='Category', y='Price', data=df, palette='mako', ax=ax)
|
| 53 |
+
ax.set_title("Product Price by Category")
|
| 54 |
+
ax.set_xlabel("Category")
|
| 55 |
+
ax.set_ylabel("Price")
|
| 56 |
+
plt.xticks(rotation=45)
|
| 57 |
+
st.pyplot(fig)
|
| 58 |
+
|
| 59 |
+
# Feature Engineering
|
| 60 |
+
st.write("### Feature Engineering")
|
| 61 |
+
|
| 62 |
+
# Binning Product Price
|
| 63 |
+
st.write("#### Product Price Binning")
|
| 64 |
+
df['PriceBucket'] = pd.cut(df['Price'], bins=[100, 500, 1000, 1500, 2000, 3000],
|
| 65 |
+
labels=['Very Low', 'Low', 'Medium', 'High', 'Very High'])
|
| 66 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 67 |
+
sns.countplot(x='PriceBucket', data=df, palette='icefire', ax=ax)
|
| 68 |
+
ax.set_title("Product Price Buckets")
|
| 69 |
+
ax.set_xlabel("Price Bucket")
|
| 70 |
+
ax.set_ylabel("Count")
|
| 71 |
+
st.pyplot(fig)
|
| 72 |
+
|
| 73 |
+
# Encoding Categorical Features
|
| 74 |
+
st.write("#### Encoding Categorical Features")
|
| 75 |
+
le = LabelEncoder()
|
| 76 |
+
df['Category'] = le.fit_transform(df['Category'])
|
| 77 |
+
df['Brand'] = le.fit_transform(df['Brand'])
|
| 78 |
+
st.write("Label Encoding Applied on 'Category' and 'Brand'.")
|
| 79 |
+
|
| 80 |
+
# Polynomial Features for Numerical Columns
|
| 81 |
+
st.write("#### Polynomial Feature Engineering")
|
| 82 |
+
numeric_columns = df.select_dtypes(include=['int64', 'float64']).columns
|
| 83 |
+
poly = PolynomialFeatures(degree=2, include_bias=False)
|
| 84 |
+
poly_features = poly.fit_transform(df[numeric_columns])
|
| 85 |
+
poly_df = pd.DataFrame(poly_features, columns=poly.get_feature_names_out(numeric_columns))
|
| 86 |
+
|
| 87 |
+
st.write("Polynomial Features Created:")
|
| 88 |
+
st.dataframe(poly_df.head())
|
| 89 |
+
|
| 90 |
+
# Correlation Heatmap
|
| 91 |
+
st.write("### Correlation Matrix")
|
| 92 |
+
corr = df.corr()
|
| 93 |
+
fig, ax = plt.subplots(figsize=(12, 8))
|
| 94 |
+
sns.heatmap(corr, annot=True, cmap='coolwarm', ax=ax)
|
| 95 |
+
ax.set_title("Correlation Matrix")
|
| 96 |
+
st.pyplot(fig)
|
|
|
|
| 97 |
|
| 98 |
else:
|
| 99 |
+
st.error("No dataset found. Please upload a dataset on the main page first.")
|