In this post, we are going to explore 15 Python NumPy code interview questions and answers with examples.
1. How to create a NumPy 1-D array by using a Python list?
We can create a NumPy array using a regular Python list NumPy provides us the array() function that is capable of creating a NumPy array. As an argument to this method, We can pass the Python list to the array function and this will create an array using this list.
Here are some examples of this:
import numpy as np array_one = np.array([1,2,3,4]) print("array one is linear",array_one)
array one is linear [1 2 3 4]
2. How To Create 2-Dimensional NumPy Array Using A List Of Lists?
We can pass the list to the array function and this will create an array using this list.
import numpy as np lists = [[0,1,2], [3,4,5], [6,7,8]] array_one = np.array(lists) print("Two Dimensional Array:",array_one) print("Two Dimensional Array Shape:",array_one.shape)
Two Dimensional Array: [[0 1 2] [3 4 5] [6 7 8]] Two Dimensional Array Shape: (3, 3)
3. How To Create NumPy Array Using Empty() Functions?
Numpy has one more function that can be used to create and initialize an array.The function name is empty().When you create an array you can create it with empty() and populate the desired values at runtime. It just
reserves the places for the number of columns and the number of rows for you.
Let us understand empty() with an example:
import numpy as np array_six = np.empty((3,3)) print("array six using empty",array_six)
array six using empty [[0.00000000e+000 0.00000000e+000 9.76118064e-313] [1.95820216e-306 6.23054972e-307 8.90106955e-307] [1.20161797e-306 1.86920872e-306 1.08225056e-312]]
4. How To Create NumPy Array Using Eye() Functions?
eye() is another function that is available in NumPy library that is used to create a matrix with x*x size and all the elements in the diagonal are 1.
Let us understand this with an example:
import numpy as np array_seven = np.eye(3) print("Eye function output:",array_seven)
Eye function output: [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
5.How To Create NumPy Array Using Ones() Function?
NumPy has ones() function to create arrays with all elements as 1.
import numpy as np array_four = np.ones((4,4)) print("array four using ones",array_four)
array four using ones [[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]]
ones() function can be passed additional arguments as dtype to specify the data type.
array_five = np.ones((4,4),dtype=np.int16)
6. How To Create NumPy Array Using Zeros() Function?
NumPy has zeros() function to create arrays with all elements as 0.
import numpy as np array_three = np.zeros((4,4)) print("array three using zero",array_three)
array three using zero [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]
7. How To Create 2-D Array Using Ones_like() Function?
NumPy library has a function ones_like() which takes another array and creates an array of the same size with all elements as 1.
import numpy as np array_three = [[0 1] [2 3] [4 5] [6 7]] array_four = np.ones_like(array_three) print("2D Array using ones_like",array_four)
2D Array using ones_like [[1 1] [1 1] [1 1] [1 1]]
8. How To Create 2-D Array Using Empty_like() Function?
empty_like() function creates an array with placeholders values. This array is used when we want to just put the placeholder values in an array and later update the element values with exact values.
Here is an example:
import numpy as np array_three = [[0 1] [2 3] [4 5] [6 7]] array_six = np.empty_like(array_three) print("2D Array using empty_like",array_six)
2D Array using empty_like [[ 7864421 7471220] [ 7536737 7340078] [ 121 6553721] [808648704 859255908]]
9. How To Create 2-D Array Using Zeros_like() Function?
NumPy also have zeros_like() function which takes an array and creates the same size array with elements as 0.
Here is an example:
import numpy as np array_three = [[0 1] [2 3] [4 5] [6 7]] array_five = np.zeros_like(array_three) print("2D Array using zeros_like",array_five)
2D Array using zeros_like [[0 0] [0 0] [0 0] [0 0]]
10.How to use NumPy arrange() function to Create an array?
arrange() function can be used to create a 1-D,2-D or N-D array by passing some arguments to it.
import numpy as np array_three = np.arange(8) print("Array using arange",array_three)
Array using arange [0 1 2 3 4 5 6 7]
11.What is reshape() Numpy function?
reshape() function of Numpy is used to change the shape of a NumPy array. When we want to change the shape of an array we can make use of NumPy reshape() function. The main applications are like we have a linear array and we want to convert it to a 2-D or N-D array. Then we can use reshape function to do this. We need to make sure that the elements counts should match with the reshaped array elements counts.
import numpy as np array1 = np.arange(4) array1 = array1.reshape(2,2)
array([[ 0, 1], [2, 3]])
12. How to use arrange() and reshape() function to create a multi-dimension array?
We can make use of arrange() function to create a linear array and then reshape function to change it to a 2-D array.
import numpy as np array_three = np.arange(8).reshape(4,2) print("2D Array using reshape and arange",array_three)
2D Array using reshape and arange [[0 1] [2 3] [4 5] [6 7]]
An important point to note here is that arguments in reshape, should be equal to arrange argument when they are multiplied.
For Example in our example we are passing 8 in arange() so in reshape(), we can pass 4,2 or 2,4 or 2,2,2
13.How to create a NumPy array with step size?
We can eliminate the elements by using the stepsize and create an array of elements that comes in the steps. For example, we have an array of numbers and we can create a new array with the step size of 2.
import numpy as np array1 = np.arange(1,10,2) print(array1) #array_two array2=np.arange(1,20,5) print(array2)
[1, 3, 5, 7, 9] [ 1, 6, 11, 16]
14. How to create a matrix of numbers in NumPy?
We can create a matrix of random numbers by using the randint() function of NumPy.
Here is an example that creates a matrix of size (10,) with random integers between [0,10[
import numpy as np array1 = np.random.randint(10, size=(3,3)) print(array1)
array([[1, 4, 7], [3, 5, 4], [0, 2, 1]])
15.How to create a matrix in NumPy by using identity() function?
The identity matrix is a matrix that has all diagonal elements as 1 and the size is n x n, which means rows and column counts are the same. We can pass only one number n to the identity function that will be assigned to the number of rows and columns and we will get a matrix of size n x n.
import numpy as np matrix1 = np.identity(3) print(matrix1)
array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]])