AttributeError:“浮动”对象没有属性“ shape”

时间:2018-09-23 16:30:08

标签: python numpy sympy covariance

我正在尝试使用python sympy.Solvers获得包含np.cov的方程的解决方案(W2),但得到AttributeError:'float'对象没有属性'shape'。

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

np.cov和2个1d数组有效:

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

但是如果数组之一是object dtype,我会收到您的错误:

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'