Bak*_*war 5 python numpy ipython pandas scikit-learn
我正在使用12个变量对约400K观测值运行K-means聚类。最初,当我使用Kmeans代码运行单元时,它会在2分钟后弹出一条消息,提示内核已中断并会重新启动。然后,需要花费一些时间,好像内核已经死了,并且代码将不再运行。
因此,我尝试使用125k观测值,但没有。变量。但是我仍然得到同样的信息。
这是什么意思?这是否意味着ipython Notebook无法在125k观测值上运行kmeans并杀死内核?
如何解决呢?这对我今天来说非常重要。:(
请指教。
我使用的代码:
从sklearn.cluster导入KMeans从sklearn.metrics导入silhouette_score
# Initialize the clusterer with n_clusters value and a random generator
# seed of 10 for reproducibility.
kmeans=KMeans(n_clusters=2,init='k-means++',n_init=10, max_iter=100)
kmeans.fit(Data_sampled.ix[:,1:])
cluster_labels = kmeans.labels_
# The silhouette_score gives the average value for all the samples.
# This gives a perspective into the density and separation of the formed
# clusters
silhouette_avg = silhouette_score(Data_sampled.ix[:,1:],cluster_labels)
Run Code Online (Sandbox Code Playgroud)
从一些调查来看,这可能与 iPython Notebook / Jupyter 无关。看来这是一个问题sklearn
,它可以追溯到一个问题numpy
。sklearn
请参阅此处和此处的相关 github 问题,以及此处的底层 numpy 问题。
最终,计算轮廓分数需要计算一个非常大的距离矩阵,并且对于大量行来说,距离矩阵似乎占用了系统上太多的内存。例如,在两次运行类似计算期间查看我的系统(OSX,8GB 内存)上的内存压力 - 第一个峰值是包含 10k 记录的 Silhouette Score 计算,第二个……稳定期 .. 是包含 40k 记录的:
根据此处相关的 SO 答案,您的内核进程可能会被操作系统杀死,因为它占用了太多内存。
最终,这将需要在底层代码库中对sklearn
和/或进行一些修复numpy
。您可以在此期间尝试一些选项:
或者,如果您比我聪明并且有一些空闲时间,请考虑尝试修复和sklearn
/或numpy
:)