Python Numpy and N-dimensional array operations-2

Part-1 of numpy series blog introduces python numpy library and creation of multidimensional array using Numpy. This blog post focuses on accessing array elements using operations like slicing and indexing.

Array elements can be accessed using indexing, square brackets ([]) are used for indexing.  This indexing using square brackets is similar to Python lists, index zero returns the first element in the one-dimensional array and index one give the second element of the array.

import numpy as np
one_array = np.arange (11)*4
print (one_array)

Output:
[ 0  4  8 12 16 20 24 28 32 36 40]

print ("Index zero elemment is: ",one_array[0])
print ("Index One elemment is: ",one_array[1])
print ("Index 4 elemment is: ",one_array[4])

Output:
Index zero elemment is:  0
Index One elemment is:  4
Index 4 elemment is:  16
Index -1 elemment is:  40

An array index can be a negative number, Index -1 returns the final element in the one-dimensional array.

Accessing subset of an array (Slicing) :

A subset of the numpy array can be accessed by specifying the range. When the range is specified for accessing subset end index is excluded while returning the subset. In the below example we are accessing index 2 to 7. The returned array contains elements from index 2 to index 6 (excluding 7). The answer is  [ 8 12 16 20 24].

one_array = np.arange (11)*4
print (one_array)

Output:
[ 0  4  8 12 16 20 24 28 32 36 40]

sub_set = one_array [2:7]
print (sub_set)
Output:
Subset returned is : [ 8 12 16 20 24]

sub_set = one_array [2:-2]
print ("Subset returned is with negative end index  :",sub_set)
Output:
Subset returned is with negative end index : [ 8 12 16 20 24 28 32]
sub_set = one_array [:7]
print ("Subset returned is with omiting start index:",sub_set)
Output:
Subset returned is with omiting start index: [ 0  4  8 12 16 20 24]

sub_set = one_array [1:]
print ("Subset returned is with omiting end index:",sub_set)

Output:
Subset returned is with omiting end index: [ 4  8 12 16 20 24 28 32 36 40]



if the start index is not specified to access the subset then it returns from the beginning of the array, and if the end index is not specified it returns all the elements until the end of the array. Array subset can be extracted with range also.  The syntax to specify the range is

[start_index: end_index: range]

Below example shows slicing of the array using a range, in this example the range specified is ‘3’. numpy returns every third element from index 1 till index 6 (excluding index 7).

one_array = np.arange (11)*4

print (one_array)
Output:
[ 0  4  8 12 16 20 24 28 32 36 40]

sub_set = one_array [1:7:3]
print (sub_set)
Output:
[ 4 16]

Multidimensional array:

Indexing multidimensional array is different when compared to indexing one-dimensional array. In the below-given example, we are creating a 3×3 array which contains mixed data strings, integers, and floating point numbers.

multi_array = np.array ([["c2plabs","robotics","machine"],[23,43,45],[12.08,56.32,89.21]])
print (multi_array)
print ("shape of array",np.shape(multi_array))
Output:
[['c2plabs' 'robotics' 'machine']
 ['23' '43' '45']
 ['12.08' '56.32' '89.21']]
shape of array (3, 3)

print (multi_array[0]) # Printing Index zero of the array.

Output: ['c2plabs' 'robotics' 'machine']

print (multi_array[1]) # Printing Index one of the array.

Output: ['23' '43' '45']

print (multi_array[2]) # Printing Index two of the array.

Output: ['12.08' '56.32' '89.21']

print (multi_array[0,1])
Output: robotics

Here “multi_array”  is a 3×3 array. Index zero of multi_array returns first row of the array, index one returns second row and index two returns third row of the array. In multidimensional arrays to access a single element, we have to give two indices in square brackets.

multi_array[0,1] gives element at first row and a second column which is robotics in this case. Similarly, the range of indexing can be used to access subarray of the multidimensional array.

multi_array = np.array ([["c2plabs","robotics","machine","learn"],[23,43,45,34],[12.08,56.32,89.21,56.12],['a','b','c','d']])
print (multi_array)

Output:

[['c2plabs' 'robotics' 'machine' 'learn']
 ['23' '43' '45' '34']
 ['12.08' '56.32' '89.21' '56.12']
 ['a' 'b' 'c' 'd']]

print (multi_array [0:2,2:4])
Output:
array([['machine', 'learn'],
       ['45', '34']], dtype='<U8')

multi_array [0:2,2:4] specifies  return the common elements in rows 0,1 (excluding 2) and columns 2,3 (excluding column 4) from the above array.

[‘c2plabs’ ‘robotics’ ‘machine’ ‘learn’]    # This is row 0

[’23’              ’43’          ’45’           ’34’]        # This is row 1

In the above type of indexing, we can skip row indices or column indices which indicates we need to return all the rows or all the columns.

sub_array = multi_array [:,1:3] # return all the rows as row indices is omited, 
                                 # and return columns one and two exlude column 3
print (sub_array) #

Output:

[['robotics' 'machine']
 ['43' '45']
 ['56.32' '89.21']
 ['b' 'c']]

This how we can access elements in multidimensional arrays using numpy indexing slicing.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *