File size: 11,453 Bytes
ea9f040 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
import streamlit as st
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from Utility.data_loader import load_train_series,load_train_events,load_sample_submission,load_test_series
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
from xgboost import XGBClassifier # or XGBRegressor depending on your task
import xgboost as xgb
import numpy as np
@st.cache_data
def load_sampled_data():
# df3 = pd.read_parquet("train_series.parquet", columns=['series_id', 'step', 'anglez', 'enmo'])
# df4 = pd.read_parquet("test_series.parquet", columns=['series_id', 'step', 'anglez', 'enmo'])
df2 = pd.read_csv("train_events.csv")
# Sample safely based on available data
# df3_sample = df3.sample(n=min(5_000_000, len(df3)), random_state=42)
# df4_sample = df4.sample(n=min(1_000_000, len(df4)), random_state=42)
return df2
# Load
# df3, df4, df2 = load_sampled_data()
df2 = load_sampled_data()
# df = pd.concat([df3, df4], axis=0, ignore_index=True)
# merged_df = pd.merge(df, df2, on=['series_id', 'step'], how='inner')
merged_df = pd.read_csv("merged_df.csv")
# Rename timestamp columns if they exist
if 'timestamp_x' in merged_df.columns:
merged_df.rename(columns={'timestamp_x': 'sensor_timestamp'}, inplace=True)
if 'timestamp_y' in merged_df.columns:
merged_df.rename(columns={'timestamp_y': 'event_timestamp'}, inplace=True)
st.title("π Step Distribution Analysis")
# Layout: 2 columns
col1, col2 = st.columns([1, 1]) # Equal width
# ----- Column 1: Boxplot -----
with col1:
st.subheader("π¦ Boxplot of Step")
fig, ax = plt.subplots(figsize=(6, 4)) # Adjusted for better visibility
sns.boxplot(x=df2['step'], ax=ax, color='steelblue')
ax.set_title("Distribution of Step Count", fontsize=14)
ax.set_xlabel("Step", fontsize=12)
st.pyplot (fig)
# ----- Column 2: Insights -----
with col2:
st.subheader("π§ Insights from the Boxplot")
st.markdown("""
<small>
<b>Central Tendency:</b><br>
- The <b>median</b> is close to the center of the box, suggesting a fairly symmetric distribution within the interquartile range (IQR).<br>
<b>Spread:</b><br>
- A <b>wide IQR</b> indicates significant variability in the step counts across sessions.<br>
<b>Outliers:</b><br>
- The <b>dots on the right</b> are outliers β representing very high step counts.<br>
- These could reflect either:<br>
- <b>Legitimate long-duration recordings</b><br>
- Or <b>data quality issues</b> (e.g., duplication or sensor errors)
<b>Distribution Shape:</b><br>
- A <b>longer left whisker</b> implies a <b>left-skewed</b> distribution.<br>
- Most sessions have <b>lower step values</b>, with a few very high outliers.
</small>
""", unsafe_allow_html=True)
#st.write("1. Data Visualization - Scatter Plot (feature vs feature or vs target)")
# Assume merged_df is already defined or loaded
df_sample = merged_df # or use df_sample = merged_df.sample(n=50000) to downsample
st.subheader("Scatter Plot: anglez vs enmo")
col1, col2 = st.columns([1, 1])
with col1:
#st.subheader("Scatter Plot: anglez vs enmo")
# fig, ax = plt.subplots(figsize=(6, 4))
# sns.scatterplot(x=df['anglez'], y=df['enmo'], ax=ax)
# ax.set_title("Scatter Plot: anglez vs enmo")
# st.pyplot(fig)
# Create the plot
fig, ax = plt.subplots(figsize=(6, 4))
sns.scatterplot(x='anglez', y='enmo', data=df_sample, ax=ax)
ax.set_title("Scatter Plot: anglez vs enmo")
# Display in Streamlit
st.pyplot(fig)
with col2:
st.markdown("""
<small>
<b>1. Clustered Points:</b> Most `enmo` values are near 0, suggesting low movement.<br>
<b>2. Symmetry:</b> Spread is balanced on both sides of anglez (Β±), indicating no directional bias.<br>
<b>3. Weak Correlation:</b> No visible trend, suggesting independence between `anglez` and `enmo`.<br>
<b>4. Outliers:</b> A few high `enmo` points may indicate sudden or intense movement.<br>
<b>5. Interpretation:</b> Most data reflects light activity or rest, regardless of body orientation.
</small>
""", unsafe_allow_html=True)
# df_sample = merged_df.sample(n=10000) # adjust sample size for performance
# # Subheader
# st.subheader("Pair Plot of Features")
# # Create pairplot
# fig = sns.pairplot(df_sample[['anglez', 'enmo', 'step']])
# fig.fig.suptitle("Pair Plot of Features", y=1.02)
# # Display in Streamlit
# st.pyplot(fig)
# Define columns to plot
col1, col2 = st.columns([1, 1]) # Equal width
# Column 1: Pair Plot
with col1:
st.subheader("π Pair Plot of Features")
fig = sns.pairplot(merged_df[['anglez', 'enmo', 'step']])
st.pyplot(fig)
# Column 2: Insights
with col2:
st.subheader("π§ Insights from Pair Plot")
st.markdown("""
<div style='font-size: 14px'>
### π Distribution Insights:
- **anglez**: Symmetric distribution peaking near -50 to 0.
- **enmo**: Right-skewed, most values below 0.1.
- **step**: Right-skewed, with a few large outliers.
### π Pairwise Relationships:
- **anglez vs enmo**: No clear trend; cone-like shape.
- **anglez vs step**: No correlation; looks uniformly scattered.
- **enmo vs step**: Clustered at low values. High steps sometimes with low enmo.
### π‘ Summary:
- Features appear largely **uncorrelated**.
- Helps identify **data distributions** and potential **outliers**.
- Can assist in **feature selection/engineering**.
</div>
""", unsafe_allow_html=True)
# plot_columns = ['anglez', 'enmo', 'step']
# # Safety check: make sure required columns exist
# if all(col in merged_df.columns for col in plot_columns):
# # Check data size and sample accordingly
# max_rows = len(merged_df)
# sample_size = min(10000, max_rows) # Don't exceed available rows
# df_sample = merged_df.sample(n=sample_size)
# # Subheader
# st.subheader("Pair Plot of Features")
# # Create pairplot
# fig = sns.pairplot(df_sample[plot_columns])
# fig.fig.suptitle("Pair Plot of Features", y=1.02)
# # Display in Streamlit
# st.pyplot(fig)
# else:
# st.error("One or more required columns ('anglez', 'enmo', 'step') are missing in the dataset.")
# Plot
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
sns.histplot(df_sample['anglez'], kde=True, bins=50, ax=axes[0])
axes[0].set_title("Distribution of anglez")
sns.histplot(df_sample['enmo'], kde=True, bins=50, ax=axes[1])
axes[1].set_title("Distribution of enmo")
plt.tight_layout()
st.pyplot(fig)
# Show insights side by side
col1, col2 = st.columns(2)
with col1:
st.markdown("""
<div style='font-size: 14px'>
<h3> π Distribution of `anglez`: </h3>
- The distribution is **roughly symmetric**, centered around **-50 to 0**.
- It resembles a **left-heavy bell shape**, suggesting:
- Most sensor angles were **tilted negatively**.
- Indicates a **natural resting position** or specific posture.
</div>
""", unsafe_allow_html=True)
with col2:
st.markdown("""
<div style='font-size: 14px'>
<h3> π Distribution of `enmo`: </h3>
- Highly **right-skewed** (sharp peak near zero).
- The majority of `enmo` values are **very small** (< 0.05), indicating:
- **Minimal movement or low activity** in most sessions.
- Few data points reflect **moderate to high movement**.
</div>
""", unsafe_allow_html=True)
# st.write("Multicollinearity Check - Correlation Matrix")
# features = ['anglez', 'enmo', 'step', 'night']
# df_subset = merged_df[features]
# # Streamlit title
# st.subheader("Multicollinearity Check - Correlation Matrix")
# # Calculate correlation matrix
# corr_matrix = df_subset.corr()
# # Plot heatmap
# fig, ax = plt.subplots(figsize=(6, 4))
# sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', ax=ax)
# ax.set_title("Correlation Matrix")
# # Display in Streamlit
# st.pyplot(fig)
st.subheader("Multicollinearity Check - Correlation Matrix")
# Select relevant features
features = ['anglez', 'enmo', 'step', 'night']
df_subset = merged_df[features]
# Calculate correlation matrix
corr_matrix = df_subset.corr()
# Create plot
fig, ax = plt.subplots(figsize=(6, 4))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', center=0, fmt=".3f", ax=ax)
ax.set_title("Correlation Matrix")
# Layout in two columns
col1, col2 = st.columns(2)
# Column 1: Heatmap
with col1:
st.pyplot(fig)
# Column 2: Textual Insights
with col2:
st.markdown("""
### π Insights from Correlation Matrix
- **`anglez` & `enmo`**:
πΈ Weak negative correlation (**-0.11**) β suggests minimal linear relationship.
- **`step` & `night`**:
β οΈ Perfect correlation (**1.00**) β indicates **redundancy**, likely representing the same event in different forms.
- **Overall**:
β
Low multicollinearity across most features β safe for modeling.
π Recommend removing either `step` or `night` to reduce feature duplication.
""")
# Encode
le = LabelEncoder()
merged_df['series_id'] = le.fit_transform(merged_df['series_id'])
merged_df['event'] = le.fit_transform(merged_df['event'])
# Drop columns with string or datetime values
drop_cols = ['sensor_timestamp', 'event_timestamp', 'night', 'step', 'sleep_duration_hrs', 'series_id']
df_cleaned = merged_df.drop(columns=[col for col in drop_cols if col in merged_df.columns])
# Ensure only numeric features in X
X = df_cleaned.drop('event', axis=1).select_dtypes(include=[np.number])
y = merged_df['event']
# Split and scale
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=27)
st.subheader("Feature Importance")
# Create model instance
xgb_model = XGBClassifier(use_label_encoder=False, eval_metric='logloss') # example for classification
# Fit the model
xgb_model.fit(X_train, y_train)
# Plot feature importance
fig, ax = plt.subplots(figsize=(6, 4))
xgb.plot_importance(xgb_model, ax=ax)
ax.set_title("XGBoost Feature Importance")
# Show in Streamlit
st.subheader("XGBoost Feature Importance")
col1, col2 = st.columns(2)
# Column 1: Plot
with col1:
st.pyplot(fig)
st.markdown("""
#### π« Low-Impact Features:
- Features like `step` and `night` (excluded in this plot) showed **minimal or redundant contribution**.
- π You may consider **removing** them to simplify the model.
""")
# Column 2: Insights
with col2:
st.markdown("""
<small>
<h3> π XGBoost Feature Importance: Key Insights </h3>
#### π Top Features:
- πΉ **`anglez`** β Highest importance score (**1557**)
- πΉ **`enmo`** β Close second with score (**1546**)
#### β
Summary:
- Both `anglez` and `enmo` contribute **significantly** to the model.
- Their high scores reflect **strong influence** in predicting the target variable.
#### π‘ Interpretation:
- These features likely capture **activity level** or **sleep posture** patterns.
- Keeping both is **recommended** for accurate classification.
</small>
""", unsafe_allow_html=True)
|