7 Numpy Interview Questions and Answers

Below are 7 Numpy questions which can be asked in a Python Related job interview.

Q. 1 – What is Numpy?

The numpy is a module which is responsible for effectively storing and processing data at a faster rate as compared to normal array. The advantage of numpy is support of large number of in built mathematical operations as compared to other programming languages. Also, the support to represent n dimensions is also possible with numpy.

Q. 2 – How to Install Numpy?

As numpy is an external Python module, that’s why you need to use pip to install it. Just write python3 -m pip install numpy on terminal/Command line of your PC, this will download as well install numpy.

Q. 3 – How to create Single dimension numpy array?

import numpy as np
list1 = [1, 2.5, 8, 0, 1]
arr1 = np.array(list1)
print(arr1)                  # Prints out [1.  2.5 8.  0.  1. ]

Q. 4 – What attributes are provided by numpy?

  • ndim => As numpy provides n dimensions, we can get how many dimensions currently the array is having with ndim.
  • shape => Indicates number of rows and columns which again can be in different dimensions.
  • dtype => Indicates data type of elements stored in numpy.
import numpy as np
ip = [[1, 2, 3, 4], [5, 6, 7, 8]]
numpy_array = np.array(ip)
print(numpy_array)
print("Number of Dimensions in Numpy array are =>", numpy_array.ndim)
print("Shape of Numpy array is =>", numpy_array.shape)
print("Data Types in Numpy array are =>", numpy_array.dtype)

Output of Above Code

[[1 2 3 4]
 [5 6 7 8]]
Number of Dimensions in Numpy array are => 2
Shape of Numpy array is => (2, 4)
Data Types in Numpy array are => int64

Q. 5 – What utility methods are provided by numpy for creating different elements?

  • np.zeros() => Creates a numpy array only having zeros as elements. For example – np.zeros((3, 3)) creates a three-by-three dimensional numpy array just containing zeros only.
  • np.ones() => Creates a numpy array only having ones as elements. For example – np.ones((4, 4)) creates a four-by-four dimensional numpy array just containing ones only.
  • np.eye() => Creates a numpy array having ones at diagonals and zeros elsewhere. For example – np.eye(4, 5) will creates a four-by-five dimensional numpy array having ones at diagonals, zeros elsewhere.
  • np.arange() => Create a single or n dimension array in which numbers are populated starting from 0 to the number specified as parameter. For example – np.arange(7) will be returns array([0, 1, 2, 3, 4, 5, 6])

Q. 6 – Explain various simple mathematical operations which can be done on numpy?

import numpy as np
numpy_array1 = np.array([[1., 2., 3.],[4., 5., 6.]])
numpy_array2 = np.array([[2., 3., 4.], [4., 5., 6.]])

print("Adding two numpy arrays")
print(numpy_array1 + numpy_array2)
print("\n")

print("Subtracting two numpy arrays")
print(numpy_array1 - numpy_array2)
print("\n")

print("Reversing a numpy array")
print(1 / numpy_array1)
print("\n")

print("Reversing a numpy array")
print(1 / numpy_array2)
print("\n")

print("Taking under root of numpy array")
print(numpy_array1 ** 0.5)

Output of Above Code

Adding two numpy arrays
[[ 3.  5.  7.]
 [ 8. 10. 12.]]


Subtracting two numpy arrays
[[-1. -1. -1.]
 [ 0.  0.  0.]]


Reversing a numpy array
[[1.         0.5        0.33333333]
 [0.25       0.2        0.16666667]]


Reversing a numpy array
[[0.5        0.33333333 0.25      ]
 [0.25       0.2        0.16666667]]


Taking under root of numpy array
[[1.         1.41421356 1.73205081]
 [2.         2.23606798 2.44948974]]

Q. 7 – How to Transposing a Numpy Array?

Numpy array can be transposed by numpy_array.T code statement.

import numpy as np
numpy_array1 = np.array([[1., 2., 3.],[4., 5., 6.]])
print(numpy_array1.T)

Output of Above Code

[[1. 4.]
 [2. 5.]
 [3. 6.]]

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts