Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

python 3.x - Color coding the scatter plot per KMeans cluster

So I used the below code and I want to know how to color code the scatter plot per KMeans cluster. I’m new to coding, so please bear with me:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
data = pd.read_csv(“Banknote authentication dataset.csv”)
var = data[‘V1’]
skew = data[‘V2’]
varskew = np.column_stack((var,skew))
kem_res = KMeans(n_clusters=2).fit(varskew)
clusters = km_res.cluster_centers_
plt.scatter(varskew[:,0],varskew[:,1],s=10,alpha=0.5)
plt.scatter(clusters[:,0],clusters[:,1],s=50)
plt.show()

So, I got the dataset and the clusters plotted, but I want to color code the dataset per the cluster they belong to.

PS: I’m using Python3 on iPad using Juno application.

This is the dataset being used

Scatter Plot as per above code:

Scatter Plot as per above code


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...