Python Numpy TypeError:输入类型不支持 ufunc 'isfinite' [英] Python Numpy TypeError: ufunc 'isfinite' not supported for the input types

查看:78
本文介绍了Python Numpy TypeError:输入类型不支持 ufunc 'isfinite'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

def topK(dataMat,sensitivity):
    meanVals = np.mean(dataMat, axis=0)
    meanRemoved = dataMat - meanVals
    covMat = np.cov(meanRemoved, rowvar=0)
    eigVals,eigVects = np.linalg.eig(np.mat(covMat))

我在上面最后一行的标题中看到错误.我怀疑与数据类型有关,因此,这是来自 Spyder 中的变量资源管理器的变量和数据类型的图像:

I get the error in the title on the last line above. I suspect has something to do with the datatype, so, here's an image of the variable and datatype from the Variable Explorer in Spyder:

我尝试将 np.linalg.eig(np.mat(covMat)) 更改为 np.linalg.eig(np.array(np.mat(covMat)))np.linalg.eig(np.array(covMat)),没有任何效果.有任何想法吗?(一个例子会很棒!)

I've tried changing np.linalg.eig(np.mat(covMat)) to np.linalg.eig(np.array(np.mat(covMat))) and to np.linalg.eig(np.array(covMat)), nothing works. Any ideas? (an example would be great!)

推荐答案

你的数组有一个 dtypeobject,但这应该是一些浮点 dtype.使用例如

Your array has a dtype of object, but this should be some floating point dtype. Use e.g.

covMat = np.array(covMat, dtype=float)

转换dtype

这篇关于Python Numpy TypeError:输入类型不支持 ufunc 'isfinite'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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