Search this site
Embedded Files
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
  • More
    • Home
    • Courses
    • Contact

日本語  ❯

Lesson 5    ❮    Lesson List    ❮    Top Page

5.1 NumPy Arrays      

5.2 Index and Reshape

5.3 Universal Functions    

5.4 Join, Split, and Transpose

❯  5.5  Search and Filter      

⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳  Video   7m 3s
☷  Interactive readings   5m
✑  Practice 5.5 (G Colab)   25m

Filtering with Boolean Index

Getting some elements out of an existing array and creating a new array out of them is called filtering.

In NumPy, you filter an array using a boolean index list  An element is contained in the filtered array only if the value at an index is True. 

Use bitwise operator &, |, ~ for and, or, and not, respectively.

Using where for ndarray Comprehension

Using a list comprehension with if statement can be useful for filtering. While there is no ndarray comprehension, a similar task can be done using this method:

where(condition, x, y)
returns elements chosen from x or y depending on condition.

Making a Structured Array

Unlike list, NumPy can only work with 1 data type. We can force NumPy to work with multiple data types by assigning a dtype. The resulting array is called a structured array. 

Sorting ndarray

To sort ndarray, you can use:

np.sort(arr)
returns a sorted copy of an array.

©2023. All rights reserved.  Samy Baladram,
Graduate Program in Data Science - GSIS - Tohoku University
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse