Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Chowdhury Sayeb Islam
Chowdhury Sayeb Islam

Posted on

     

Numpy Indexing and Selection

NumPy Indexing and Selection

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

import numpy as np
Enter fullscreen modeExit fullscreen mode
#Creating sample arrayarr = np.arange(0,11)#Showarr
Enter fullscreen modeExit fullscreen mode

Bracket Indexing and Selection

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

#Get values in a rangearr[1:5]#Get values in a rangearr[0:5]
Enter fullscreen modeExit fullscreen mode

Broadcasting

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

#Setting a value with index range (Broadcasting)arr[0:5]=100#Showarr
Enter fullscreen modeExit fullscreen mode
# Reset array, we'll see why I had to reset in  a momentarr = np.arange(0,11)#Showarr
Enter fullscreen modeExit fullscreen mode
#Important notes on Slicesslice_of_arr = arr[0:6]#Show sliceslice_of_arr
Enter fullscreen modeExit fullscreen mode
#Change Sliceslice_of_arr[:]=99#Show Slice againslice_of_arr
Enter fullscreen modeExit fullscreen mode

Now notice that the changes also occur in our original array!

arr
Enter fullscreen modeExit fullscreen mode

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

#To get a copy, need to be explicitarr_copy = arr.copy()arr_copy
Enter fullscreen modeExit fullscreen mode

Indexing a 2D array (matrices)

The general format isarr_2d[row][col] orarr_2d[row,col]. I recommend usually using the comma notation for clarity.

arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))#Showarr_2d
Enter fullscreen modeExit fullscreen mode
#Indexing rowarr_2d[1]
Enter fullscreen modeExit fullscreen mode
# Format is arr_2d[row][col] or arr_2d[row,col]# Getting individual element valuearr_2d[1][0]
Enter fullscreen modeExit fullscreen mode
# Getting individual element valuearr_2d[1,0]
Enter fullscreen modeExit fullscreen mode
# 2D array slicing#Shape (2,2) from top right cornerarr_2d[:2,1:]
Enter fullscreen modeExit fullscreen mode
#Shape bottom rowarr_2d[2]
Enter fullscreen modeExit fullscreen mode
#Shape bottom rowarr_2d[2,:]
Enter fullscreen modeExit fullscreen mode

Fancy Indexing

Fancy indexing allows one to select entire rows or columns out of order. To show this, let's quickly build out a NumPy Array:

#Set up matrixarr2d = np.zeros((10,10))
Enter fullscreen modeExit fullscreen mode
#Length of arrayarr_length = arr2d.shape[1]
Enter fullscreen modeExit fullscreen mode
#Set up arrayfor i in range(arr_length):    arr2d[i] = iarr2d
Enter fullscreen modeExit fullscreen mode

Fancy indexing allows the following

arr2d[[2,4,6,8]]
Enter fullscreen modeExit fullscreen mode
#Allows in any orderarr2d[[6,4,2,7]]
Enter fullscreen modeExit fullscreen mode

More Indexing

Indexing a 2D Matrix can be a bit confusing at first, especially when you start to add in step size.

Selection

Let's briefly go over how to use brackets for selection based off of comparison operators.

arr = np.arange(1,11)arr
Enter fullscreen modeExit fullscreen mode
arr > 4
Enter fullscreen modeExit fullscreen mode
bool_arr = arr>4
Enter fullscreen modeExit fullscreen mode
bool_arr
Enter fullscreen modeExit fullscreen mode
arr[bool_arr]
Enter fullscreen modeExit fullscreen mode
arr[arr>2]
Enter fullscreen modeExit fullscreen mode
x = 2arr[arr>x]
Enter fullscreen modeExit fullscreen mode
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Learning new skills. Help me to grow.
  • Education
    Brac University
  • Work
    Undergraduate Student
  • Joined

More fromChowdhury Sayeb Islam

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp