Python Numpy TypeError:输入类型不支持ufunc'isfinite' [英] Python Numpy TypeError: ufunc 'isfinite' not supported for the input types
问题描述
这是我的代码:
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!)
推荐答案
您的数组的dtype
为object
,但这应该是一些浮点数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屋!