Skip to content
Related Articles

Related Articles

Accessing Data Along Multiple Dimensions Arrays in Python Numpy
  • Last Updated : 10 Jul, 2020

NumPy (Numerical Python) is a Python library that comprises of multidimensional arrays and numerous functions to perform various mathematical and logical operations on them. NumPy also consists of various functions to perform linear algebra operations and generate random numbers. NumPy is often used along with packages like SciPy and Matplotlib for technical computing.

An n-dimensional (multidimensional) array has a fixed size and contains items of the same type. the contents of the multidimensional array can be accessed and modified by using indexing and slicing the array as desired. For accessing elements of an array we need to first import the library:

import numpy as np

We can use Integer Indexing to access elements of data. We can also perform Slicing to access sub-sequences of data.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 1-dimensional array
array1D = np.array([1, 2, 3, 4, 5])
  
print(array1D)
  
# to access elements using positive
# index
print("\nusing positive index :" +str(array1D[0]))
print("using positive index :" +str(array1D[4]))
  
# negative indexing works in opposite
# direction
print("\nusing negative index :" +str(array1D[-5]))
print("using negative index :" +str(array1D[-1]))

chevron_right


Output :



[1 2 3 4 5]

using positive index :1
using positive index :5

using negative index :5
using negative index :1

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 2-dimensional array 
array2D = np.array([[9395],
                    [84, 100],
                    [9987]])
  
print(array2D)
print("shape :" +str(array2D.shape))
  
print("\npositive indexing :" +str(array2D[1, 0]))
print("negative indexing :" +str(array2D[-2, 0]))
  
print("\nslicing using positive indices :" +str(array2D[0:3, 1]))
print("slicing using positive indices :" +str(array2D[:, 1]))
print("slicing using negative indices :" +str(array2D[:, -1]))

chevron_right


Output :

[[ 93  95]
 [ 84 100]
 [ 99  87]]
shape :(3, 2)

positive indexing :84
negative indexing :84

slicing using positive indices :[ 95 100  87]
slicing using positive indices :[ 95 100  87]
slicing using negative indices :[ 95 100  87]

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 3-dimensional array 
array3D = np.array([[[ 012],
                     [ 345],
                     [ 678]],
   
                    [[ 9, 10, 11],
                     [12, 13, 14],
                     [15, 16, 17]],
  
                    [[18, 19, 20],
                     [21, 22, 23],
                     [24, 25, 26]]])
  
print(array3D)
print("shape :" +str(array3D.shape))
  
print("\nacessing element :" +str(array3D[0, 1, 0]))
print("acessing elements of a row and a column of an array:"
      +str(array3D[:, 1, 0]))
print("accessing sub part of an array :" +str(array3D[1]))

chevron_right


Output :

[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]]
shape :(3, 3, 3)
acessing element :3
acessing elements of a row and a column of an array:[ 3 12 21]
accessing sub part of an array :[[ 9 10 11]
 [12 13 14]
 [15 16 17]]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :