【问题标题】:AttributeError: 'float' object has no attribute 'shape'AttributeError:“浮动”对象没有属性“形状”
【发布时间】:2018-09-23 16:30:07
【问题描述】:

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

【问题讨论】:

  • 请将您的代码粘贴为文本,而不是图像
  • 欢迎来到 Stackoverflow!为了得到一个好的答案,如果你提供一个minimal reproducible example(代码和数据)真的很有帮助。请将代码和数据都以文本的形式发布,而不是图像(搜索引擎不能很好地处理图像,我们无法将图像复制到我们的文本编辑器)。
  • 告诉我们R_indR_port - 他们是ndarray吗?如果是这样,shapedtype 是什么?
  • 类型是pandas.core.series.Series。
  • 是pd.DataFrame的一列

标签: python numpy sympy covariance


【解决方案1】:

np.cov 与 2 个一维数组有效:

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'

【讨论】:

  • 这很有帮助。谢谢
猜你喜欢
  • 2021-09-05
  • 2016-05-09
  • 2015-05-07
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 2017-05-03
相关资源
最近更新 更多