Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import streamlit as st | |
| import plotly.express as px | |
| import numpy as np | |
| import scipy.stats as stats | |
| # Elegant and Centered Title | |
| st.markdown( | |
| """ | |
| <h1 style='text-align: center; color: red;'>Tips Dataset Explorer</h1> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| # Upload Section | |
| file = st.file_uploader("๐ Upload a CSV file", type=["csv"]) | |
| if file is not None: | |
| df = pd.read_csv(file) | |
| st.markdown("### ๐งพ Preview of the Dataset:") | |
| st.write(df.head()) | |
| categorical_cols = df.select_dtypes(include='object').columns.tolist() | |
| numerical_cols = df.select_dtypes(include=['int64', 'float64']).columns.tolist() | |
| st.markdown("## ๐ Univariate Analysis") | |
| # ========================== # | |
| # CATEGORICAL PART # | |
| # ========================== # | |
| st.markdown("### ๐จ Categorical Feature Analysis") | |
| selected_cat = st.selectbox("Select a categorical feature", categorical_cols, key='cat_feature') | |
| if selected_cat: | |
| col_data = df[selected_cat] | |
| st.write(f"**Feature: `{selected_cat}`**") | |
| st.write(f"๐ข Unique values: {col_data.unique()}") | |
| st.write(f"๐งฎ Number of unique values: {col_data.nunique()}") | |
| st.write(f"๐ Mode: {col_data.mode()[0]}") | |
| st.write(f"โ Missing values: {col_data.isnull().sum()}") | |
| value_counts = col_data.value_counts() | |
| proportion = (value_counts / len(df)).round(3) * 100 | |
| st.write("๐ Frequency Table:") | |
| freq_table = pd.DataFrame({ 'Count': value_counts, 'Percentage (%)': proportion }) | |
| st.write(freq_table) | |
| # Entropy | |
| entropy = stats.entropy(value_counts) | |
| st.write(f"๐ง Entropy (uncertainty in distribution): `{entropy:.3f}`") | |
| # Charts | |
| st.write("๐ Bar Chart:") | |
| st.bar_chart(value_counts) | |
| if st.checkbox("๐ Show Pie Chart", key="pie_cat"): | |
| fig_pie = px.pie(names=value_counts.index, values=value_counts.values, | |
| title=f'Distribution of {selected_cat}') | |
| st.plotly_chart(fig_pie) | |
| # Cross-tab Heatmap | |
| other_cat = st.selectbox("Compare with another categorical feature", | |
| [col for col in categorical_cols if col != selected_cat], key='cat_cross') | |
| if other_cat: | |
| cross_tab = pd.crosstab(df[selected_cat], df[other_cat]) | |
| st.markdown(f"### ๐ Cross-tabulation: `{selected_cat}` vs `{other_cat}`") | |
| st.dataframe(cross_tab.style.background_gradient(cmap='viridis')) | |
| # ========================== # | |
| # NUMERICAL PART # | |
| # ========================== # | |
| st.markdown("### ๐ฆ Numerical Feature Analysis") | |
| selected_num = st.selectbox("Select a numerical feature", numerical_cols, key='num_feature') | |
| if selected_num: | |
| col_data = df[selected_num].dropna() | |
| st.write(f"**Feature: `{selected_num}`**") | |
| st.write(f"๐ Mean: {col_data.mean():.2f}") | |
| st.write(f"๐ช Median: {col_data.median():.2f}") | |
| st.write(f"๐ Min: {col_data.min()} | ๐ Max: {col_data.max()}") | |
| st.write(f"๐ Std: {col_data.std():.2f} | ๐ Variance: {col_data.var():.2f}") | |
| st.write(f"๐ Skewness: {col_data.skew():.2f} | ๐บ Kurtosis: {col_data.kurt():.2f}") | |
| st.write(f"โ Missing Values: {df[selected_num].isnull().sum()}") | |
| # Quartiles | |
| Q1 = col_data.quantile(0.25) | |
| Q2 = col_data.quantile(0.50) | |
| Q3 = col_data.quantile(0.75) | |
| IQR = Q3 - Q1 | |
| st.write(f"๐ข Quartiles: Q1={Q1:.2f}, Q2={Q2:.2f}, Q3={Q3:.2f}, IQR={IQR:.2f}") | |
| # Outliers based on Z-score | |
| z_scores = np.abs(stats.zscore(col_data)) | |
| outliers = (z_scores > 3).sum() | |
| st.write(f"๐จ Outliers (Z-score > 3): {outliers}") | |
| # Distribution Plot | |
| st.write("๐ Distribution (Histogram):") | |
| fig_hist = px.histogram(df, x=selected_num, nbins=30, title=f'Distribution of {selected_num}') | |
| st.plotly_chart(fig_hist) | |
| # Box Plot | |
| st.write("๐ฆ Box Plot:") | |
| fig_box = px.box(df, y=selected_num, title=f'Box Plot of {selected_num}') | |
| st.plotly_chart(fig_box) | |
| # KDE Plot Option | |
| if st.checkbox("๐ Show KDE (Density Estimation)", key='kde'): | |
| fig_kde = px.histogram(df, x=selected_num, nbins=30, marginal="violin", title=f'KDE of {selected_num}') | |
| st.plotly_chart(fig_kde) | |
| # Binning | |
| if st.checkbox("๐ง Show Value Bins", key='bins'): | |
| bins = st.slider("Number of bins", min_value=2, max_value=20, value=5) | |
| binned_col = pd.cut(col_data, bins=bins) | |
| bin_counts = binned_col.value_counts().sort_index() | |
| st.write(bin_counts) | |
| fig_bin_pie = px.pie(names=bin_counts.index.astype(str), values=bin_counts.values, | |
| title=f'Binned Distribution of {selected_num}') | |
| st.plotly_chart(fig_bin_pie) | |
| # Log Transformation Preview | |
| if st.checkbox("๐ Preview Log Transformation", key='log_transform'): | |
| df['log_' + selected_num] = np.log1p(col_data) | |
| fig_log = px.histogram(df, x='log_' + selected_num, nbins=30, | |
| title=f'Log-Transformed Histogram of {selected_num}') | |
| st.plotly_chart(fig_log) | |
| # Summary Table | |
| st.markdown("### ๐ Summary Statistics") | |
| st.dataframe(df[numerical_cols].describe().T.style.background_gradient(cmap='Blues')) | |
| else: | |
| st.warning("๐ Please upload a CSV file to begin analysis.") | |