Numpy.cov () exception: 'float' object does not have 'shape' attribute

I have a dataset for different types of plants, and I divided each species into another np.array.

When trying to generate Gaussian models from these types, I had to calculate the means and covariance matrices for each other label.

The problem is this: when used np.cov()in one of the labels, the function raises the error "object float" does not have the attribute "shape", and I can’t understand where this problem came from. The exact line of code I'm using is as follows:

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

Where label0is the numpy ndarray form (50.3), where the columns represent different variables, and each row represents a different observation.

Accurate error tracing:

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

, ?

+4
1

, dtype=object:

import numpy  as np

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

AttributeError: 'float' 'shape'

, . :

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

: object ( , NumPy - ), , , .

+6

Source: https://habr.com/ru/post/1683651/


All Articles