如何实现K-means(Python)?

如何实现K-means(Python)?,python,pandas,machine-learning,k-means,Python,Pandas,Machine Learning,K Means,我有一个数据集,由61行28列所用应用程序类别组成。我对编码非常陌生,我将第一次尝试机器学习算法,所以我感谢任何帮助 这是我的数据: Category Calendar Clock Communication Education Entertainment Finance UserId 1 1

我有一个数据集,由61行28列所用应用程序类别组成。我对编码非常陌生,我将第一次尝试机器学习算法,所以我感谢任何帮助

这是我的数据:

Category  Calendar  Clock  Communication  Education  Entertainment  Finance   
UserId                                                                         
  1                1      1              1          9              1         0   
  2                0      1              1          0              0         0   
  3                0      1              1          0              0         0   
  4                1      1              6          0              0         0   
  5                0      1              1          1              1         0   
  6                0      0              0          0              0         0   
  7                0      1              1          0              0         0   
  8                0      1              1          0              0         0   
  9                0      0              1          0              7         0   
  10               0      1              1          0              0         0   
  11               0      2              1          0              5         0   
  13               0      0              1          0              0         0   
  14               0      1              1          0              1         0   
  15               1      1              1          0              0         0   
  16               0      1              1          0              0         0   
  17               0      0              1          1              1         0   
  19               0      1              1          0              0         0   
  20               0      0              1          0              0         0   
  21               0      1              1          0              1         0   
  .....  
  61               0      1              1          0              5         0   
                 
我正在寻找一个k-means的实现,将用户分为3组,并将其行为分为已用类别。
我该怎么办

我想这就是你要找的

from pylab import plot,show
from numpy import vstack,array
from numpy.random import rand
import numpy as np
from scipy.cluster.vq import kmeans,vq
import pandas as pd
import pandas_datareader as dr
from math import sqrt
from sklearn.cluster import KMeans
from matplotlib import pyplot as plt
data = np.asarray([np.asarray(df['Feature1']),np.asarray(df['Feature2']),[np.asarray(df['Feature3'])]).T
X = data
distorsions = []
for k in range(2, 20):
    k_means = KMeans(n_clusters=k)
    k_means.fit(X)
    distorsions.append(k_means.inertia_)
fig = plt.figure(figsize=(15, 5))
plt.plot(range(2, 20), distorsions)
plt.grid(True)
plt.title('Elbow curve')

centroids,_ = kmeans(data,3)
# assign each sample to a cluster
idx,_ = vq(data,centroids)
# some plotting using numpy's logical indexing
plot(data[idx==0,0],data[idx==0,1],'ob',
     data[idx==1,0],data[idx==1,1],'oy',
     data[idx==2,0],data[idx==2,1])
plot(centroids[:,0],centroids[:,1],'sg',markersize=8)
show()

details = [(name,cluster) for name, cluster in zip(df.index,idx)]
for detail in details:
    print(detail)

到目前为止,您是否研究过k-means集群?scikit learn是python的机器学习工具。是的,我对它的工作原理有一些想法,但我不能用我的数据来实现。此外,k-means可以使用我的数据给出可解释的结果?谢谢你的回复!但它不起作用!这对我很管用。什么不起作用?这很模糊。ValueError:无法将字符串转换为float:“地图和导航”我甚至看不到“地图和导航”。你能发布你的数据帧吗?我需要一个小时才能将原始数据转换为实际的数据帧。我没有一个小时的时间来做这件事。谢谢!我为你浪费的时间向你道歉。