Python Numpy TypeError:输入类型不支持 ufunc 'isfinite'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40809503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:01:35  来源:igfitidea点击:

Python Numpy TypeError: ufunc 'isfinite' not supported for the input types

pythonarrayspandasnumpyeigenvalue

提问by swabygw

Here's my code:

这是我的代码:

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))

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:

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

enter image description here

在此处输入图片说明

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!)

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

回答by jmd_dk

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

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

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

to convert the dtype

转换 dtype