Python – AttributeError: ‘float’ object has no attribute ‘shape’

covariancenumpypythonsympy

I'm trying to get the solution(W2) of a equation, which include np.cov, using python sympy.Solvers, but get a AttributeError: 'float' object has no attribute 'shape'.

enter image description here

enter image description here

Best Solution

np.cov with 2 1d arrays works:

In [202]: np.cov(np.arange(10),np.arange(10))
Out[202]: 
array([[9.16666667, 9.16666667],
       [9.16666667, 9.16666667]])

But if one of the arrays is object dtype, I get your error:

In [203]: np.cov(np.arange(10),np.arange(10).astype(object))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-203-db3c70a7640d> in <module>()
----> 1 np.cov(np.arange(10),np.arange(10).astype(object))

/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in cov(m, y, rowvar, bias, ddof, fweights, aweights)
   2300             w *= aweights
   2301 
-> 2302     avg, w_sum = average(X, axis=1, weights=w, returned=True)
   2303     w_sum = w_sum[0]
   2304 

/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in average(a, axis, weights, returned)
    389 
    390     if returned:
--> 391         if scl.shape != avg.shape:
    392             scl = np.broadcast_to(scl, avg.shape).copy()
    393         return avg, scl

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