【问题标题】:I am trying to get kmeans to plot 5 clusters, but I'm only get 1 cluster plotted我试图让 kmeans 绘制 5 个集群,但我只绘制了 1 个集群
【发布时间】:2021-08-01 04:36:55
【问题描述】:

我在 SO 上找到了一些似乎运行良好的代码。

这段代码,直接在下面,产生了图,也在下面。

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


iris = datasets.load_iris()

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

现在,我在代码中做一个简单的替换,指向我自己的数据,就像这样。

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

x = df[['Spend']]
y = df[['Revenue']]

kmeans = cluster.KMeans(n_clusters=5, random_state=42).fit(x,y)
data = [plotly.graph_objs.Scatter(x=df[['Spend']], 
                                  y=df[['Revenue']], 
                                  mode='markers',     
                                  marker=dict(color=kmeans.labels_))]
plotly.offline.iplot(data)

这给了我这个情节。

这是我的数据框。

# Import pandas library
import pandas as pd
  
# initialize list of lists
data = [[110,'CHASE CENTER',53901,8904,44997,4], [541,'METS STADIUM',57999,4921,53078,1], [538,'DEN BRONCOS',91015,9945,81070,1], [640,'LAMBEAU WI',76214,5773,70441,3], [619,'SAL AIRPORT',93000,8278,84722,5]]
  
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Location', 'Location_Description', 'Revenue','Spend','Profit_Or_Loss','cluster_number'])
  
# print dataframe.
df

我一定是错过了什么愚蠢的东西,但我不明白它是什么。

【问题讨论】:

    标签: python python-3.x machine-learning artificial-intelligence cluster-analysis


    【解决方案1】:

    你的维度有问题:

    # In the iris dataset
    >>> iris.data[:,0].shape
    (150,)
    # Your data
    >>> x.shape
    (5, 1)
    
    # You need to flatter your array
    x.values.flatten().shape
    (5,)
    

    例如:

    from sklearn import datasets
    from sklearn import cluster
    import plotly
    plotly.offline.init_notebook_mode()
    
    x = df[['Spend']]
    y = df[['Revenue']]
    
    x_flat = x.values.flatten()
    y_flat = y.values.flatten()
    
    kmeans = cluster.KMeans(n_clusters=5, random_state=42).fit(x)
    data = [plotly.graph_objs.Scatter(x=x_flat, 
                                      y=y_flat, 
                                      mode='markers',     
                                      marker=dict(color=kmeans.labels_))]
    plotly.offline.iplot(data)
    

    另一方面,cluster.KMeans.fit 接受一个数组(而不是您传递的两个)。您将不得不将它们转换为某种形状(n_samples,n_features):

    X = np.zeros((x_flat.shape[0], 2))
    X[:, 0] = x_flat
    X[:, 1] = y_flat
    # X.shape -> (5, 2)
    
    kmeans = cluster.KMeans(n_clusters=5, random_state=42).fit(X)
    

    【讨论】:

    • 啊。现在它起作用了。我没有你说我必须把它弄平。感谢您指出这一点!
    猜你喜欢
    • 2021-11-28
    • 2014-06-24
    • 2019-06-10
    • 2019-04-12
    • 2017-07-28
    • 2014-11-15
    • 2019-09-14
    • 2014-12-26
    • 2013-12-23
    相关资源
    最近更新 更多