Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import seaborn as sns
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
# Load the dataset
|
| 7 |
+
file_path = 'Enhanced_Dummy_HBL_Data.xlsx' # Update this path as needed
|
| 8 |
+
data = pd.read_excel(file_path)
|
| 9 |
+
|
| 10 |
+
# Set up the Streamlit page
|
| 11 |
+
st.title("HBL Transactions Dashboard")
|
| 12 |
+
st.sidebar.header("Navigation")
|
| 13 |
+
options = st.sidebar.selectbox("Select Visualization", [
|
| 14 |
+
"Account Type Distribution",
|
| 15 |
+
"Transaction Flow by Beneficiary Bank",
|
| 16 |
+
"Geographic Heatmap of Transactions",
|
| 17 |
+
"Anomalies in Transactions",
|
| 18 |
+
"Comparative Analysis of Transaction Types",
|
| 19 |
+
"Time-Based Analysis",
|
| 20 |
+
"Customer Insights"
|
| 21 |
+
])
|
| 22 |
+
|
| 23 |
+
# Task 1: Account Type Distribution
|
| 24 |
+
if options == "Account Type Distribution":
|
| 25 |
+
st.header("Account Type Distribution")
|
| 26 |
+
account_type_distribution = data['Account Type'].value_counts().reset_index()
|
| 27 |
+
account_type_distribution.columns = ['Account Type', 'Count']
|
| 28 |
+
|
| 29 |
+
fig, ax = plt.subplots(figsize=(8, 6))
|
| 30 |
+
sns.barplot(data=account_type_distribution, x='Account Type', y='Count', palette='coolwarm', ax=ax)
|
| 31 |
+
ax.set_title('Account Type Distribution')
|
| 32 |
+
st.pyplot(fig)
|
| 33 |
+
|
| 34 |
+
# Task 2: Transaction Flow by Beneficiary Bank
|
| 35 |
+
elif options == "Transaction Flow by Beneficiary Bank":
|
| 36 |
+
st.header("Transaction Flow by Beneficiary Bank")
|
| 37 |
+
top_beneficiary_banks = data.groupby(['Region', 'Transaction To'])['Credit'].sum().reset_index()
|
| 38 |
+
top_beneficiary_banks = top_beneficiary_banks.sort_values(by='Credit', ascending=False).groupby('Region').head(5)
|
| 39 |
+
|
| 40 |
+
fig, ax = plt.subplots(figsize=(12, 8))
|
| 41 |
+
sns.barplot(data=top_beneficiary_banks, x='Credit', y='Transaction To', hue='Region', palette='viridis', ax=ax)
|
| 42 |
+
ax.set_title('Top 5 Beneficiary Banks by Region')
|
| 43 |
+
st.pyplot(fig)
|
| 44 |
+
|
| 45 |
+
# Task 3: Geographic Heatmap of Transactions
|
| 46 |
+
elif options == "Geographic Heatmap of Transactions":
|
| 47 |
+
st.header("Geographic Heatmap of Transactions")
|
| 48 |
+
heatmap_data = data.groupby('Region')[['Credit', 'Debit']].sum().reset_index()
|
| 49 |
+
heatmap_matrix = heatmap_data.set_index('Region')
|
| 50 |
+
|
| 51 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 52 |
+
sns.heatmap(heatmap_matrix, annot=True, fmt='.0f', cmap='YlGnBu', cbar=True, ax=ax)
|
| 53 |
+
ax.set_title('Geographic Heatmap of Transactions')
|
| 54 |
+
st.pyplot(fig)
|
| 55 |
+
|
| 56 |
+
# Task 4: Anomalies in Transactions
|
| 57 |
+
elif options == "Anomalies in Transactions":
|
| 58 |
+
st.header("Anomalies in Transactions")
|
| 59 |
+
from scipy.stats import zscore
|
| 60 |
+
|
| 61 |
+
data['Credit_Z'] = zscore(data['Credit'])
|
| 62 |
+
data['Debit_Z'] = zscore(data['Debit'])
|
| 63 |
+
|
| 64 |
+
credit_anomalies = data[data['Credit_Z'].abs() > 3]
|
| 65 |
+
debit_anomalies = data[data['Debit_Z'].abs() > 3]
|
| 66 |
+
|
| 67 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
| 68 |
+
sns.scatterplot(x=data.index, y=data['Credit'], label='Credit Transactions', alpha=0.6, color='blue', ax=ax)
|
| 69 |
+
sns.scatterplot(x=credit_anomalies.index, y=credit_anomalies['Credit'], label='Credit Anomalies', color='red', ax=ax)
|
| 70 |
+
sns.scatterplot(x=data.index, y=data['Debit'], label='Debit Transactions', alpha=0.6, color='green', ax=ax)
|
| 71 |
+
sns.scatterplot(x=debit_anomalies.index, y=debit_anomalies['Debit'], label='Debit Anomalies', color='orange', ax=ax)
|
| 72 |
+
ax.set_title('Anomalies in Transactions')
|
| 73 |
+
ax.legend()
|
| 74 |
+
st.pyplot(fig)
|
| 75 |
+
|
| 76 |
+
# Task 5: Comparative Analysis of Transaction Types
|
| 77 |
+
elif options == "Comparative Analysis of Transaction Types":
|
| 78 |
+
st.header("Comparative Analysis of Transaction Types")
|
| 79 |
+
melted_data = data.melt(id_vars=['Account Type'], value_vars=['Credit', 'Debit'],
|
| 80 |
+
var_name='Transaction Type', value_name='Amount')
|
| 81 |
+
|
| 82 |
+
fig, ax = plt.subplots(figsize=(12, 8))
|
| 83 |
+
sns.boxplot(data=melted_data, x='Account Type', y='Amount', hue='Transaction Type', palette='Set2', ax=ax)
|
| 84 |
+
ax.set_title('Comparative Analysis of Transaction Types')
|
| 85 |
+
st.pyplot(fig)
|
| 86 |
+
|
| 87 |
+
# Task 6: Time-Based Analysis
|
| 88 |
+
elif options == "Time-Based Analysis":
|
| 89 |
+
st.header("Time-Based Analysis")
|
| 90 |
+
if 'Time' in data.columns:
|
| 91 |
+
data['Time'] = pd.to_datetime(data['Time'])
|
| 92 |
+
time_data = data.groupby(data['Time'].dt.to_period('M'))[['Credit', 'Debit']].sum().reset_index()
|
| 93 |
+
time_data['Time'] = time_data['Time'].dt.to_timestamp()
|
| 94 |
+
|
| 95 |
+
fig, ax = plt.subplots(figsize=(12, 8))
|
| 96 |
+
sns.lineplot(data=time_data, x='Time', y='Credit', label='Credit', marker='o', ax=ax)
|
| 97 |
+
sns.lineplot(data=time_data, x='Time', y='Debit', label='Debit', marker='o', ax=ax)
|
| 98 |
+
ax.set_title('Transaction Trends Over Time')
|
| 99 |
+
st.pyplot(fig)
|
| 100 |
+
|
| 101 |
+
# Task 7: Customer Insights
|
| 102 |
+
elif options == "Customer Insights":
|
| 103 |
+
st.header("Customer Insights")
|
| 104 |
+
if 'Customer Type Description' in data.columns:
|
| 105 |
+
customer_data = data.groupby('Customer Type Description')[['Credit', 'Debit']].sum().reset_index()
|
| 106 |
+
customer_data.set_index('Customer Type Description', inplace=True)
|
| 107 |
+
|
| 108 |
+
fig, ax = plt.subplots(figsize=(10, 8))
|
| 109 |
+
customer_data.plot(kind='bar', stacked=True, colormap='coolwarm', ax=ax)
|
| 110 |
+
ax.set_title('Total Credit and Debit by Customer Type')
|
| 111 |
+
st.pyplot(fig)
|