Import the NumPy library under the alias np
.
import numpy as np
Given the following NumPy array, return the first element and the last element.
arr = np.array([8,2,4,9,7,3,8,1])
#Return the first element here
arr[0]
8
#Returns the last element here
arr[-1]
1
Given the following NumPy array, return the entire array except for the last element.
new_arr = np.array([1,6,4,8,7,5,2,1,4,5,6,1,4,5,6,2,1])
#Place your solution here
new_arr[:-1]
array([1, 6, 4, 8, 7, 5, 2, 1, 4, 5, 6, 1, 4, 5, 6, 2])
Change the second element of this_arr
to 4
.
this_arr = np.array([2,4,89,7,5,4,2,6,4,5,78,1,6,9,7,456,12,45])
#Place your solution here
this_arr[1] = 4
Given the array original_array
, create a reference to that array called reference_array
. Change the second element of reference_array
to 16
and then print original_array
to verify that it modified the original data structure.
original_array = np.array([12,45,86,79,45,26,13,46,28])
#Solution goes here
reference_array = original_array
reference_array[1] = 16
original_array
array([12, 16, 86, 79, 45, 26, 13, 46, 28])
Given the array first_array
, create a copy of the array called copy_array
. Change the third element of copy_array
to 42
and then print first_array
to verify that its elements remain unchanged.
first_array = np.array([16,45,75,16,43,45,78,19,23,46,79,58])
#Solution goes here
copy_array = first_array.copy()
copy_array[2] = 42
first_array
array([16, 45, 75, 16, 43, 45, 78, 19, 23, 46, 79, 58])
Given the two-dimensional array matrix
, print the value 35
.
matrix = np.array([[15, 120, 115],[230, 35, 130],[335, 420, 425]])
#Solution goes here
matrix[1][1]
35
Given the array integer_array
, print a NumPy array of Boolean values that indicate whether the corresponding values in integer_array
are greater than 4.
integer_array = np.array([2,6,4,9,8,5,3,1,2,5,4,8,7,5,6])
#Solution goes here
integer_array > 4
array([False, True, False, True, True, True, False, False, False, True, False, True, True, True, True])
Given the same array integer_array
, print a new NumPy array that only includes the values from integer_array
that are less then 6
.
integer_array[integer_array < 6]
array([2, 4, 5, 3, 1, 2, 5, 4, 5])