NumPy Array Exercise

You will find practice questions related to the NumPy array on this page. These exercises will solve your doubts regarding various ways of creating a NumPy array.

For your better understanding and to solve these questions quickly, it is recommended that you have a knowledge of certain NumPy functions such as zeros(), ones(), linspace(), and others. If you need to learn them visit the following tutorial:

Q1 Using NumPy function, create a NumPy array of 10 eights.

Expected Output

array([ 8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.])

Solution

import numpy as np
arr = np.full(10, 8, dtype=np.float)
print(arr)

Q2 Using NumPy function only, create an array of all even integers from 20 to 30.

Expected Output

array([20, 22, 24, 26, 28, 30])

Solution

import numpy as np
arr = np.linspace(20, 30, 6, dtype=np.int)
print(arr)

If you want to learn more about linspace() function, then visit How to use NumPy linspace() in Python?

Q3 Using NumPy functions only, create a 4x4 matrix with values ranging from 0 to 15.

Expected Output

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

Solution

import numpy as np
arr = np.arange(0, 16).reshape(4, 4)
print(arr)

If you want to learn more about arange() function, then visit How to use NumPy arange() in Python?

Q4 Generate a NumPy array from 0 to 100 (including 100) with a step size of 2. After generating the array, performs the following operations:

  1. Obtain the sum of the array using numpy.sum() function.
  2. Obtain the array in which each element of the above-generated array is squared.
  3. Obtain the sum of the array obtained by (b).
  4. From the array obtained in (b), take only the values that are smaller than 100.

Solution

import numpy as np
arr = np.arange(0, 101, step=2)
print(arr)
print('Sum of the elements: ', np.sum(arr))
squared_arr = arr*arr
print('Squared array:\n', squared_arr)
print('Sum of elements of squared array: ', np.sum(squared_arr))
print(squared_arr[np.where(squared_arr < 100)])

Q5 Write a NumPy program to sum of all the multiples of 3 or 5 below 100.

Solution

import numpy as np
a = np.arange(3, 100, step=3)
print(a)
b = np.arange(5, 100, step=5)
print(b)
arr = np.concatenate((a, b))
print(arr)
np.sum(arr)

Q6 Suppose that a is a 2-dimensional NumPy array and that b is a 1-dimensional NumPy array whose length is equal to the number of columns of a. Write a statement that creates a 2-dimensional NumPy array c that has the same shape as a and whose rows are obtained by multiplying the corresponding row of a with b.

Solution

So, if a is the 3x2 NumPy array [[1, 2], [3, 4], [5,6]] and b is the NumPy array [2, 4], then c will be the 3x2 2-dimensional NumPy array [[2, 8], [6, 16], [10, 24]].

import numpy as np
a = np.array([[1, 2], [3, 4], [5, 6]])
b = np.array([2, 4])
c = a*b

When you multiply a and b, broadcasting takes place, and the resultant array obtained is

[[ 2  8]
 [ 6 16]
 [10 24]]

Q7 Assume that a is a 2-dimensional NumPy array of shape (4,2) and that b is a 2-dimensional NumPy of shape (2,3). Write a statement that creates a 2-dimensional NumPy array c of shape (4,3) that is equal to the product of a and b, considered as matrices.

Solution

So, if a is a 4x2 NumPy array [[1,2],[3,4],[5,6],[7,8]] and b is a 2x3 NumPy array [[0,1,2],[3,4,5]], then c will be the 4x3 NumPy array [[6,9,12],[12,19,26],[18,29,40],[24,39,54]].

import numpy as np
a = np.array([[1,2],[3,4],[5,6],[7,8]])
b = np.array([[0,1,2],[3,4,5]])
c = np.dot(a, b)

When you multiply a and b, matrix multiplication takes place, and the resultant array obtained is

[[ 6  9 12]
 [12 19 26]
 [18 29 40]
 [24 39 54]]

Q8 Assume that a and b are 2-dimensional NumPy arrays of the same shape. Write a statement that creates a 2-dimensional NumPy array c whose rows consist of the rows of a, followed by the rows of b. So c is the concatenation of a and b.

Solution

So, if a is the 3x2 NumPy array [[1, 2], [3, 4], [5, 6]] and b is the 3x2 NumPy array [[10, 9], [8, 7], [6, 5]], then on concatenation c will be the 6x2 2-dimensional NumPy array [[1, 2], [3, 4], [5,6], [10, 9], [8, 7], [6, 5]].

import numpy as np
a = np.array([[1, 2], [3, 4], [5, 6]])
b = np.array([[10, 9], [8, 7], [6, 5]])
c = np.concatenate((a, b))
print(c)
[[ 1  2]
 [ 3  4]
 [ 5  6]
 [10  9]
 [ 8  7]
 [ 6  5]]

Recommended Posts