24

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?

7
  • label0 seems to be a float rather than an array.
    – cs95
    Aug 14, 2017 at 8:38
  • According to the documentation of np.cov, the parameter should have one row per variable and one column per observation, not the other way around.
    – jdehesa
    Aug 14, 2017 at 8:39
  • @COLDSPEED when I print the type of label0 ot says np.ndarray. Printing out the shape it says (50,3). So it's definitely not a float, but an array of floats haha
    – Mat Gomes
    Aug 14, 2017 at 8:45
  • 2
    @jdehesa That's the default settings yes, one row per variable and one column per observation. However, if u read the documentation, you can pass rowvar=False if yoh have the transpose of the matrix described.
    – Mat Gomes
    Aug 14, 2017 at 8:47
  • 3
    Just to be sure: what is label0.dtype? A minimal, complete and verifiable example would help--something that we can run to reproduce the error. Aug 14, 2017 at 8:47

2 Answers 2

31

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.

3
  • Spot on! Someone else already replied with this fix, but since you gave it ad an answer I will update it.
    – Mat Gomes
    Aug 14, 2017 at 8:53
  • 5
    "... check where it came from ..." When in doubt, suspect Pandas. Aug 14, 2017 at 8:53
  • @MatGomes I'm glad it worked. Don't forget to accept the most helpful answer if that answer resolved your issue.
    – MSeifert
    Aug 14, 2017 at 9:23
12

try

    label0.astype(float32)

and then calculate your cov.

It might because your dtype is object.

1
  • the float32 needs to be in quotes otherwise the code throws a NameError Mar 29, 2022 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.