Spaces:
Sleeping
Sleeping
cluster visuals added
Browse files- docs/clusters_tsne.png +3 -0
- docs/persona_radar_chart.png +3 -0
- visualize_clusters.py +103 -0
docs/clusters_tsne.png
ADDED
|
Git LFS Details
|
docs/persona_radar_chart.png
ADDED
|
Git LFS Details
|
visualize_clusters.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
from sklearn.manifold import TSNE
|
| 6 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
INPUT_FILE = "wallet_dataset_labeled.csv"
|
| 10 |
+
OUTPUT_DIR = "docs"
|
| 11 |
+
RADAR_CHART_FILE = os.path.join(OUTPUT_DIR, "persona_radar_chart.png")
|
| 12 |
+
TSNE_PLOT_FILE = os.path.join(OUTPUT_DIR, "clusters_tsne.png")
|
| 13 |
+
|
| 14 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 15 |
+
|
| 16 |
+
def load_data():
|
| 17 |
+
if not os.path.exists(INPUT_FILE):
|
| 18 |
+
print(f"Error: {INPUT_FILE} not found.")
|
| 19 |
+
return None
|
| 20 |
+
return pd.read_csv(INPUT_FILE)
|
| 21 |
+
|
| 22 |
+
def plot_radar_chart(df):
|
| 23 |
+
print("Generating Radar Chart")
|
| 24 |
+
|
| 25 |
+
features = [
|
| 26 |
+
'tx_count', 'active_days', 'total_gas_spent',
|
| 27 |
+
'total_nft_volume_usd', 'dex_trades', 'total_traded_usd'
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
scaler = MinMaxScaler()
|
| 31 |
+
df_scaled = df.copy()
|
| 32 |
+
df_scaled[features] = scaler.fit_transform(df[features])
|
| 33 |
+
|
| 34 |
+
persona_means = df_scaled.groupby('Persona')[features].mean()
|
| 35 |
+
|
| 36 |
+
labels=np.array(features)
|
| 37 |
+
num_vars = len(labels)
|
| 38 |
+
angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=False).tolist()
|
| 39 |
+
angles += angles[:1]
|
| 40 |
+
|
| 41 |
+
fig, ax = plt.subplots(figsize=(10, 10), subplot_kw=dict(polar=True))
|
| 42 |
+
|
| 43 |
+
colors = sns.color_palette("husl", len(persona_means))
|
| 44 |
+
|
| 45 |
+
for idx, (persona, row) in enumerate(persona_means.iterrows()):
|
| 46 |
+
values = row.tolist()
|
| 47 |
+
values += values[:1]
|
| 48 |
+
ax.plot(angles, values, color=colors[idx], linewidth=2, label=persona)
|
| 49 |
+
ax.fill(angles, values, color=colors[idx], alpha=0.1)
|
| 50 |
+
|
| 51 |
+
ax.set_theta_offset(np.pi / 2)
|
| 52 |
+
ax.set_theta_direction(-1)
|
| 53 |
+
ax.set_xticks(angles[:-1])
|
| 54 |
+
ax.set_xticklabels(labels)
|
| 55 |
+
|
| 56 |
+
plt.legend(loc='upper right', bbox_to_anchor=(1.3, 1.1))
|
| 57 |
+
plt.title("Persona Behavioral Fingerprints (Normalized)", y=1.08)
|
| 58 |
+
|
| 59 |
+
plt.savefig(RADAR_CHART_FILE, bbox_inches='tight', dpi=300)
|
| 60 |
+
print(f"Saved radar chart to {RADAR_CHART_FILE}")
|
| 61 |
+
plt.close()
|
| 62 |
+
|
| 63 |
+
def plot_tsne(df):
|
| 64 |
+
print("Generating t-SNE Plot (this may take a moment)...")
|
| 65 |
+
|
| 66 |
+
feature_cols = [
|
| 67 |
+
'tx_count', 'active_days', 'avg_tx_per_day', 'total_gas_spent',
|
| 68 |
+
'total_nft_buys', 'total_nft_sells', 'total_nft_volume_usd',
|
| 69 |
+
'unique_nfts_owned', 'dex_trades', 'avg_trade_size_usd',
|
| 70 |
+
'total_traded_usd', 'erc20_receive_usd', 'erc20_send_usd',
|
| 71 |
+
'native_balance_delta'
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
X = df[feature_cols].fillna(0)
|
| 75 |
+
|
| 76 |
+
tsne = TSNE(n_components=2, random_state=42, init='random', learning_rate='auto')
|
| 77 |
+
X_embedded = tsne.fit_transform(X)
|
| 78 |
+
|
| 79 |
+
plt.figure(figsize=(12, 8))
|
| 80 |
+
sns.scatterplot(
|
| 81 |
+
x=X_embedded[:, 0],
|
| 82 |
+
y=X_embedded[:, 1],
|
| 83 |
+
hue=df['Persona'],
|
| 84 |
+
palette='husl',
|
| 85 |
+
alpha=0.7
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
plt.title("t-SNE Projection of Wallet Clusters")
|
| 89 |
+
plt.xlabel("Dimension 1")
|
| 90 |
+
plt.ylabel("Dimension 2")
|
| 91 |
+
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
|
| 92 |
+
|
| 93 |
+
plt.tight_layout()
|
| 94 |
+
plt.savefig(TSNE_PLOT_FILE, dpi=300)
|
| 95 |
+
print(f"Saved t-SNE plot to {TSNE_PLOT_FILE}")
|
| 96 |
+
plt.close()
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
df = load_data()
|
| 100 |
+
if df is not None:
|
| 101 |
+
plot_radar_chart(df)
|
| 102 |
+
plot_tsne(df)
|
| 103 |
+
print("Visualization complete.")
|