dzone python training institute

Python NumPy Exercise

Question 1: How to make a python function that handles scalars to work on numpy arrays?

Note: Convert the function maxx that works on two scalars, to work on two arrays.

	
	Input:

def maxx(x, y):
    """Get the maximum of two items"""
    if x >= y:
        return x
    else:
        return y

maxx(1, 5)
#> 5

Desired Output:

a = np.array([5, 7, 9, 8, 6, 4, 5])
b = np.array([6, 3, 4, 8, 9, 7, 1])
pair_max(a, b)
#> array([ 6.,  7.,  9.,  8.,  9.,  7.,  5.])
   
   
    
        

Solution

def maxx(x, y): """Get the maximum of two items""" if x >= y: return x else: return y pair_max = np.vectorize(maxx, otypes=[float]) a = np.array([5, 7, 9, 8, 6, 4, 5]) b = np.array([6, 3, 4, 8, 9, 7, 1]) pair_max(a, b) #> array([ 6., 7., 9., 8., 9., 7., 5.])

Question 2: How to swap two columns in a 2d numpy array?

Note: Swap columns 1 and 2 in the array arr.


arr = np.arange(9).reshape(3,3)
arr


Solution

# Input arr = np.arange(9).reshape(3,3) arr # Output arr[:, [1,0,2]] #> array([[1, 0, 2], #> [4, 3, 5], #> [7, 6, 8]])

Question 3: How to swap two rows in a 2d numpy array?

Note: Swap rows 1 and 2 in the array arr:


arr = np.arange(9).reshape(3,3)
arr


Solution

# Input arr = np.arange(9).reshape(3,3) # Output arr[[1,0,2], :] #> array([[3, 4, 5], #> [0, 1, 2], #> [6, 7, 8]])

Question 4: How to reverse the rows of a 2D array?

Note: Reverse the rows of a 2D array arr.


# Input
arr = np.arange(9).reshape(3,3)

    
        

Solution

# Input arr = np.arange(9).reshape(3,3) # Solution arr[::-1] £ Output array([[6, 7, 8], [3, 4, 5], [0, 1, 2]])

Question 5: How to reverse the columns of a 2D array?

Note: Reverse the columns of a 2D array arr.


# Input
arr = np.arange(9).reshape(3,3)


Solution

# Input arr = np.arange(9).reshape(3,3) # Solution arr[:, ::-1] #> array([[2, 1, 0], #> [5, 4, 3], #> [8, 7, 6]])

Question 6: How to create a 2D array containing random floats between 5 and 10?

Note: Create a 2D array of shape 5x3 to contain random decimal numbers between 5 and 10.

    
        

Solution

# Input arr = np.arange(9).reshape(3,3) # Solution Method 1: rand_arr = np.random.randint(low=5, high=10, size=(5,3)) + np.random.random((5,3)) # print(rand_arr) # Solution Method 2: rand_arr = np.random.uniform(5,10, size=(5,3)) print(rand_arr) #> [[ 8.50061025 9.10531502 6.85867783] #> [ 9.76262069 9.87717411 7.13466701] #> [ 7.48966403 8.33409158 6.16808631] #> [ 7.75010551 9.94535696 5.27373226] #> [ 8.0850361 5.56165518 7.31244004]]

Question 7: How to print only 3 decimal places in python numpy array?

Print or show only 3 decimal places of the numpy array rand_arr.


Input:

rand_arr = np.random.random((5,3))


Solution

# Input rand_arr = np.random.random((5,3)) # Create the random array rand_arr = np.random.random([5,3]) # Limit to 3 decimal places np.set_printoptions(precision=3) rand_arr[:4] #> array([[ 0.443, 0.109, 0.97 ], #> [ 0.388, 0.447, 0.191], #> [ 0.891, 0.474, 0.212], #> [ 0.609, 0.518, 0.403]])

Question 8: How to pretty print a numpy array by suppressing the scientific notation (like 1e10)?

Note: Pretty print rand_arr by suppressing the scientific notation (like 1e10)


Input:

# Create the random array
np.random.seed(100)
rand_arr = np.random.random([3,3])/1e3
rand_arr

#> array([[  5.434049e-04,   2.783694e-04,   4.245176e-04],
#>        [  8.447761e-04,   4.718856e-06,   1.215691e-04],
#>        [  6.707491e-04,   8.258528e-04,   1.367066e-04]])
Desired Output:

#> array([[ 0.000543,  0.000278,  0.000425],
#>        [ 0.000845,  0.000005,  0.000122],
#>        [ 0.000671,  0.000826,  0.000137]])


Solution

# Reset printoptions to default np.set_printoptions(suppress=False) # Create the random array np.random.seed(100) rand_arr = np.random.random([3,3])/1e3 rand_arr #> array([[ 5.434049e-04, 2.783694e-04, 4.245176e-04], #> [ 8.447761e-04, 4.718856e-06, 1.215691e-04], #> [ 6.707491e-04, 8.258528e-04, 1.367066e-04]]) Output np.set_printoptions(suppress=True, precision=6) # precision is optional rand_arr #> array([[ 0.000543, 0.000278, 0.000425], #> [ 0.000845, 0.000005, 0.000122], #> [ 0.000671, 0.000826, 0.000137]])

Question 9: How to limit the number of items printed in output of numpy array?

Note: Limit the number of items printed in python numpy array a to a maximum of 6 elements.


Input:

a = np.arange(15)
#> array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])

Desired Output:

#> array([ 0,  1,  2, ..., 12, 13, 14])


Solution

np.set_printoptions(threshold=6) a = np.arange(15) a #> array([ 0, 1, 2, ..., 12, 13, 14])

Question 10: How to print the full numpy array without truncating?

Note: Print the full numpy array a without truncating.


Input:

np.set_printoptions(threshold=6)
a = np.arange(15)
a
#> array([ 0,  1,  2, ..., 12, 13, 14])

Desired Output:

a
#> array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])


Solution

# Input np.set_printoptions(threshold=6) a = np.arange(15) # Solution np.set_printoptions(threshold=np.nan) a #> array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])