Reputation: 13
I tried to make clustering for my data, the code is
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from sklearn.manifold import MDS
import itertools
import matplotlib.cm
from sklearn import manifold
xx = pd.read_csv('distances.data',sep=" ",header=None)
NL=max(xx[0])
ND=max(xx[1])
if NL>ND:
ND=NL
N = len(xx)
dist=np.zeros([ND,ND])
for i in range(N):
ii = xx[0][i]-1
jj = xx[1][i]-1
dist[ii][jj]=xx[2][i]
dist[jj][ii]=xx[2][i]
print("start")
mds = manifold.MDS(max_iter=200, eps=1e-4, n_init=1,dissimilarity='precomputed',random_state=0)
dp_mds = mds.fit_transform(dist)
plt.scatter(dp_mds[:, 0], dp_mds[:, 1])
plt.savefig("1.jpg")
and got the shape like this for my data enter image description here but need each cluster has a different color , how can i do it , please ?
can i get like that enter image description here
Upvotes: 1
Views: 470
Reputation: 13242
You'll want to utilize c
and cmap
:
colors = dict(c=dp_mds[:, 0], cmap=plt.cm.get_cmap('jet', 6))
plt.scatter(dp_mds[:, 0], dp_mds[:, 1], **colors)
Upvotes: 1