Saturday, September 14, 2024

Introducing NumPy, Half 2: Indexing Arrays | by Lee Vaughan | Sep, 2024

Must read


Fast Success Knowledge Science

Slicing and dicing like a professional

Towards Data Science
A cartoon of a happy Rubik’s cube, representing an ndarray, shedding lots of little cubes.
Indexing an array by DALL-E3

NumPy is Python’s foundational library for numerical calculations. With NumPy, the heavy lifting is dealt with by arrays, primarily tables of parts of the identical information kind. Arrays are optimized for efficiency, allowing quicker mathematical and logical operations than conventional Python information varieties, like lists.

In Half 1, we coated how you can create arrays, describe them, and entry their attributes utilizing dot notation. On this article, we’ll look at how you can entry the weather in arrays utilizing indexes and slices, so you possibly can extract the worth of parts and alter them utilizing project statements. Array indexing makes use of sq. brackets [], identical to Python lists.

As a refresher from Half 1, here’s a graphical illustration of a 1D, 2D, and 3D array, with the axes annotated. You’ll want to know the axes’ instructions to index correctly.

Diagram showing graphic examples of a 1D NumPy array (as a sequence of numbers), a 2D array (as a table of numbers), and a 3D array (as a cube of numbers).
Graphical illustration of 1D, 2D, and 3D arrays (from Python Instruments for Scientists) (This and several other future hyperlinks to my e-book are affiliate hyperlinks)



Supply hyperlink

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article