newshow
newshow

Reputation: 11

Error complex data not supported in spectral clustering

I'm running spectral clustering on a similarity matrix which is 1000*1000. my similarity matrix is as follows:

matrix([[0.000, 0.031, 0.030, ..., 0.850, 0.867, 0.838],
        [0.031, 0.000, 0.005, ..., 0.780, 0.805, 0.781],
        [0.030, 0.005, 0.000, ..., 0.803, 0.823, 0.795],
        ...,
        [0.850, 0.780, 0.803, ..., 0.000, 0.024, 0.008],
        [0.867, 0.805, 0.823, ..., 0.024, 0.000, 0.014],
        [0.838, 0.781, 0.795, ..., 0.008, 0.014, 0.000]])

I created adjacency and degree matrix and Laplacian matrix and then calculated eigenvalues and eigenvectors and I used the second smallest eigenvalue to figure out which node should be placed in which category.:

e, v = np.linalg.eig(L)
fig = plt.figure(figsize=[30, 6])
ax1 = plt.subplot(221)
plt.plot(e)
ax1.title.set_text('eigenvalues')
i = np.where(e < 300)[0]
ax2 = plt.subplot(222)
plt.plot(v[:, i[0]])
ax3 = plt.subplot(223)
plt.plot(v[:, i[1]])
ax3.title.set_text('second eigenvector with eigenvalue close to 0')

enter image description here

But when I want to run kmeans as below to separate point I have an error:

U = np.array(v[:, i[1]])
km = KMeans(init='k-means++', n_clusters=3)
km.fit(U)
km.labels_

Error:

> ValueError: Complex data not supported [[-0.04866435+0.j] [-0.04909432+0.j] [-0.04840705+0.j] [-0.04859193+0.j] [-0.0514795 +0.j],...]

Can you help me to know why is this error? I searched alot but no result.

Upvotes: 1

Views: 3295

Answers (2)

Guenter
Guenter

Reputation: 485

There is a simple solution to this, just insert two lines of code as follows:

e, v = np.linalg.eig(L)
e = e.real
v = v.real

This will allow the use of the data for KMeans.

Upvotes: 2

Giuseppe Angora
Giuseppe Angora

Reputation: 853

Reading the documentation related to what np.linalg.eig returns:

The eigenvalues, each repeated according to its multiplicity. The eigenvalues are not necessarily ordered. The resulting array will be of complex type, unless the imaginary part is zero in which case it will be cast to a real type. When a is real the resulting eigenvalues will be real (0 imaginary part) or occur in conjugate pairs

The eigenvalues you have computed are complex, and you can not use complex with a KMeans.

Upvotes: 0

Related Questions