1

This is my code to transform a lists of data to be fed into a Kmeans model. I want to visualize my clusters in a 2d plot using PCA.

import numpy as np
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

#my data is longer than this but this is a sample :
search_terms = ['computer','usb port', 'phone adaptor']
clicks = [3,2,1]
bounce = [0,0,2]
conversion = [4,1,0]

X = np.array([bounce,conversion,clicks]).T
y = np.array(search_term)

num_clusters = 5

pca = PCA(n_components=2, whiten=True).fit_transform(X)
data2D = pca.transform(X)

km = KMeans(n_clusters=num_clusters, init='k-means++',n_init=10, verbose=1)
km.fit(X_pca)

centers2D = pca.transform(km.cluster_centers_)
plt.scatter( data2D[:,0], data2D[:,1], c=label_color)

This is the error i am getting:

data2D = pca.transform(X)
AttributeError: 'numpy.ndarray' object has no attribute 'transform'

I suppose we cant use pca's fit_transform on a numpy array. What can i do instead?

Thanks

1 Answer 1

1

It looks like you're calling fit_transform twice, is this really what you want to do?

This seems to work for me:

pca = PCA(n_components=2, whiten=True).fit(X)
data2D = pca.transform(X)

data2D
Out[5]: 
array([[-1.29303192,  0.57277158],
       [ 0.15048072, -1.40618467],
       [ 1.14255114,  0.8334131 ]])
Sign up to request clarification or add additional context in comments.

3 Comments

but now you're calling transform on the PCA? what is this line trying to do? you've already tranformed it to 2D.
Even if i do fit then transform, i get the same error: pca = PCA(n_components=2, whiten=True).fit(X) then data2D = pca.transform(X) i get this error: data2D = X_pca.transform(X) ` X = X - self.mean_` TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'
apologies after fixing that part the error was caused by not changing km.fit(X_pca) . Thanks for your help, ill be deleting this qn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.