Scipy Kmeans退出并出现TypeError [英] Scipy Kmeans exits with TypeError

查看:183
本文介绍了Scipy Kmeans退出并出现TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行下面的代码时,我收到一个TypeError:

When running the code below, I'm getting a TypeError that says:

scipy.cluster._vq.update_cluster_means中的文件"_vq.pyx",第342行 TypeError:不支持float或double类型之外的其他类型"

"File "_vq.pyx", line 342, in scipy.cluster._vq.update_cluster_means TypeError: type other than float or double not supported"

from PIL import Image
import scipy, scipy.misc, scipy.cluster

NUM_CLUSTERS = 5

im = Image.open('d:/temp/test.jpg')
ar = scipy.misc.fromimage(im)
shape = ar.shape
ar = ar.reshape(scipy.product(shape[:2]), shape[2])
codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
vecs, dist = scipy.cluster.vq.vq(ar, codes)
counts, bins = scipy.histogram(vecs, len(codes))

peak = codes[scipy.argmax(counts)]
print 'Most frequent color: %s (#%s)' % (peak, ''.join(chr(c) for c in peak).encode('hex'))

我不知道如何解决这个问题.

I have no idea how to fix this.

更新:

完整追溯:

Traceback (most recent call last): File "...\temp.py", line 110, in <module> codes, dist = scipy.cluster.vq.kmeans2(ar, NUM_CLUSTERS) File "...\site-packages\scipy\cluster\vq.py", line 642, in kmeans2 new_code_book, has_members = _vq.update_cluster_means(data, label, nc) File "_vq.pyx", line 342, in scipy.cluster._vq.update_cluster_means TypeError: type other than float or double not supported

Traceback (most recent call last): File "...\temp.py", line 110, in <module> codes, dist = scipy.cluster.vq.kmeans2(ar, NUM_CLUSTERS) File "...\site-packages\scipy\cluster\vq.py", line 642, in kmeans2 new_code_book, has_members = _vq.update_cluster_means(data, label, nc) File "_vq.pyx", line 342, in scipy.cluster._vq.update_cluster_means TypeError: type other than float or double not supported

推荐答案

操作:

ar = ar.reshape(scipy.product(shape[:2]), shape[2])
print(ar.dtype)

您将看到,您调用的数据类型为 uint8 的kmeans.

you will see, that you call kmeans with data of type uint8.

由于kmeans理论上是在d维 real 向量上定义的,因此scipy也不喜欢它(如错误所示)!

As kmeans, in theory, is defined on a d-dimensional real vector, scipy also does not like it (as given in the error)!

所以就这样做:

ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float)

像这样的铸造使我的示例一直运行到打印为止,还需要进行更改以反映给定的类型.

Casting like that is making my example run until the print, which also needs to be changed to reflect the given types.

这篇关于Scipy Kmeans退出并出现TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆