Sunday 15 November 2020

Weighted network between cluster centroids - Python

The following plots vectors derived from two sets of data points. I also measure and plot the centroid of these points using k-means clustering.

I'm hoping to measure some form of adjacency matrix to plot the network between each cluster based on the number of vectors, which also accounts for the amount of vectors between each cluster. So displaying the weight.

I was thinking the diagonal values of the adjacency matrix could indicate the number of vectors in the same cluster, while the non-diagonal values could indicate the number of vectors between different clusters, while considering the direction?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.cluster import KMeans

fig, ax = plt.subplots(figsize = (6,6))

df = pd.DataFrame(np.random.randint(-80,80,size=(500, 4)), columns=list('ABCD'))

A = df['A']
B = df['B']

C = df['C']
D = df['D']

Y_sklearn = df[['A','B','C','D']].values

ax.quiver(A, B, (C-A), (D-B), angles = 'xy', scale_units = 'xy', scale = 1, alpha = 0.5) 

model = KMeans(n_clusters = 20)
model.fit(Y_sklearn)

model.cluster_centers_ 
cluster_centers = model.cluster_centers_
plt.scatter(cluster_centers[:, 0], cluster_centers[:, 1], 
            color = 'black', s = 100, 
            alpha = 0.7, zorder = 2)

plt.scatter(Y_sklearn[:,0], Y_sklearn[:,1], color = 'blue', alpha = 0.2); 
plt.scatter(Y_sklearn[:,2], Y_sklearn[:,3], color = 'red', alpha = 0.2); 


from Weighted network between cluster centroids - Python

No comments:

Post a Comment