Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

How will you find the shape of any given NumPy array?

Answer»

We can use the shape attribute of the numpy array to find the shape. It returns the shape of the array in terms of row COUNT and column count of the array.

import numpy as nparr_two_dim = np.array([("x1","X2", "x3","x4"), ("x5","x6", "x7","X8" )])arr_one_dim = np.array([3,2,4,5,6])# find and PRINT shapeprint("2-D Array Shape: ", arr_two_dim.shape)print("1-D Array Shape: ", arr_one_dim.shape)"""Output:2-D Array Shape: (2, 4)1-D Array Shape: (5,)"""
2.

How will you reverse the numpy array using one line of code?

Answer»

This can be done as shown in the following:

reversed_array = arr[::-1]

where arr = ORIGINAL given array, reverse_array is the RESULTANT after reversing all ELEMENTS in the INPUT.

3.

How will you find the nearest value in a given numpy array?

Answer»

We can use the argmin() METHOD of NUMPY as SHOWN below:

import numpy as npdef find_nearest_value(arr, value): arr = np.asarray(arr) idx = (np.abs(arr - value)).argmin() return arr[idx]#DRIVER codearr = np.array([ 0.21169, 0.61391, 0.6341, 0.0131, 0.16541, 0.5645, 0.5742])value = 0.52print(find_nearest_value(arr, value)) # PRINTS 0.5645
4.

How will you sort the array based on the Nth column?

Answer»

For example, consider an array arr.

arr = np.array([[8, 3, 2], [3, 6, 5], [6, 1, 4]])

Let us try to sort the rows by the 2nd column so that we get:

[[6, 1, 4],[8, 3, 2],[3, 6, 5]]

We can do this by using the sort() METHOD in numpy as:

import numpy as nparr = np.array([[8, 3, 2], [3, 6, 5], [6, 1, 4]])#sort the array using np.sortarr = np.sort(arr.view('I8,i8,i8'), ORDER=['f1'], axis=0).view(np.int)

We can also perform sorting and that too inplace sorting by doing:

arr.view('i8,i8,i8').sort(order=['f1'], axis=0)
5.

How will you read CSV data into an array in NumPy?

Answer»

This can be ACHIEVED by USING the genfromtxt() method by SETTING the delimiter as a comma.

from NUMPY import genfromtxtcsv_data = genfromtxt('sample_file.csv', delimiter=',')
6.

How will you efficiently load data from a text file?

Answer»

We can use the METHOD NUMPY.loadtxt() which can automatically read the file’s header and footer lines and the comments if any.

This method is highly efficient and even if this method feels less efficient, then the data should be REPRESENTED in a more efficient format such as CSV etc. Various alternatives can be considered depending on the version of NumPy used.

Following are the file FORMATS that are supported:

  • Text files: These files are generally very slow, huge but portable and are human-readable.
  • Raw binary: This file does not have any metadata and is not portable. But they are fast.
  • Pickle: These are borderline slow and portable but depends on the NumPy versions.
  • HDF5: This is known as the High-Powered Kitchen Sink format which SUPPORTS both PyTables and h5py format.
  • .npy: This is NumPy's native binary data format which is extremely simple, efficient and portable.
7.

You are given a numpy array and a new column as inputs. How will you delete the second column and replace the column with a new column value?

Answer»

EXAMPLE:
GIVEN array:

[[35 53 63][72 12 22][43 84 56]]

New Column values:

[ 20  30  40]

Solution:

IMPORT numpy as np#inputsinputArray = np.array([[35,53,63],[72,12,22],[43,84,56]])new_col = np.array([[20,30,40]])# DELETE 2nd columnarr = np.delete(inputArray , 1, axis = 1)#insert new_col to arrayarr = np.insert(arr , 1, new_col, axis = 1)print (arr)
8.

What are the steps to create 1D, 2D and 3D arrays?

Answer»
  • 1D array creation:
IMPORT numpy as npone_dimensional_list = [1,2,4]one_dimensional_arr = np.array(one_dimensional_list)print("1D array is : ",one_dimensional_arr)
  • 2D array creation:
import numpy as nptwo_dimensional_list=[[1,2,3],[4,5,6]]two_dimensional_arr = np.array(two_dimensional_list)print("2D array is : ",two_dimensional_arr)
  • 3D array creation:
import numpy as npthree_dimensional_list=[[[1,2,3],[4,5,6],[7,8,9]]]three_dimensional_arr = np.array(three_dimensional_list)print("3D array is : ",three_dimensional_arr)
  • ND array creation: This can be achieved by giving the ndmin attribute. The below example demonstrates the creation of a 6D array:
import numpy as npndArray = np.array([1, 2, 3, 4], ndmin=6)print(NDARRAY)print('Dimensions of array:', ndArray.ndim)
9.

How are NumPy arrays advantageous over python lists?

Answer»
  • The list data structure of python is very highly EFFICIENT and is capable of performing various functions. But, they have severe limitations when it comes to the computation of vectorized operations which deals with ELEMENT-wise multiplication and addition. The python lists also require the information regarding the TYPE of every element which results in overhead as type dispatching code gets executes every time any operation is performed on any element. This is where the NumPy arrays COME into the picture as all the limitations of python lists are handled in NumPy arrays.
  • Additionally, as the size of the NumPy arrays increases, NumPy becomes AROUND 30x times faster than the Python List. This is because the Numpy arrays are densely packed in the memory due to their homogenous nature. This ensures the memory free up is also faster.
10.

What do you understand by NumPy?

Answer»

NumPy is one of the most popular, easy-to-use, VERSATILE, open-source, python-based, general-purpose package that is used for processing ARRAYS. NumPy is short for NUMerical PYthon. This is very famous for its highly optimized tools that result in high performance and POWERFUL N-Dimensional array processing feature that is designed explicitly to work on complex arrays. Due to its popularity and powerful performance and its FLEXIBILITY to perform various operations like trigonometric operations, algebraic and statistical computations, it is most commonly used in performing scientific computations and various broadcasting functions. The following image shows the applications of NumPy: