Machine Learning Prerequisites (Numpy)

18 / 32

Numpy - Arrays - Indexing and Array Slicing


Index of a NumPy array starts with '0', as we have in the case of a normal Python array.

import numpy as np
arr = np.array([1, 10, 31, 2, 18, 9, 22])

print(arr[1])

The output will be

10

If you want to extract a portion (a few elements) of this NumPy array, then you can use below syntax

part_arr = arr[2 : 5]

If you print the values of part_arr array, the output will be

[31, 2, 18]

That is, the 5th value is not returned, only values at index 2, 3 and 4 will be returned.

If you want to extract a portion of the array, but by a gap of say 2 elements, then you can do so by using the below syntax

skip_arr = arr[1: :2]

If you print the values of skip_arr array, the output will be

[10, 2, 9]
INSTRUCTIONS

Please follow the below instructions to try the below problems using array index slicing technique:

Please import numpy as np


Reversing of an array elements



(1) Please define a NumPy array with name my_orig_arr having following elements (1, 15, 3, 9, 26, 7, 89, 12)

my_orig_arr = np.array(<<your code comes here>>)

(2) Please reverse the elements of the above array by using the below syntax and store the result in a variable called my_rev_arr

<< your code comes here>> = my_orig_arr[ : : -1]

(3) Now print this array my_rev_arr that you got above to see its values

print(<<your code comes here>>)


Replacing selected elements of a NumPy array by a specific value



  • Create a new array my_orig_arr2 having following elements (2, 9, 17, 13, 1, 4, 20, 57).

    my_orig_arr2 = np.array(<<your code comes here>>)
    
  • Say, you want to replace a few elements (say from index 2 to 5) of the above NumPy array my_orig_arr2 by a value -6, you can do so by following the below steps:

    my_orig_arr2[2 : 5] = -6
    
  • Please print this NumPy array my_orig_arr2 to see its values (please note that only values at indexes 2, 3, 4 will be replaced, not the 5th index value)

    print(<<your code comes here>>)
    

Please remember that this is only possible in a NumPy array, if you try to do this with a normal Python array, you will get an error.

e.g.

(1) Please create a normal Python array

arr = [1, 3, 5, 10, 4, 15]

arr is a normal Python array here.

(2) Please try to replace some of its values with a value say -3, you will get an error

arr[1 : 4]  = -3


Extracting a slice(portion) of a NumPy array


To extract a slice (portion) of a NumPy array, please follow the below steps:

(1) Define a NumPy array with name apple with values (1, 8, 23, 3, 18, 91, 7, 15)

apple = np.array(<<your code comes here>>)

(2) Extract a portion of this NumPy array apple from index 1 to 4 (by using the below syntax) and store it in a variable apple_slice

<<your code comes here>> = apple[1 : 4]

(3) Print array apple_slice using the print() function to see its values

print(<<your code comes here>>)

(4) Print array apple using the print() function to see its values are intact

print(<<your code comes here>>)

If you change (replace) value in this slice (apple_slice array), the corresponding value will also get modified in its parent array apple.

Please follow the below steps to check this for yourself.

(1) Replace 2nd element of array apple_slice by a value say 99999

apple_slice[1] = 99999

please remember 2nd element will have an index of '1'

(3) Print array apple_slice using the print() function to see its values, 2nd element should be 99999 now

print(<<your code comes here>>)

(4) Print array apple using the print() function to see its values, the corresponding original value in the parent array apple should also have been changed to 99999

print(<<your code comes here>>)

Original (parent) array from which the slice was extracted also gets modified if the slice value is modified.

If you don't want original (parent) array values to get modified when its slice values are changing, you can create the slice by using a copy() function in the slicing code syntax

  • Suppose you don't want your parent (original) array values to get modified when its slice values are changing. You can achieve this by using the copy() function in the previous code syntax to create a slice.

Please follow the below steps:

(1) Please create a new slice from the apple array by using copy() function in the syntax, as shown below:

apple_slice_new = apple[2 : 5].copy()

(2) Replace 2nd element of array apple_slice_new by a value say 222222

apple_slice_new[1] = 222222

please remember 2nd element will have an index of '1'

(3) Print slice array apple_slice_new using the print() function to see its values, 2nd element should be 222222 now

print(<<your code comes here>>)

(4) Print parent array apple using the print() function to see its values, the parent array apple should be intact (no values modified).

print(<<your code comes here>>)

Original (parent) array from which the slice was extracted using the copy() function doesn't get modified if the slice value is modified.

See Answer

No hints are availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...