Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[-]
Welcome
You have to register before you can post on our site.

Username:


Password:





[-]
Discord Widget

[-]
Twitter: SickGaming

[-]
Latest Threads
[Tut] pd.agg() – Aggregating Data in Pan...
Last Post: xSicKxBot
Yesterday 11:41 AM
» Replies: 0
» Views: 0
(Indie Deal) FREE SiT, Warhammer Festiva...
Last Post: xSicKxBot
Yesterday 11:41 AM
» Replies: 0
» Views: 0
PC - Deadly Premonition 2: A Blessing in...
Last Post: xSicKxBot
Yesterday 11:41 AM
» Replies: 0
» Views: 0
News - GTA 6 Rumors: Grand Theft Auto Re...
Last Post: xSicKxBot
Yesterday 11:41 AM
» Replies: 0
» Views: 0
[Tut] How to Initialize Multiple Variabl...
Last Post: xSicKxBot
06-30-2022 04:29 PM
» Replies: 0
» Views: 0
[Tut] How to Add Custom Field to Product...
Last Post: xSicKxBot
06-30-2022 04:29 PM
» Replies: 0
» Views: 0
PC - Sonic Origins
Last Post: xSicKxBot
06-30-2022 04:29 PM
» Replies: 0
» Views: 0
News - Destiny 2's Nerf Gjallarhorn Goes...
Last Post: xSicKxBot
06-30-2022 04:29 PM
» Replies: 0
» Views: 0
(Indie Deal) FREE ConflictCraft & FLASH ...
Last Post: xSicKxBot
06-29-2022 05:25 PM
» Replies: 0
» Views: 0
PC - Symphony of War: The Nephilim Saga
Last Post: xSicKxBot
06-29-2022 04:48 PM
» Replies: 0
» Views: 0

[Tut] How to Call an Element from a Numpy Array?

#1
How to Call an Element from a Numpy Array?

Rate this post

Problem: Given a Numpy array; how will you call an element from the given array?

Example: When you call an element from a Numpy array, the element being referenced is retrieved from a specified index. Let’s have a look at the following scenario, which demonstrates the concept:

Given:
my_array = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] Question: Retrieve the elements 3 and 8 from the given 2D array. Expected Output: [3 8] - The element 3 has been retrieved from row 0 and column 2.
- The element 8 has been retrieved from row 1 and column 2.

To master the art of retrieving elements from a Numpy array, you must have a clear picture of two essential concepts –
(1) Indexing Numpy arrays
(2) Slicing Numpy Arrays

In this tutorial, we will dive into numerous examples to conquer the above concepts and thereby learn how to call Numpy array elements in a practical way.

#NOTE: Before we begin, it is extremely important to note that indexing in Python always begins from 0, meaning the first element will have the index 0, the second element will have the index 1 and so on.

Retrieving Elements from a 1D Array


To access an element from a 1D array, you simply have to refer it using its index within square brackets, i.e., arr[i] where arr is the given array and i denotes the index of the element to be accessed.

Example:

import numpy as np arr = np.array([10, 20, 30, 40, 50])
# accessing the first array element at index 0
print(arr[0])
# accessing the middle array element at index 2
print(arr[2])
# accessing the last array element at index 0
print(arr[4])
# accessing and adding first and last element
print(arr[0]+arr[4])

Output:

10
30
50
60

The above examples were a classic case of indexing 1D array elements. But what if we need to access a contiguous group of elements from the given array. This is where slicing comes into the picture.

  • Slicing allows you to access elements starting from a given index until a specified end index.
  • Syntax: arr[start:end:step]
    • If start is not specified, then it is automatically considered as 0.
    • If end is not specified, then it is automatically considered as the length of the array in that dimension.
    • If step is not specified, then it is automatically considered as 1.

Example 1: Accessing the first three elements of a given 1D array.

import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print(arr[0:3])
# or
print(arr[:3]) # OUTPUT: [10 20 30]

Example 2: Accessing the last three elements of a given 1D array.

import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print(arr[7:])
# or
print(arr[7:]) # OUTPUT: [ 80 90 100]

Example 3: Accessing every other element of a given 1D array.

import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print(arr[0:10:2])
# or
print(arr[::2]) # OUTPUT: [10 30 50 70 90]

Retrieving Elements from a 2D Array


To retrieve elements from a given 2D Numpy array, you must access their row and column indices using the syntax arr[i,j], where arr represents the given array, i represents the row index and j represents the column index.

Examples:

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
# accessing the 3rd element of 1st row
print(arr[0, 2])
# accessing the 1st element of the 2nd row
print(arr[1, 0])
# accessing and adding 1st element of 1st row (1) and last element of second row (10)
print(arr[0, 0] + arr[1, 4])

Output:

3
6
11

Now let us look at how we can slice 2D arrays to access contiguous elements lying within an index range.

Example 1: Accessing the first three elements from the first inner array.


import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0, 0:3])
# or
print(arr[0, :3]) # OUTPUT: [1 2 3]

Example 2: Accessing the last three elements of the second inner array.


import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[1, 2:])
# or
print(arr[1, 2:]) # OUTPUT: [ 8 9 10]

Example 3: Access the third element from both the inner arrays.


import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, 2])
# or
print(arr[:, 2])
# or
print(arr[0:, 2])
# or
print(arr[:2, 2]) # OUTPUT: [3 8]

Example 4: Accessing middle elements from both the arrays.


import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, 1:4])
# or
print(arr[:, 1:4])
# or
print(arr[0:, 1:4])
# or
print(arr[:2, 1:4]) # OUTPUT: [[2 3 4]
[7 8 9]]

There’s one more way to select multiple array elements from a given 2D array. Considering that you want to retrieve elements from the i-th row and j-th column, you can pack them in a tuple to specify the indexes of each element you want to retrieve.

Example:

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[(0, 1), (2, 2)]) # OUTPUT: [3 8]

Explanation: The first tuple contains the indices of the rows and the second tuple contains the indices of the columns.

Retrieving Elements from a Multi-Dimensional Array


To retrieve elements of multi-dimensional arrays, you can access the index of individual elements with the help of square bracket notation and comma-separated index values, one per axis.

As a rule of thumb: the first element in the comma-separated square bracket notation identifies the outermost axis, the second element the second-outermost axis, and so on.

Example: In the following code we will access the third element from the second array of the second dimension.

import numpy as np arr = np.array([[[100, 200, 300], [400, 500, 600]], [[700, 800, 900], [1, 2, 3]]])
print(arr[1, 1, 2]) # OUTPUT: 3

Graphical Visualization:


πŸ–ŠNote: You must remember that each axis can be sliced separately. In case the slice notation is not specified for a particular axis, then the interpreter will automatically apply the default slicing (i.e., the colon :).

Accessing Elements Using Negative Indexing


You can also access elements of arrays using negative indices, starting from the end element and then moving towards the left.

Negative Indexing with 1D Arrays

Example 1: Accessing last element of a given array.

import numpy as np arr = np.array([10, 20, 30, 40, 50])
print(arr[-1]) # OUTPUT: 50

Example 2: Accessing the last three elements of a given array.

import numpy as np arr = np.array([10, 20, 30, 40, 50])
print(arr[-3:]) # OUTPUT: [30 40 50]

Negative Indexing with 2D Arrays

Example 1: Accessing last elements of both inner arrays.

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, -1]) # OUTPUT: [ 5 10]

Example 2: Accessing last three elements of both arrays.

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, -3:])

Output:

[[ 3 4 5] [ 8 9 10]]

Example 3: Access all columns except the last one.

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[:, :-1])

Output:

[[1 2 3 4] [6 7 8 9]]

πŸ’ŽADVANCED READ: Learn how to conditionally select elements in a NumPy Array here:
Conditional Indexing: How to Conditionally Select Elements in a NumPy Array?

Conclusion


Congratulations! You have successfully mastered the art of retrieving elements from arrays. We have seen numerous examples and demonstrations of selecting elements from 1D, 2D and other multi-dimensional arrays. I hope this tutorial helped you. Here’s a list of highly recommended tutorials that will further enhance your Numpy skills:


Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)

Coffee Break NumPy



https://www.sickgaming.net/blog/2022/06/...mpy-array/
Reply





Users browsing this thread:
1 Guest(s)

πš‚Μ·πš’Μ·πšŒΜ·πš”Μ·π™ΆΜ·πšŠΜ·πš–Μ·πš’Μ·πš—Μ·πšΜ· π™·Μ·πš˜Μ·πš–Μ·πšŽΜ·

Copyright Β© SickGaming.net 2022