Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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.

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.

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.

Thank you.

like image 827
pceccon Avatar asked Feb 15 '14 20:02

pceccon


2 Answers

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

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.

matrix_b = np.zeros((rows, cols), dtype=list) 
matrix_b[0, 0] = [0, 0, 1]
like image 120
M4rtini Avatar answered Oct 21 '22 23:10

M4rtini


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

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:

matrix_b = np.zeros((rows, cols), dtype=object)
matrix_b[0, 0] = np.matrix([[0], [0], [1]])
like image 23
tinybike Avatar answered Oct 21 '22 22:10

tinybike