11

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.

1
  • 2
    Receiving what error? Provide full traceback. Commented Feb 15, 2014 at 20:41

2 Answers 2

21

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]])
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I know. But I though I'd lose a lot of performance doing such thing.
8

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]

1 Comment

Thank you. I just don't know if this is the better solution. I guess I'll have to change all my code and it may gets unreadable. :S

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.