•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

# NumPy Help (Booleans)

numpy

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

### #1 KansaiRobot

KansaiRobot

CC Resident

• 82 posts

Posted 12 February 2015 - 06:08 PM

Hello everybody.

I have recently started learning NumPy (an extension of Python) and I am very confused by several things. In this post I would like help in one of them.

Let's say I have the following (forgive me if I do some mistake)

```>>>a=arange(12)
>>> a=a.reshape(3,4)
>>> a
array([[0,1,2,3],
[4,5,6,7],
[8,9,10,11]])

```

And also have some arrays of booleans say

```>>>b1=array([False,True,True])
>>>b2=array([True,False,True,False])
```

Now, I do the following

```>>>a[b1,:]
array([[4,5,6,7],
[8,9,10,11]])
>>>a[:,b2]
array([[0,2],
[4,6],
[8,10]])

```

which is understandable.

But, where on earth does the following come from???

```>>>a[b1,b2]
array([4,10]
```

I mean, I can not understand where does that come from and how did it end up like that. I would think that would give a result such as

```[[4,6]
[8,10]]
```

Can someone help me understand this? I will be very grateful. Thanks in advance

Edited by KansaiRobot, 12 February 2015 - 06:14 PM.

### #2 dargueta

dargueta

I chown trolls.

• Moderator
• 4854 posts

Posted 13 February 2015 - 12:34 PM

From the docs here:

Warning:

The definition of advanced indexing means that x[(1,2,3),] is fundamentally different than x[(1,2,3)]. The latter is equivalent to x[1,2,3] which will trigger basic selection while the former will trigger advanced indexing. Be sure to understand why this is occurs.

Looks like that might be your problem.

sudo rm -rf / && echo \$'Sanitize your inputs!'

### #3 KansaiRobot

KansaiRobot

CC Resident

• 82 posts

Posted 15 February 2015 - 04:44 PM

Can someone recommend me a basic tutorial on slicing...

Any recommendations on where to learn NumPy??

(I mean not "as basic" as i:j:k means from i to j step k- I understand that, but the other cryptic things from there... dimensions specially blow my mind  .)

Edited by KansaiRobot, 15 February 2015 - 06:26 PM.

### #4 dargueta

dargueta

I chown trolls.

• Moderator
• 4854 posts

Posted 16 February 2015 - 11:53 AM

sudo rm -rf / && echo \$'Sanitize your inputs!'

### #5 KansaiRobot

KansaiRobot

CC Resident

• 82 posts

Posted 16 February 2015 - 04:08 PM

Thank you  That is why I said "recommend". Unfortunately google still doesn't have the tecnhnology to incorporate a ranking of human opinions.

I have google it and either I find very basic ones (i:j:k means this...) and then a huge step to complicated examples. Read already a couple, not satisfied because at some point the examples become cryptic.

I suppose I have to be more specific.

If there is someone who has read or learn from some tutorial about slicing and have thought "WOW this is really good and understandable" and can share that source with me so that I can repeat the learning experience I would greatly appreciate it.

Thanks

### #6 dargueta

dargueta

I chown trolls.

• Moderator
• 4854 posts

Posted 17 February 2015 - 10:15 AM

Here's a tutorial series, Introducing NumPy Arrays. You'll probably only find a few of them useful, but I'll let you decide. Right underneath the video thumbnails there's a drop-down box where you can see the titles of the videos and jump straight to the one you want.

There's also a tutorial more comprehensive tutorial series here that doesn't do a whole lot of explaining but has a lot of examples you can work through.

sudo rm -rf / && echo \$'Sanitize your inputs!'

### #7 Grigorios

Grigorios

CC Lurker

• New Member
• 4 posts

Posted 23 March 2015 - 02:06 AM

A good book to start with NumPy is by  Jaan Kiusalaas "Numerical Methods in Engineering With Python 3"  Third Edition.

With this book you will get a good knowledge of how to use arrays in Python.

Enjoy programming with Python.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download