whiten矩阵时报错:TypeError: cannot perform reduce with flexible type

用kmeans聚类之前需要对数据进行归一化处理,我的矩阵是从数据库中获取的,之后还经过一次转换,使用whiten对矩阵进行归一化的时候,出现错误:

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/cluster/vq.py", line 133, in whiten

    std_dev = std(obs, axis=0)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/fromnumeric.py", line 2817, in std

    keepdims=keepdims)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/_methods.py", line 116, in _std

    keepdims=keepdims)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/_methods.py", line 86, in _var

    arrmean = um.add.reduce(arr, axis=axis, dtype=dtype, keepdims=True)

TypeError: cannot perform reduce with flexible type


报错的主要原因是最后一句:TypeError: cannot perform reduce with flexible type

我打印了很多次矩阵发现我的矩阵没有dtype,打印出来是这样的:

array([['1', '25', '1', ..., '0', '1', '9011.92'],

       ['0', '28', '0', ..., '0', '0', '2400'],

       ['1', '34', '1', ..., '0', '1', '1.97'],

       ..., 

       ['1', '31', '1', ..., '0', '1', '0'],

       ['0', '23', '1', ..., '0', '1', '3700'],

       ['0', '41', '1', ..., '0', '1', '3700']], 

      dtype='|S9')

于是我猜测应该是dtype的问题,然后查了一下dtype,看到这篇文章后茅塞顿开:

http://www.cnblogs.com/hhh5460/p/5129032.html

原来用astype可以改变矩阵的dtype类型,于是我试着把我的矩阵的dtype改一下:

points = points.astype(float)

打印出来全是浮点数:

>>> print points

[[  1.00000000e+00   2.50000000e+01   1.00000000e+00 ...,   0.00000000e+00

    1.00000000e+00   9.01192000e+03]

 [  0.00000000e+00   2.80000000e+01   0.00000000e+00 ...,   0.00000000e+00

    0.00000000e+00   2.40000000e+03]

 [  1.00000000e+00   3.40000000e+01   1.00000000e+00 ...,   0.00000000e+00

    1.00000000e+00   1.97000000e+00]

 ..., 

 [  1.00000000e+00   3.10000000e+01   1.00000000e+00 ...,   0.00000000e+00

    1.00000000e+00   0.00000000e+00]

 [  0.00000000e+00   2.30000000e+01   1.00000000e+00 ...,   0.00000000e+00

    1.00000000e+00   3.70000000e+03]

 [  0.00000000e+00   4.10000000e+01   1.00000000e+00 ...,   0.00000000e+00

    1.00000000e+00   3.70000000e+03]]


再用whiten函数归一化,就没报错了:

>>> whiten(points)

array([[  2.12715108e+00,   2.19163343e+00,   1.25619815e+00, ...,

          0.00000000e+00,   1.25619815e+00,   7.88747918e-01],

       [  0.00000000e+00,   2.45462944e+00,   0.00000000e+00, ...,

          0.00000000e+00,   0.00000000e+00,   2.10054573e-01],

       [  2.12715108e+00,   2.98062147e+00,   1.25619815e+00, ...,

          0.00000000e+00,   1.25619815e+00,   1.72419795e-04],

       ..., 

       [  2.12715108e+00,   2.71762546e+00,   1.25619815e+00, ...,

          0.00000000e+00,   1.25619815e+00,   0.00000000e+00],

       [  0.00000000e+00,   2.01630276e+00,   1.25619815e+00, ...,

          0.00000000e+00,   1.25619815e+00,   3.23834133e-01],

       [  0.00000000e+00,   3.59427883e+00,   1.25619815e+00, ...,

          0.00000000e+00,   1.25619815e+00,   3.23834133e-01]])





完美~~~





  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值