numpy.cov() exception: 'float' object has no attribute 'shape'

Mat Gomes picture Mat Gomes · Aug 14, 2017 · Viewed 22.7k times · Source

I have a dataset for different plant species, and I separated each species into a different np.array.

When trying to generate gaussian models out of these species, I had to calculate the means and covariance matrices for each different label.

The problem is: when using np.cov() in one of the labels, the function raises the error "'float' object has no attribute 'shape'" and I can't really figure out where the problem is coming from. The exact line of code I'm using is the following:

covx = np.cov(label0, rowvar=False)

Where label0 is a numpy ndarray of shape (50,3), where the columns represent different variables and each row is a different observation.

The exact error trace is:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-81-277aa1d02ff0> in <module>()
      2 
      3 # Get the covariances
----> 4 np.cov(label0, rowvar=False)

C:\Users\Matheus\Anaconda3\lib\site-packages\numpy\lib\function_base.py in cov(m, y, rowvar, bias, ddof, fweights, aweights)
   3062             w *= aweights
   3063 
-> 3064     avg, w_sum = average(X, axis=1, weights=w, returned=True)
   3065     w_sum = w_sum[0]
   3066 

C:\Users\Matheus\Anaconda3\lib\site-packages\numpy\lib\function_base.py in average(a, axis, weights, returned)
   1143 
   1144     if returned:
-> 1145         if scl.shape != avg.shape:
   1146             scl = np.broadcast_to(scl, avg.shape).copy()
   1147         return avg, scl

AttributeError: 'float' object has no attribute 'shape'

Any ideas of what is going wrong?

Answer

MSeifert picture MSeifert · Aug 14, 2017

The error is reproducible if the array is of dtype=object:

import numpy  as np

label0 = np.random.random((50, 3)).astype(object)
np.cov(label0, rowvar=False)

AttributeError: 'float' object has no attribute 'shape'

If possible you should convert it to a numeric type. For example:

np.cov(label0.astype(float), rowvar=False)  # works

Note: object arrays are rarely useful (they are slow and not all NumPy functions deal gracefully with these - like in this case), so it could make sense to check where it came from and also fix that.