Python Numpy TypeError: ufunc 'isfinite' not supported for the input types
Question:
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 it has something to do with the datatype, so, here’s an image of the variable and datatype from the Variable Explorer in Spyder:
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?
Answers:
Your array has a dtype
of object
, but this should be some floating point dtype
. Use e.g.
covMat = np.array(covMat, dtype=float)
to convert the dtype
If you got this error, that means the function you called used np.isfinite
under the hood. Common functions that do so are np.allclose
, np.isclose
and linalg
methods (as in the OP). As jmd_dk’s answer mentions, it usually means the dtype of your array is object
.
A common fix is to cast the array to float
dtype.
covMat = covMat.astype(float)
Another common case is when concatenation / stack goes wrong or when the array comes from another datatype (e.g. pandas dataframes). In that case, try converting to a list first and then cast to np.ndarray
.
arr = np.array(arr.tolist())
For example, for the following case, straightforward recasting won’t work but converting to a list and then into an ndarray works.
arr = pd.Series([np.array([1,2]), np.array([3,4]), np.array([5,6])]).values
# array([array([1, 2]), array([3, 4]), array([5, 6])], dtype=object)
np.array(arr, dtype=float) # <---- error
np.array(arr.tolist(), dtype=float) # <---- no error
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 it has something to do with the datatype, so, here’s an image of the variable and datatype from the Variable Explorer in Spyder:
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?
Your array has a dtype
of object
, but this should be some floating point dtype
. Use e.g.
covMat = np.array(covMat, dtype=float)
to convert the dtype
If you got this error, that means the function you called used np.isfinite
under the hood. Common functions that do so are np.allclose
, np.isclose
and linalg
methods (as in the OP). As jmd_dk’s answer mentions, it usually means the dtype of your array is object
.
A common fix is to cast the array to float
dtype.
covMat = covMat.astype(float)
Another common case is when concatenation / stack goes wrong or when the array comes from another datatype (e.g. pandas dataframes). In that case, try converting to a list first and then cast to np.ndarray
.
arr = np.array(arr.tolist())
For example, for the following case, straightforward recasting won’t work but converting to a list and then into an ndarray works.
arr = pd.Series([np.array([1,2]), np.array([3,4]), np.array([5,6])]).values
# array([array([1, 2]), array([3, 4]), array([5, 6])], dtype=object)
np.array(arr, dtype=float) # <---- error
np.array(arr.tolist(), dtype=float) # <---- no error