Problem 1

Import the NumPy library under the alias np.

In [1]:
import numpy as np

Problem 2

Given the following NumPy array, return the first element and the last element.

In [2]:
arr = np.array([8,2,4,9,7,3,8,1])
In [3]:
#Return the first element here
arr[0]
Out[3]:
8
In [4]:
#Returns the last element here
arr[-1]
Out[4]:
1

Problem 3

Given the following NumPy array, return the entire array except for the last element.

In [5]:
new_arr = np.array([1,6,4,8,7,5,2,1,4,5,6,1,4,5,6,2,1])
In [6]:
#Place your solution here
new_arr[:-1]
Out[6]:
array([1, 6, 4, 8, 7, 5, 2, 1, 4, 5, 6, 1, 4, 5, 6, 2])

Problem 4

Change the second element of this_arr to 4.

In [7]:
this_arr = np.array([2,4,89,7,5,4,2,6,4,5,78,1,6,9,7,456,12,45])
In [8]:
#Place your solution here
this_arr[1] = 4

Problem 5

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.

In [9]:
original_array = np.array([12,45,86,79,45,26,13,46,28])
In [10]:
#Solution goes here
reference_array = original_array
reference_array[1] = 16
original_array
Out[10]:
array([12, 16, 86, 79, 45, 26, 13, 46, 28])

Problem 6

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.

In [11]:
first_array = np.array([16,45,75,16,43,45,78,19,23,46,79,58])
In [12]:
#Solution goes here
copy_array = first_array.copy()

copy_array[2] = 42
first_array
Out[12]:
array([16, 45, 75, 16, 43, 45, 78, 19, 23, 46, 79, 58])

Problem 7

Given the two-dimensional array matrix, print the value 35.

In [13]:
matrix = np.array([[15, 120, 115],[230, 35, 130],[335, 420, 425]])
In [14]:
#Solution goes here
matrix[1][1]
Out[14]:
35

Problem 8

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.

In [15]:
integer_array = np.array([2,6,4,9,8,5,3,1,2,5,4,8,7,5,6])
In [16]:
#Solution goes here
integer_array > 4
Out[16]:
array([False,  True, False,  True,  True,  True, False, False, False,
        True, False,  True,  True,  True,  True])

Problem

Given the same array integer_array, print a new NumPy array that only includes the values from integer_array that are less then 6.

In [17]:
integer_array[integer_array < 6]
Out[17]:
array([2, 4, 5, 3, 1, 2, 5, 4, 5])