Python Forum
How to create correct scatter plot for PCA?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create correct scatter plot for PCA?
#1
[Image: search?view=detailV2&insightstoken=bcid_...vt=2&sim=1]

I clustered my data (using kmeans) with high dimensions in Python and after I wanted to build scatter plot with using PCA. But my plot is very strange and I don't understand why? (image in attachment) Also I found that PCA components have negative values. Can someone advise how to build correct scatter plot?
My main steps :
1.normalize data
2.Kmeans clustering
3.create scatter plot


My code:
#Normalize data
scaler = MinMaxScaler()
new2 = pd.DataFrame(scaler.fit_transform(dd))
#Kmeans
kmeans = KMeans(n_clusters=5)
kmeans.fit(new2)
clusters = kmeans.predict(new2)
#PCA and scatter plot
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(new2)
principalDf = pd.DataFrame(data = principalComponents
             , columns = ['principal component 1', 'principal component 2'])
finalDf = pd.concat([principalDf, new2[['Cluster']]], axis = 1)

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(1,1,1) 
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)
targets = ['0','1','2','3','4']
colors = ['red','blue','black','pink','green']
for target, color in zip(targets,colors):
    indicesToKeep = finalDf['Cluster'] == target
    ax.scatter(finalDf.loc[indicesToKeep, 'principal component 1']
               , finalDf.loc[indicesToKeep, 'principal component 2']
               , c = color
               , s = 50)
ax.legend(targets)
ax.grid()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a plot with line graphs and vertical bars devansing 6 2,342 Feb-28-2023, 05:38 PM
Last Post: devansing
  Matplotlib scatter plot in loop with None values ivan_sc 1 2,274 Nov-04-2021, 11:25 PM
Last Post: jefsummers
  Matplotlib scatter plot slider with datetime sonny 0 2,956 Feb-05-2021, 02:31 AM
Last Post: sonny
  matplotlib creating a scatter plot via PathCollection tpourjalali 0 2,477 Apr-11-2020, 05:59 PM
Last Post: tpourjalali
  Not able to figure out how to create bar plot on aggregate data - Python darpInd 1 2,289 Mar-30-2020, 11:37 AM
Last Post: jefsummers
  How to add lables to the scatter plot? Jack_Sparrow 3 3,268 Dec-11-2019, 10:20 PM
Last Post: Larz60+
  It doesn't show scatter plot but it says: "<Figure size 640x480 with 1 Axes>" dcardonaa 0 3,500 Oct-10-2019, 02:34 AM
Last Post: dcardonaa
  Plot correct values Felipe 9 7,044 Feb-18-2017, 09:21 AM
Last Post: merlem

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020