Python 如何将numpy数组的数据类型更改为';对象';?
我的目标是这样做:Python 如何将numpy数组的数据类型更改为';对象';?,python,numpy,object,Python,Numpy,Object,我的目标是这样做: weights[1][0][0] = some_object(1) 但它抛出了一个错误: TypeError: float() argument must be a string or a number, not 'some_object' 因此,我想将数据类型更改为“object” 在我的代码中,我有权重。它们看起来像这样: >>> print(weights) [ array([[-2.66665269, 0. ], [-0.3
weights[1][0][0] = some_object(1)
但它抛出了一个错误:
TypeError: float() argument must be a string or a number, not 'some_object'
因此,我想将数据类型更改为“object”
在我的代码中,我有权重。它们看起来像这样:
>>> print(weights)
[ array([[-2.66665269, 0. ],
[-0.36358187, 0. ],
[ 1.55058871, 0. ],
[ 3.91364328, 0. ]])
array([[ 0.],
[ 0.]])]
我想将权重[1][0][0]
更改为对象。我正在引用中的0:
[ array([[-2.66665269, 0. ],
[-0.36358187, 0. ],
[ 1.55058871, 0. ],
[ 3.91364328, 0. ]])
array([[ 0.], #this zero right here
[ 0.]])]
我想将该0转换为某个_对象(1)。所以我将数据类型更改为“object”,但它仍然是浮动的
>>> weights=np.array(weights,dtype='object')
>>> weights.dtype
object
>>> weights[1].dtype
float64
>>> weights[1][0].dtype
float64
>>> weights[1][0][0].dtype
float64
所以现在我试着:
>>> weights[1].dtype='object'
TypeError: Cannot change data-type for object array.
还有这个
>>> weights[1][0].dtype='object'
TypeError: Cannot change data-type for object array.
所以现在我不能这样做:
weights[1][0][0] = some_object(1)
TypeError: float() argument must be a string or a number, not 'object'
因为数据类型不正确。如何更改数据类型
编辑:我找到了答案
weights[1] = weights[1].tolist()
weights[1][0][0] = some_object(1)
....
weights=np.array(weights)
让我们确保我们了解您从什么开始:
In [7]: weights
Out[7]:
[array([[-2.66665269, 0. ],
[-0.36358187, 0. ],
[ 1.55058871, 0. ],
[ 3.91364328, 0. ]]), array([[ 0.],
[ 0.]])]
In [8]: len(weights)
Out[8]: 2
In [9]: weights[0]
Out[9]:
array([[-2.66665269, 0. ],
[-0.36358187, 0. ],
[ 1.55058871, 0. ],
[ 3.91364328, 0. ]])
In [10]: weights[0].dtype
Out[10]: dtype('float64')
In [11]: weights[0].shape
Out[11]: (4, 2)
In [13]: weights[1]
Out[13]:
array([[ 0.],
[ 0.]])
In [14]: weights[1].dtype
Out[14]: dtype('float64')
In [15]: weights[1].shape
Out[15]: (2, 1)
这是一个2项列表,包含两个数组。两者都是二维浮点数
首先,将整个列表包装到对象数组中:
In [16]: duh =np.array(weights,dtype='object')
In [17]: duh
Out[17]:
array([ array([[-2.66665269, 0. ],
[-0.36358187, 0. ],
[ 1.55058871, 0. ],
[ 3.91364328, 0. ]]),
array([[ 0.],
[ 0.]])], dtype=object)
这是一个2元素数组,形状(2,)。但它不会改变元素的性质。还有一个潜在的问题-如果元素数组具有相同的形状,它将创建一个3d对象数组
这不是更改数组数据类型的正确语法<代码>数据类型不是可写属性
weights[1].dtype='object'
我们可以使用astype
:
In [19]: weights[1].astype(object)
Out[19]:
array([[0.0],
[0.0]], dtype=object)
In [20]: weights[1]=weights[1].astype(object)
In [21]: weights
Out[21]:
[array([[-2.66665269, 0. ],
[-0.36358187, 0. ],
[ 1.55058871, 0. ],
[ 3.91364328, 0. ]]), array([[0.0],
[0.0]], dtype=object)]
它生成了一个新数组,我们必须将其写回原始列表中
现在我可以更改第二个数组的一个元素
In [22]: weights[1][0,0]=None
In [23]: weights
Out[23]:
[array([[-2.66665269, 0. ],
[-0.36358187, 0. ],
[ 1.55058871, 0. ],
[ 3.91364328, 0. ]]), array([[None],
[0.0]], dtype=object)]
在玩这样的游戏时,你必须注意数组的位置和列表的位置。并注意阵列的形状和数据类型。不要盲目地编制索引,并抱着最好的希望。显示/打印这些属性,如果数组不太大,则显示/打印整个数组。dtype不是可写属性/属性。-除非数据类型是或试图成为对象,否则为<代码>A=零(10,dtype=“u2”);A.dtype=“u1”工作正常(在numpy 1.15.2中测试)