python - “列表”对象没有属性 'reshape'

标签 python numpy

我一直在关注有关 python 的视频教程,但无论键入与视频完全相同的代码,我都会收到一个属性错误。

import numpy as np

A = np.array([[56.0, 0.0, 4,4, 68.0],
             [1.2, 104.0, 52.0, 8.0],
             [1.8, 135.0, 99.0, 0.9]])

cal = A.sum(axis=0)
percentage = 100*A/cal.reshape(1,4)
print(percentage)

属性错误指向百分比变量,说明列表没有属性'reshape'。我搜索了解决方案并在文档中看到它已被“从 0.19.0 版开始弃用。调用此方法会引发错误。请改为调用 .values.reshape(...)。” , 我根据文档提供的内容尝试了更新的版本。

percentage = 100*A/cal.values.reshape(1,4)

经过这次尝试,还是导致了属性错误。我不知道我是否做错了,因为我是 python 的新手。

最佳答案

我收到了这个警告:

VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. A = np.array([[56.0, 0.0, 4,4, 68.0],

根据这个问题: numpy.VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences

这与根据长度不等的列表创建数组有关。所以我认为在这一行中:

[56.0, 0.0, 4,4, 68.0]

4,4 应该是 4.4,对吧?

关于python - “列表”对象没有属性 'reshape',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71158544/

相关文章:

python - 使用 Python 读取 MS-Word 文件中页眉和页脚中的表格内容

python - 如何在 VSCode 中启用 Pylint?

python - 在 python scipy/numpy 中按行过滤矩阵元素

python - VTK 渲染窗口图像到 numpy 数组

python - 给定索引标签,如何提取数据框中的索引位置?

python -/: 'datetime.timedelta' and 'datedelta' 不支持的操作数类型

python - 拟合 RandomForestClassifier 时内存使用量激增

python - python中数组的导数?

python - 将 Numpy 多维数组添加在一起

python - numpy 除法的问题