stacktrace告诉我们唯一支持的数据类型scipy.cluster._vq.update_cluster_means()
是float
和double
.检查scipy的源代码确认了这一点:
def update_cluster_means(np.ndarray obs, np.ndarray labels, int nc): """ The update-step of K-means. Calculate the mean of observations in each cluster. Parameters ---------- obs : ndarray The observation matrix. Each row is an observation. Its dtype must be float32 or float64. ...
来源:GitHub上的_vq.pyx
要解决您的问题,首先需要使用numpy.ndarray.astype()将输入转换为支持的数据类型:
codes, dist = scipy.cluster.vq.kmeans(ar.astype(float), NUM_CLUSTERS) # Or: = scipy.cluster.vq.kmeans(ar.astype('double'), NUM_CLUSTERS)