whackthejacker/DataHubHub
1
1import streamlit as st2import pandas as pd3import numpy as np4import plotly.express as px5import plotly.graph_objects as go6 7def render_dataset_statistics(dataset, dataset_type):8 """9 Renders statistical analysis of the dataset.10 11 Args:12 dataset: The dataset to analyze (pandas DataFrame)13 dataset_type: The type of dataset (csv, json, etc.)14 """15 if dataset is None:16 st.warning("No dataset to analyze.")17 return18 19 st.markdown("<h3>Dataset Statistics</h3>", unsafe_allow_html=True)20 21 # Tabs for different kinds of statistics22 tab1, tab2, tab3 = st.tabs(["Summary Statistics", "Distribution Analysis", "Correlation Analysis"])23 24 with tab1:25 # Summary statistics26 st.markdown("### Summary Statistics")27 28 # Filter only numeric columns for statistics29 numeric_cols = dataset.select_dtypes(include=[np.number]).columns.tolist()30 31 if numeric_cols:32 # Display summary statistics33 st.dataframe(dataset[numeric_cols].describe().T.style.highlight_max(axis=1, color='#FFD21E'), use_container_width=True)34 35 # Top values for categorical columns36 categorical_cols = dataset.select_dtypes(exclude=[np.number]).columns.tolist()37 if categorical_cols:38 st.markdown("### Category Value Counts")39 selected_cat_col = st.selectbox("Select categorical column", categorical_cols)40 41 # Show top values and their counts42 value_counts = dataset[selected_cat_col].value_counts().head(10)43 fig = px.bar(44 x=value_counts.index, 45 y=value_counts.values,46 title=f"Top 10 values in {selected_cat_col}",47 labels={"x": selected_cat_col, "y": "Count"},48 color_discrete_sequence=["#2563EB"]49 )50 st.plotly_chart(fig, use_container_width=True)51 else:52 st.warning("No numeric columns found in the dataset.")53 54 with tab2:55 # Distribution analysis56 st.markdown("### Distribution Analysis")57 58 if numeric_cols:59 selected_num_col = st.selectbox("Select numeric column", numeric_cols)60 61 # Create distribution plot62 fig = px.histogram(63 dataset, 64 x=selected_num_col,65 title=f"Distribution of {selected_num_col}",66 marginal="box",67 color_discrete_sequence=["#FFD21E"],68 template="simple_white"69 )70 st.plotly_chart(fig, use_container_width=True)71 72 # Basic distribution stats73 col1, col2, col3, col4 = st.columns(4)74 with col1:75 st.metric("Mean", f"{dataset[selected_num_col].mean():.2f}")76 with col2:77 st.metric("Median", f"{dataset[selected_num_col].median():.2f}")78 with col3:79 st.metric("Min", f"{dataset[selected_num_col].min():.2f}")80 with col4:81 st.metric("Max", f"{dataset[selected_num_col].max():.2f}")82 else:83 st.warning("No numeric columns found in the dataset.")84 85 with tab3:86 # Correlation analysis87 st.markdown("### Correlation Analysis")88 89 if len(numeric_cols) > 1:90 # Compute correlation matrix91 corr_matrix = dataset[numeric_cols].corr()92 93 # Plot heatmap94 fig = px.imshow(95 corr_matrix,96 color_continuous_scale=["#84919A", "#FFFFFF", "#FFD21E"],97 title="Correlation Matrix",98 template="simple_white"99 )100 st.plotly_chart(fig, use_container_width=True)101 102 # Top correlated features103 st.markdown("### Top Correlated Features")104 105 # Convert correlation matrix to a long format106 corr_pairs = []107 for i in range(len(corr_matrix.columns)):108 for j in range(i+1, len(corr_matrix.columns)):109 col1 = corr_matrix.columns[i]110 col2 = corr_matrix.columns[j]111 corr_value = corr_matrix.iloc[i, j]112 corr_pairs.append((col1, col2, corr_value))113 114 # Sort by absolute correlation115 corr_pairs.sort(key=lambda x: abs(x[2]), reverse=True)116 117 # Display top 10 correlated pairs118 if corr_pairs:119 top_pairs = pd.DataFrame(corr_pairs[:10], columns=["Feature 1", "Feature 2", "Correlation"])120 st.dataframe(121 top_pairs.style.format({122 "Correlation": "{:.4f}"123 }).background_gradient(subset=["Correlation"], cmap="coolwarm"),124 use_container_width=True125 )126 127 # Scatter plot for the top correlated pair128 if corr_pairs:129 top_pair = corr_pairs[0]130 fig = px.scatter(131 dataset, 132 x=top_pair[0], 133 y=top_pair[1],134 title=f"Scatter plot: {top_pair[0]} vs {top_pair[1]} (Corr: {top_pair[2]:.4f})",135 color_discrete_sequence=["#2563EB"],136 template="simple_white"137 )138 fig.add_traces(139 go.Scatter(140 x=[None], 141 y=[None],142 mode='lines',143 line=dict(color="#FFD21E", width=3),144 name='Best Fit'145 )146 )147 st.plotly_chart(fig, use_container_width=True)148 else:149 st.warning("Need at least two numeric columns for correlation analysis.")150 