InterviewSolution
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:
|
|
| 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» [[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»
|
|
| 9. |
How are NumPy arrays advantageous over python lists? |
Answer»
|
|
| 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: |
|