Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumPy - Set values in structured array based on other values in structured array

Tags:

python

numpy

I have a structured NumPy array:

a = numpy.zeros((10, 10), dtype=[
    ("x", int),
    ("y", str)])

I want to set values in a["y"] to either "hello" if the corresponding value in a["x"] is negative. As far as I can tell, I should be doing that like this:

a["y"][a["x"] < 0] = "hello"

But this seems to change the values in a["x"]! What is the problem with what I'm doing, and how else should I do this?

like image 384
Matthew Avatar asked Aug 09 '14 14:08

Matthew


1 Answers

First of all, in numpy structured arrays, when you specify datatype as str numpy assumes it to be a 1 character string.

>>> a = numpy.zeros((10, 10), dtype=[
        ("x", int), 
        ("y", str)])

>>> print a.dtype
dtype([('x', '<i8'), ('y', 'S')])

As a result the values you enter get truncated to 1 character.

>>> a["y"][0][0] = "hello"
>>> print a["y"][0][0]
h

Hence use data type as a10, Where 10 being the max length of your string.

Refer this link, which specifies more definitions for other data structures.

Secondly your approach seems correct to me.

Inititating a structured numpy array with datatype int and a10

>>> a = numpy.zeros((10, 10), dtype=[("x", int), ("y", 'a10')])

Filling it with random numbers

>>> a["x"][:] = numpy.random.randint(-10, 10, (10,10))
>>> print a["x"]
 [[  2  -4 -10  -3  -4   4   3  -8 -10   2]
 [  5  -9  -4  -1   9 -10   3   0  -8   2]
 [  5  -4 -10 -10  -1  -8  -1   0   8  -4]
 [ -7  -3  -2   4   6   6  -8   3  -8   8]
 [  1   2   2  -6   2  -9   3   6   6  -6]
 [ -6   2  -8  -8   4   5   8   7  -5  -3]
 [ -5  -1  -1   9   5  -7   2  -2  -9   3]
 [  3 -10   7  -8  -4  -2  -4   8   5   0]
 [  5   6   5   8  -8   5 -10  -6  -2   1]
 [  9   4  -8   6   2   4 -10  -1   9  -6]]

Applying your filtering

>>> a["y"][a["x"]<0] = "hello"
>>> print a["y"]
[['' 'hello' 'hello' 'hello' 'hello' '' '' 'hello' 'hello' '']
 ['' 'hello' 'hello' 'hello' '' 'hello' '' '' 'hello' '']
 ['' 'hello' 'hello' 'hello' 'hello' 'hello' 'hello' '' '' 'hello']
 ['hello' 'hello' 'hello' '' '' '' 'hello' '' 'hello' '']
 ['' '' '' 'hello' '' 'hello' '' '' '' 'hello']
 ['hello' '' 'hello' 'hello' '' '' '' '' 'hello' 'hello']
 ['hello' 'hello' 'hello' '' '' 'hello' '' 'hello' 'hello' '']
 ['' 'hello' '' 'hello' 'hello' 'hello' 'hello' '' '' '']
 ['' '' '' '' 'hello' '' 'hello' 'hello' 'hello' '']
 ['' '' 'hello' '' '' '' 'hello' 'hello' '' 'hello']]

Verifying a["x"]

>>> print a["x"]
[[  2  -4 -10  -3  -4   4   3  -8 -10   2]
 [  5  -9  -4  -1   9 -10   3   0  -8   2]
 [  5  -4 -10 -10  -1  -8  -1   0   8  -4]
 [ -7  -3  -2   4   6   6  -8   3  -8   8]
 [  1   2   2  -6   2  -9   3   6   6  -6]
 [ -6   2  -8  -8   4   5   8   7  -5  -3]
 [ -5  -1  -1   9   5  -7   2  -2  -9   3]
 [  3 -10   7  -8  -4  -2  -4   8   5   0]
 [  5   6   5   8  -8   5 -10  -6  -2   1]
 [  9   4  -8   6   2   4 -10  -1   9  -6]]
like image 50
Raghav RV Avatar answered Oct 17 '22 21:10

Raghav RV