我必须在数据集上使用k表示聚类(我正在使用Scikit学习),如下所示
但是,当我应用K意味着它没有按预期给我质心。 并且分类不正确。 如果我想知道在scikit学习中没有正确分类的要点,那么会有什么想法。 这是代码。
km = KMeans(n_clusters=3, init='k-means++', max_iter=300, n_init=10) km.fit(Train_data.values) plt.plot(km.cluster_centers_[:,0],km.cluster_centers_[:,1],'ro') plt.show()这里Train_data是pandas框架,有2个功能和3500个样本,代码给出了以下内容。
我可能发生了因为初始质心选择不当但可能是什么解决方案?
I have to use k means clustering (I am using Scikit learn) on a dataset looks like this
But when I apply the K means it doesn't give me the centroids as expected. and classifies incorrectly. Also What would be the ideas if I want to know the points not correctly classify in scikit learn. Here is the code.
km = KMeans(n_clusters=3, init='k-means++', max_iter=300, n_init=10) km.fit(Train_data.values) plt.plot(km.cluster_centers_[:,0],km.cluster_centers_[:,1],'ro') plt.show()Here Train_data is pandas frame and having 2 features and 3500 samples and the code gives following.
I might have happened because of bad choice of initial centroids but what could be the solution ?
最满意答案
首先,我希望您注意到X和Y轴上的范围在两个图中都不同。 因此,第一个质心(按X值排序)并不是那么糟糕。 由于存在大量异常值,所以获得了第二个和第三个。 它们可能每个都占据最右边的一半。 此外,k-means的输出取决于质心的初始选择,因此请查看不同的运行或将init参数设置为随机可以改善结果。 提高效率的另一种方法是去除距离d的半径内具有少于n个邻居的所有点。 为了有效地实现这一点,你可能需要一个kd树,或者只是使用sklearn提供的DBSCAN ,看看它是否更好。
此外,K-Means ++可能会选择离群值作为初始簇,如此处所述。 因此,您可能希望将KMeans中的init参数更改为“随机”并执行多次运行并获取最佳质心。
对于您的数据,因为它是2-D,很容易知道点是否被正确分类。 使用鼠标“拾取”近似质心的坐标(参见此处 ),然后将从拾取坐标获得的聚类与从k-means获得的聚类进行比较。
I got a solution for this. The problem was scaling. I just scaled both axes using
sklearn.preprocessing.scaleAnd this is my result
更多推荐
发布评论