0% found this document useful (0 votes)
11 views5 pages

02-Numpy Indexing and Selection

Uploaded by

Saif Wali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

02-Numpy Indexing and Selection

Uploaded by

Saif Wali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

02-Numpy Indexing and Selection

March 24, 2024

___

1 NumPy Indexing and Selection


In this lecture we will discuss how to select elements or groups of elements from an array.

[2]: import numpy as np

[3]: #Creating sample array


arr = np.arange(0,11)

[4]: #Show
arr

[4]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

1.1 Bracket Indexing and Selection


The simplest way to pick one or some elements of an array looks very similar to python lists:

[5]: #Get a value at an index


arr[8]

[5]: 8

[6]: #Get values in a range


arr[1:5]

[6]: array([1, 2, 3, 4])

[7]: #Get values in a range


arr[0:5]

[7]: array([0, 1, 2, 3, 4])

1.2 Broadcasting
Numpy arrays differ from a normal Python list because of their ability to broadcast:

1
[8]: #Setting a value with index range (Broadcasting)
arr[0:5]=100

#Show
arr

[8]: array([100, 100, 100, 100, 100, 5, 6, 7, 8, 9, 10])

[9]: # Reset array, we'll see why I had to reset in a moment


arr = np.arange(0,11)

#Show
arr

[9]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

[10]: #Important notes on Slices


slice_of_arr = arr[0:6]

#Show slice
slice_of_arr

[10]: array([0, 1, 2, 3, 4, 5])

[11]: #Change Slice


slice_of_arr[:]=99

#Show Slice again


slice_of_arr

[11]: array([99, 99, 99, 99, 99, 99])

Now note the changes also occur in our original array!

[12]: arr

[12]: array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])

Data is not copied, it’s a view of the original array! This avoids memory problems!

[13]: #To get a copy, need to be explicit


arr_copy = arr.copy()

arr_copy

[13]: array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])

2
1.3 Indexing a 2D array (matrices)
The general format is arr_2d[row][col] or arr_2d[row,col]. I recommend usually using the
comma notation for clarity.

[14]: arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))

#Show
arr_2d

[14]: array([[ 5, 10, 15],


[20, 25, 30],
[35, 40, 45]])

[15]: #Indexing row


arr_2d[1]

[15]: array([20, 25, 30])

[16]: # Format is arr_2d[row][col] or arr_2d[row,col]

# Getting individual element value


arr_2d[1][0]

[16]: 20

[17]: # Getting individual element value


arr_2d[1,0]

[17]: 20

[18]: # 2D array slicing

#Shape (2,2) from top right corner


arr_2d[:2,1:]

[18]: array([[10, 15],


[25, 30]])

[19]: #Shape bottom row


arr_2d[2]

[19]: array([35, 40, 45])

[20]: #Shape bottom row


arr_2d[2,:]

[20]: array([35, 40, 45])

3
1.3.1 Fancy Indexing
Fancy indexing allows you to select entire rows or columns out of order,to show this, let’s quickly
build out a numpy array:

[21]: #Set up matrix


arr2d = np.zeros((10,10))

[22]: #Length of array


arr_length = arr2d.shape[1]

[23]: #Set up array

for i in range(arr_length):
arr2d[i] = i

arr2d

[23]: array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],
[ 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.],
[ 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],
[ 5., 5., 5., 5., 5., 5., 5., 5., 5., 5.],
[ 6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],
[ 7., 7., 7., 7., 7., 7., 7., 7., 7., 7.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],
[ 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.]])

Fancy indexing allows the following

[24]: arr2d[[2,4,6,8]]

[24]: array([[ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],
[ 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],
[ 6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.]])

[25]: #Allows in any order


arr2d[[6,4,2,7]]

[25]: array([[ 6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],
[ 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],
[ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],
[ 7., 7., 7., 7., 7., 7., 7., 7., 7., 7.]])

1.4 More Indexing Help


Indexing a 2d matrix can be a bit confusing at first, especially when you start to add in step size.
Try google image searching NumPy indexing to fins useful images, like this one:

4
1.5 Selection
Let’s briefly go over how to use brackets for selection based off of comparison operators.

[28]: arr = np.arange(1,11)


arr

[28]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

[30]: arr > 4

[30]: array([False, False, False, False, True, True, True, True, True, True],
dtype=bool)

[31]: bool_arr = arr>4

[32]: bool_arr

[32]: array([False, False, False, False, True, True, True, True, True, True],
dtype=bool)

[33]: arr[bool_arr]

[33]: array([ 5, 6, 7, 8, 9, 10])

[34]: arr[arr>2]

[34]: array([ 3, 4, 5, 6, 7, 8, 9, 10])

[37]: x = 2
arr[arr>x]

[37]: array([ 3, 4, 5, 6, 7, 8, 9, 10])

2 Great Job!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy