Python 错误:使用序列设置数组元素。蟒蛇/麻木

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21803256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:39:31  来源:igfitidea点击:

Error: Setting an array element with a sequence. Python / Numpy

pythonarraysnumpy

提问by pceccon

I'm receiving this error when trying to assign an array to another array specific position. I was doing this before creating simple lists and doing such assignment. But Numpy is faster than simple lists and I was trying to use it now.

尝试将数组分配到另一个数组特定位置时收到此错误。在创建简单列表并进行此类分配之前,我正在这样做。但是 Numpy 比简单的列表更快,我现在正在尝试使用它。

The problem is cause I have a 2D array that stores some data and, in my code, I have, e.g., to calculate the gradient for each position value, so I create another 2D array where each position stores the gradient for its value.

问题是因为我有一个 2D 数组来存储一些数据,并且在我的代码中,我有,例如,计算每个位置值的梯度,所以我创建了另一个 2D 数组,其中每个位置存储其值的梯度。

import numpy as np

cols = 2
rows = 3

# This works
matrix_a = []

for i in range(rows):
    matrix_a.append([0.0] * cols)

print matrix_a    
matrix_a[0][0] = np.matrix([[0], [0]])    
print matrix_a

# This doesn't work
matrix_b = np.zeros((rows, cols)) 
print matrix_b   

matrix_b[0, 0] = np.matrix([[0], [0]])

What happens is 'cause I have a class defining a np.zeros((rows, cols))object, that stores information about some data, simplifying, e.g., images data.

发生的事情是因为我有一个定义np.zeros((rows, cols))对象的类,它存储有关某些数据的信息,简化了例如图像数据。

class Data2D(object):
    def __init__(self, rows=200, cols=300):
        self.cols = cols
        self.rows = rows
        # The 2D data structure
        self.data = np.zeros((rows, cols))

In a specific method, I have to calculate the gradient for this data, which is a 2 x 2 matrix (cause of this I would like to use ndarray, and not a simple array), and, to do this, I create another instance of this object to store this new data, in which each point (pixel) should store its gradient. I was using simple lists, which works, but I though I could gain some performance with numpy.

在特定方法中,我必须计算此数据的梯度,它是一个 2 x 2 矩阵(因此我想使用ndarray,而不是简单的array),为此,我创建了另一个实例这个对象来存储这个新数据,其中每个点(像素)应该存储它的梯度。我使用的是简单的列表,这很有效,但我虽然可以使用 numpy.js 获得一些性能。

There is a way to work around this? Or a better way to do such thing? I know that I can define the array type to object, but I don't know if I lose performance doing such thing.

有办法解决这个问题吗?或者更好的方法来做这样的事情?我知道我可以将数组类型定义为object,但我不知道这样做是否会降低性能。

Thank you.

谢谢你。

采纳答案by tinybike

The trouble is that matrix_b is defaulting to a float dtype. On my machine, checking

问题是 matrix_b 默认为 float dtype。在我的机器上,检查

matrix_b.dtype

returns dtype('float64'). To create a numpy array that can hold anything, you can manually set dtype to object, which will allow you to place a matrix inside of it:

返回dtype('float64')。要创建一个可以容纳任何东西的 numpy 数组,您可以手动将 dtype 设置为 object,这将允许您在其中放置一个矩阵:

matrix_b = np.zeros((rows, cols), dtype=object)
matrix_b[0, 0] = np.matrix([[0], [0], [1]])

回答by M4rtini

You could add another dimension of size 3 to your array.

您可以向数组中添加另一个大小为 3 的维度。

import numpy as np

cols = 2
rows = 3
matrix_b = np.zeros((rows, cols, 3)) 
matrix_b[0, 0] = np.array([0, 0, 1])
matrix_b[0, 0] = [0, 0, 1]  #This also works 

Another option is to set the dtype to listand then you can set each element to a list. But this is not really recommended, as you will lost much of the speed performance of numpy by doing this.

另一种选择是将 dtype 设置为list,然后您可以将每个元素设置为一个列表。但这并不是真正推荐的,因为这样做会失去 numpy 的大部分速度性能。

matrix_b = np.zeros((rows, cols), dtype=list) 
matrix_b[0, 0] = [0, 0, 1]