問(wèn)題描述
我只是想知道如何解決python3.6中的屬性錯(cuò)誤.
錯(cuò)誤是
I am just wondering how to solve the attribute error in python3.6.
The error is
'list' 對(duì)象沒(méi)有屬性 'astype'.
'list' object has no attribute 'astype'.
我的相關(guān)代碼也是爆款.
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
有人可以給我建議嗎?
謝謝!
推薦答案
根本問(wèn)題是 Python 列表和 NumPy 數(shù)組的混淆,它們是不同的數(shù)據(jù)類型.作為 np.foo(array)
調(diào)用的 NumPy 方法通常不會(huì)抱怨,如果你給他們一個(gè) Python 列表,他們會(huì)默默地將它轉(zhuǎn)換為 NumPy 數(shù)組.但是,如果您嘗試調(diào)用對(duì)象中包含的方法,例如 array.foo()
,那么它當(dāng)然必須已經(jīng)具有適當(dāng)?shù)念愋?
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 立即知道數(shù)組的類型.這避免了您首先創(chuàng)建一個(gè)數(shù)組然后將其轉(zhuǎn)換為另一種類型的不必要的工作.
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 對(duì)象像float32"這樣的字符串.
NumPy recommends using dtype objects instead of strings like "float32".
這篇關(guān)于如何解決 AttributeError:'list' 對(duì)象沒(méi)有屬性 'astype'?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!