Advanced Indexing Techniques on NumPy Arrays - Learn NumPy Series

Опубликовано: 15 Октябрь 2024
на канале: Derrick Sherrill
27,039
634

This video is apart of a full Learn NumPy Series-
   • Introduction to NumPy Arrays for Begi...  

In this one we'll look at how we can begin using advanced indexing methods on our NumPy Arrays

#Python #NumPy #Tutorial

Join The Socials -- Picking Shoutouts Across YouTube, Insta, FB, and Twitter!
FB -   / codewithderrick  
Insta -   / codewithderrick  
Twitter -   / codewithderrick  
LinkedIn -   / derricksherrill  
GitHub - https://github.com/Derrick-Sherrill

We're at 4950+ Subscribers at the time of writing this! How awesome. Thanks so much everyone. Your support is phenomenal. Super honored by all the kind words and comments.

*****************************************************************
Full code from the video:
import numpy as np


row_1 = [1,2,3,4,5]
row_2 = [6,7,8,9,10]
row_3 = [11,12,13,14,15]
row_4 = [16,17,18,19,20]
row_5 = [21,22,23,24,25]

test_data = np.array([row_1,row_2,row_3,row_4,row_5])
print(test_data)

Using Python Slices
print(test_data[:,2:4:1])
Same Elements but reversed
print(test_data[:,-2:-4:-1])

#boolean index
greater_than_five = test_data != 5
returns one dimensional array
print(greater_than_five)
single line operation
print(test_data[greater_than_five])
print(test_data[test_data!=5])

But what if we wanted to retain shape?
drop_under_5_array = np.where(test_data != 5, test_data, 0)
print(drop_under_5_array)

Using Multiple Logic Conditions
drop_under_5_and_over_20 = np.logical_and(test_data!=5, test_data!=20)
YouTube Description doesn't allow angled brackets :(
print(drop_under_5_and_over_20)
print(test_data[drop_under_5_and_over_20])

https://github.com/Derrick-Sherrill/N...

Packages (& Versions) used in this video:
Python 3.7
NumPy 1.17

*****************************************************************
Code from this tutorial and all my others can be found on my GitHub:
https://github.com/Derrick-Sherrill/D...

Check out my website:
https://www.derricksherrill.com/

If you liked the video - please hit the like button. It means more than you know. Thanks for watching and thank you for all your support!!

Always looking for suggestions on what video to make next -- leave me a comment with your project! Happy Coding!