2017-03-23 8 views
1

클러스터 된 분산 형 플롯을 시각화하기 위해 점을 찍을 때 포인트 정보를 얻을 수 있도록 플로팅을 사용하고 있습니다. KMeans를 사용하여 생성 한 클러스터에 다른 색상을 지정하는 데 문제가 있습니다.분산 형 플롯으로 플롯 별 파이썬으로 클러스터 분리

plt.scatter(restult[:,0], result[:,1], c=cluster_labels 

cluster_labels이되는 : (PLT로) matplotlib.pyplot이 음모를 꾸미고 때 나는 다음 코드를 사용

n_clusters = 3 
km = KMeans(n_clusters).fit(result) 
labels = km.labels_ 

을 그리고 그것은 완전히 잘 작동하지만 나는 후버 정보가 필요합니다.

trace = go.Scatter(
    x = result[:,0], 
    y = result[:,1], 
    mode = 'markers', 
    text = index, # I want to see the index of each point 
) 
data = [trace] 

# Plot and embed in ipython notebook! 
py.iplot(data, filename='basic-scatter') 

내가 도움을 주셔서 감사합니다 : 지금까지 plotly과에서 어딘지

이입니다!

답변

1
  • 의 홍채 데이터를 사용하자가 kmeans에서
  • 레이블 그냥하기 matplotlib

from sklearn import datasets 
from sklearn import cluster 
import plotly 
plotly.offline.init_notebook_mode() 

iris = datasets.load_iris() 
kmeans = cluster.KMeans(n_clusters=3, 
         random_state=42).fit(iris.data[:,0:2]) 
data = [plotly.graph_objs.Scatter(x=iris.data[:,0], 
            y=iris.data[:,1], 
            mode='markers',  
            marker=dict(color=kmeans.labels_) 
           ) 
     ] 
plotly.offline.iplot(data) 

enter image description here

0
처럼, 색상 ( marker=dict(color=kmeans.labels_)를)로 사용되는 설정

Maxmimilian의 방법으로 확장하십시오 - sklearn version> = 0.17을 사용한다면 1 차원 배열을 전달하는 것이 0.17에서 더 이상 사용되지 않으므로 배열을 변형해야합니다.

x = df[df.columns[1]] 
x = x.values.reshape(-1,1) 
y = df[df.columns[2]] 
y = y.values.reshape(-1,1) 

kmeans = cluster.KMeans(n_clusters = 3, random_state = 0).fit(x, y) 


trace1 = go.Scatter(
x = df[df.columns[1]], 
y = df[df.columns[2]], 
mode = 'markers', 
marker=dict(color=kmeans.labels_, 
      size = 7.5, 
      line = dict(width=2) 
      ), 
text = df.index, 
name='Actual' 
) 
: 여기

은 재편과 예제