错误:使用序列设置数组元素。Python / Numpy

时间:2021-02-04 18:03:24

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.

问题是,我有一个二维数组来存储一些数据,在我的代码中,我有,例如,计算每个位置值的梯度,所以我创建了另一个二维数组,每个位置都为它的值存储梯度。

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的类。0(行,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,而不是一个简单的数组),而要做到这一点,我创建另一个实例对象来存储新的数据,在每一个点(像素)应该存储它的梯度。我使用的是简单的列表,这是可行的,但是我认为我可以通过numpy获得一些性能。

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.

谢谢你!

2 个解决方案

#1


17  

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

问题是,matrix_b默认为浮点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]])

#2


7  

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 list and 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,然后可以将每个元素设置为list。但这并不是真正推荐的,因为这样做会丢失numpy的很多速度性能。

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

#1


17  

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

问题是,matrix_b默认为浮点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]])

#2


7  

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 list and 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,然后可以将每个元素设置为list。但这并不是真正推荐的,因为这样做会丢失numpy的很多速度性能。

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