How to solve the AttributeError:#39;list#39; object has no attribute #39;astype#39;?(如何解决 AttributeError:list 对象没有属性 astype?)
问题描述
我只是想知道如何解决python3.6中的属性错误.
错误是
I am just wondering how to solve the attribute error in python3.6.
The error is
'list' 对象没有属性 'astype'.
'list' object has no attribute 'astype'.
我的相关代码也是爆款.
My related code is as blow.
def _init_mean_std(self, data):
data = data.astype('float32')
self.mean, self.std = np.mean(data), np.std(data)
self.save_meanstd()
return data
有人可以给我建议吗?
谢谢!
推荐答案
根本问题是 Python 列表和 NumPy 数组的混淆,它们是不同的数据类型.作为 np.foo(array)
调用的 NumPy 方法通常不会抱怨,如果你给他们一个 Python 列表,他们会默默地将它转换为 NumPy 数组.但是,如果您尝试调用对象中包含的方法,例如 array.foo()
,那么它当然必须已经具有适当的类型.
The root issue is confusion of Python lists and NumPy arrays, which are different data types. NumPy methods that are invoked as np.foo(array)
usually won't complain if you give them a Python list, they will convert it to an NumPy array silently. But if you try to invoke a method contained in the object, like array.foo()
then of course it has to have the appropriate type already.
我建议使用
data = np.array(data, dtype=np.float32)
以便 NumPy 立即知道数组的类型.这避免了您首先创建一个数组然后将其转换为另一种类型的不必要的工作.
so that the type of an array is known to NumPy at once. This avoids unnecessary work where you first create an array and then cast it to another type.
NumPy 建议使用 dtype 对象像float32"这样的字符串.
NumPy recommends using dtype objects instead of strings like "float32".
这篇关于如何解决 AttributeError:'list' 对象没有属性 'astype'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何解决 AttributeError:'list' 对象没有属性 'astype'?
基础教程推荐
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01