(regional_features, X_scaled, feature_cols)
| 76 | st.metric("Largest", f"{largest_cluster_size} regions") |
| 77 | |
| 78 | def cluster_visualization(regional_features, X_scaled, feature_cols): |
| 79 | col1, col2 = st.columns(2) |
| 80 | |
| 81 | with col1: |
| 82 | if len(X_scaled) > 1: |
| 83 | pca = PCA(n_components=2) |
| 84 | X_pca = pca.fit_transform(X_scaled) |
| 85 | |
| 86 | pca_df = pd.DataFrame({ |
| 87 | 'PC1': X_pca[:, 0], |
| 88 | 'PC2': X_pca[:, 1], |
| 89 | 'Region': regional_features['REGIONAL_OFFICE_NAME'], |
| 90 | 'Cluster': regional_features['Cluster'], |
| 91 | 'Total_Cost': regional_features['Total_Cost'] |
| 92 | }) |
| 93 | |
| 94 | fig_pca = px.scatter( |
| 95 | pca_df, |
| 96 | x='PC1', |
| 97 | y='PC2', |
| 98 | color='Cluster', |
| 99 | size='Total_Cost', |
| 100 | hover_name='Region', |
| 101 | title="Regional Clusters", |
| 102 | size_max=40 |
| 103 | ) |
| 104 | fig_pca.update_layout(height=400) |
| 105 | st.plotly_chart(fig_pca, use_container_width=True) |
| 106 | |
| 107 | with col2: |
| 108 | cluster_summary = regional_features.groupby('Cluster')[feature_cols].mean().round(2) |
| 109 | |
| 110 | display_summary = cluster_summary.copy() |
| 111 | display_summary['Total_Cost'] = display_summary['Total_Cost'].apply(lambda x: f"\u00a3{x:,.0f}") |
| 112 | display_summary['Mean_Cost'] = display_summary['Mean_Cost'].apply(lambda x: f"\u00a3{x:,.0f}") |
| 113 | display_summary['Cost_Per_Record'] = display_summary['Cost_Per_Record'].apply(lambda x: f"\u00a3{x:,.0f}") |
| 114 | display_summary.columns = ['Total', 'Mean', 'Var', 'Per Record'] |
| 115 | |
| 116 | st.dataframe(display_summary, use_container_width=True) |
| 117 | |
| 118 | def geographic_distribution(regional_features): |
| 119 | map_data = [] |
no outgoing calls
no test coverage detected