The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

KMeans label attribute

KMeans label attribute

0
Votes
1
Answer

How to fix that thing? I was trying to follow the video. Thank you so much.

 
 
 

 

df_segm_pca_kmeans = pd.concat([df_segmentation.reset_index(drop = True), pd.DataFrame(scores_pca)], axis = 1)
df_segm_pca_kmeans.columns.values[-3: ] = ["Component 1", "Component 2", "Component 3"]
df_segm_pca_kmeans["Segment K-means PCA"] = kmeans_pca.labels_
df_segm_pca_kmeans

 

 

 

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-52-a697fdea9e43> in <module>
      1 df_segm_pca_kmeans = pd.concat([df_segmentation.reset_index(drop = True), pd.DataFrame(scores_pca)], axis = 1)
      2 df_segm_pca_kmeans.columns.values[-3: ] = ["Component 1", "Component 2", "Component 3"]
----> 3 df_segm_pca_kmeans["Segment K-means PCA"] = kmeans_pca.labels_
      4 df_segm_pca_kmeans

AttributeError: 'KMeans' object has no attribute 'labels_'
1 Answer

365 Team

Hi Liqian, 
You need to fit your kmeans_pca first, for it to have the labels_ attribute.
So, you should make sure you run this line:
kmeans_pca.fit(scores_pca)
before you create the df_segm_pca_kmeans table.
Hope this helps!
 
Best, 
Eli

lol I kinda did, but I forgot to change it to kmeans_pca. Thanks.

6 months

No worries, glad it’s working! (:

6 months