如何解决AttributeError:'list'对象没有属性'astype'? [英] How to solve the AttributeError:'list' object has no attribute 'astype'?

查看:316
本文介绍了如何解决AttributeError:'list'对象没有属性'astype'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何解决 python3.6中的属性错误.错误是

I am just wondering how to solve the attribute error in python3.6. The error is

列表"对象没有属性类型".

'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数组,它们是不同的数据类型.如果您给他们一个Python列表,通常以 np.foo(array)调用的NumPy方法不会抱怨,它们会静默地将其转换为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'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆