Numpy Interview Questions: Click Here
Numpy Interview Questions: Click Here
© Copyright by Interviewbit
Contents
19. What happens when the split() method is used for splitting NumPy arrays?
20. What happens when we use the arrays_split() method for splitting the NumPy
array?
21. How will you implement the moving average for the 1D array in NumPy?
22. How is fliplr different from flipud methods in NumPy?
Python lists support storing heterogeneous data types whereas NumPy arrays
can store datatypes of one nature itself. NumPy provides extra functional
capabilities that make operating on its arrays easier which makes NumPy array
advantageous in comparison to Python lists as those functions cannot be
operated on heterogeneous data.
NumPy arrays are treated as objects which results in minimal memory usage.
Since Python keeps track of objects by creating or deleting them based on the
requirements, NumPy objects are also treated the same way. This results in
lesser memory wastage.
NumPy arrays support multi-dimensional arrays.
NumPy provides various powerful and efficient functions for complex
computations on the arrays.
NumPy also provides various range of functions for BitWise Operations, String
Operations, Linear Algebraic operations, Arithmetic operations etc. These are
not provided on Python’s default lists.
import numpy as np
Two-Dimensional array
import numpy as np
arr = [[1,2,3,4],[4,5,6,7]]
numpy_arr = np.array(arr)
Three-Dimensional array
import numpy as np
arr = [[[1,2,3,4],[4,5,6,7],[7,8,9,10]]]
numpy_arr = np.array(arr)
Using the np.array() function, we can create NumPy arrays of any dimensions.
6. How do you find the data type of the elements stored in the
NumPy arrays?
NumPy supports the following datatypes:
i - integer
S - string
b - boolean
f - float
u - unsigned integer
c - complex float
m - timedelta
M - datetime
O - object
U - unicode string
V - fixed memory chunk for types such as void
We can make use of the dtype property that returns the type of the elements
stored in the NumPy array. Let us consider the below code snippet. We create
some sample arrays and we see what the data types of these arrays are.
import numpy as np
print(arr1.dtype)
print(arr2.dtype)
print(arr3.dtype)
int64
<U12
|S1
import numpy as np
# To reverse array
reverse_arr = arr[::-1]
print(reverse_arr)
Output:
[6 4 2 1]
import numpy as np
Output:
[6 5 4 2 1]
import numpy as np
arr = np.array([1, 2, 1, 3, 5, 0, 0, 0, 2, 3])
result = np.bincount(arr)
print(result)
[3 2 2 2 0 1]
It has to be noted here that each element represents the count of the corresponding
index value present in the original array. This is demonstrated in the below image:
We can check for the emptiness of a NumPy array by making use of the size attribute.
Let us consider the below example. We have NumPy array arr filled with zeros. If the
size element returns zero, that means the array is empty or it only consists of zeros.
import numpy as np
arr = np.zeros((1,0)) #returns empty array
print(arr.size) #returns 0
This return 0
import numpy as np
arr = np.array([[1,2,3,4],[5,6,7,8]])
new_arr =arr[:,0]
print(new_arr)
Output:
[1 5]
arr[:,[0]] - This returns the elements of the first column by adding extra
dimension to it.
import numpy as np
arr = np.array([[1,2,3,4],[5,6,7,8]])
new_arr =arr[:,[0]]
print(new_arr)
Output:
[[1]
[5]]
import numpy as np
# NumPy matrices
A = np.arange(15,24).reshape(3,3)
B = np.arange(20,29).reshape(3,3)
print("A: ",A)
print("B: ",B)
# Multiply A and B
result = A.dot(B)
print("Result: ", result)
Output
A: [[15 16 17]
[18 19 20]
[21 22 23]]
B: [[20 21 22]
[23 24 25]
[26 27 28]]
Result: [[1110 1158 1206]
[1317 1374 1431]
[1524 1590 1656]]
where,
a1,a2: arrays of the same shape
axis: Represents the axis along which the arrays are joined. The default value is 0.
out: If mentioned, it specifies the destination for placing the result.
For example:
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
With axis 0:
[[1 2]
[3 4]
[5 6]]
With axis 1:
[[1 2 5]
[3 4 6]]
Notice how the arrays are concatenated with different values of the axis.
import pandas as pd
import numpy as np
# Pandas DataFrame
df = pd.DataFrame(data={'A': [3, 2, 1], 'B': [6,5,4], 'C': [9, 8, 7]},
index=['i', 'j', 'k'])
print("Pandas DataFrame: ")
print(df)
Pandas DataFrame:
A B C
i 3 6 9
j 2 5 8
k 1 4 7
Pandas DataFrame to NumPy array:
[[3 6 9]
[2 5 8]
[1 4 7]]
Convert B and C columns of Pandas DataFrame to NumPy Array:
[[6 9]
[5 8]
[4 7]]
Function Vectorization technically means that the function is applied to all elements
in the array. Typically, certain python functionalities on arrays (such as loops) are
slower in nature because python arrays can contain elements of different data types.
Since the C program expects a specific datatype, there are chances of compiler
optimisation which makes C code run faster. Since NumPy arrays support storing
elements of a single datatype, most of the implementations of the functions written
in NumPy meant for arithmetic, logical operations etc have optimised C program
code under their hood. Additionally, NumPy also helps developers create their own
vectorised functions by following the below steps:
Write your required function that takes array elements as parameters.
Vectorize the function by making use of the vectorize() method of the NumPy
package.
Give array inputs to the vectorized function.
The below example demonstrates the process of vectorization.
import numpy as np
# Define your function
def add(arr1, arr2):
return (arr1 + arr2)
arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
print(result)
[5 7 9]
Both methods are used for combining the NumPy arrays. The main difference is that
the hstack method combines arrays horizontally whereas the vstack method
combines arrays vertically.
For example, consider the below code.
import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])
# vstack arrays
c = np.vstack((a,b))
print("After vstack: \n",c)
# hstack arrays
d = np.hstack((a,b))
print("After hstack: \n",d)
After vstack:
[[1 2 3]
[4 5 6]]
After hstack:
[1 2 3 4 5 6]
Notice how a er the vstack method, the arrays were combined vertically along the
column and how a er the hstack method, the arrays were combined horizontally
along the row.
17. How do you find the local peaks (or maxima) in a 1-D NumPy
Array?
Peaks are the points that are surrounded by smaller value points on either side as
shown in the image below:
import numpy as np
# define NumPy array
arr = np.array([1, 4, 8, 1, 3, 5, 1, 6, 1, -5, -1, 19, 2])
Output:
[ 2 5 7 11]]
The +1 at the end of the expression is required as it finds the indexes within
the slice arr[1:-1] and not the entire array arr.
The where() method returns a tuple of arrays where the first element is our
required array. Hence we add [0] a er the where method.
Using combination of .diff(), .sign() and .where() method:
In this method, we calculate the difference between each element using the
diff() method of NumPy.
Then we use the sign() method on the array to get the sign of difference.
The value can be either -1 or +1. This result is then passed on to another diff()
method which returns 0, -2 or +2 value. The value 0 indicates that the points are
continuously increasing or decreasing, +2 indicates minimum peak and -2
indicates maximum peak (local maxima).
We then identify the position or indexes of the local maxima using the where()
method. The reason for using +1 at the end of where and [0] a er where is the
same as the explanation described in Method 1 for finding local maxima.
The following code example demonstrates this:
import numpy as np
# define NumPy array
arr = np.array([1, 4, 8, 1, 3, 5, 1, 6, 1, -5, -1, 19, 2])
all_peaks = np.diff(np.sign(np.diff(arr)))
maxima_peaks_positions = np.where(all_peaks == -2)[0] + 1
print(maxima_peaks_positions)
Output:
[ 2 5 7 11]]
where,
array - array that needs to be split
sections -
If we give an integer X, X equal sub-arrays are obtained a er dividing the
array. If the split is not possible, ValueError is raised.
For example:
import numpy as np
a = np.arange(8)
split_arr = np.split(a, 2)
split_arr
Output
If we give a 1-D sorted array then the entries would represent where the array
would be split along the axis. For instance if we provide [2:3] and axis as 0, then
the result would be
[arr[0:2], arr[2:3], arr[3:]]
If the provided index exceeds the array dimension along the given axis, then
an empty subarray will be returned.
For example:
import numpy as np
a = np.arange(6.0)
split_arr = np.split(a, [3, 4, 5, 6, 7,8])
split_arr
axis - Along what axis the array has to be split. By default, the value is 0
In the above figure, we see there are 5 elements in the array, we want to split the
array to 3 subarrays. So L % N = 5%3 = 2 subarrays of size (L//N +1) = (5//3 +1) = 2 are
returned and remaining 1 subarray of size L//N = 1 is returned.
Syntax:
where,
array - Given Input array.
sections - List of indices or Number of subarrays to be returned.
axis - Axis along which values have to be appended.
The code for the example illustrated above is:
import numpy as np
arr = np.arange(5.0)
split_arrs = np.array_split(arr, 3)
split_arrs
Output:
import numpy as np
def calculate_moving_average(arr, w):
return np.convolve(arr, np.ones(w),'valid')/w
The above-defined function can be then used for finding the moving average as
shown in the examples below:
arr1 = np.array([4,5,8,9,3,2,4,2,0,2])
print("Moving average of window length 2: ")
av1 = calculate_moving_average(arr1, 2)
print(av1)
Output:
We see that the positions of the elements are flipped le or right than their original
position in the result.
Syntax of fliplr:
np.fliplr(arr)
Here, we see that the numbers 1, 3 and 5 elements are flipped in the up/down
direction in the result.
The syntax for flipud:
np.flipud(arr)
import numpy as np
upper_case_arr = np.char.upper(arr)
lower_case_arr = np.char.lower(arr)
capitalize_case_arr = np.char.capitalize(arr)
titlecase_arr = np.char.title(arr)
swapcase_arr = np.char.swapcase(arr)
Output:
import numpy as np
zeroes_filled_arr = np.char.zfill(arr, 8)
print("Transformed array: ")
print(zeroes_filled_arr)
Output:
Transformed array:
['00000022' '00000009' '00001234' '00000567' '00089102']
import numpy as np
Output:
Transformed Array:
['i' 'l o v e' 'N u m P y' 'A N D' 'i n t e r v i e w b i t']
import numpy as np
# Create Sample NumPy Array
arr = np.array(['i', 'love', 'NumPy', 'AND', 'interviewbit'], dtype=str)
transformed_array = np.char.multiply(arr, 5)
print("Transformed array:")
print(transformed_array)
Output:
Transformed array:
['iiiii' 'lovelovelovelovelove' 'NumPyNumPyNumPyNumPyNumPy'
'ANDANDANDANDAND'
'interviewbitinterviewbitinterviewbitinterviewbitinterviewbit']
import numpy as np
arr = np.arange(10, 60)
print(arr)
Output:
[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
58 59]
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]
The resultant array should be: (zeros on the border and 1s within it)
[[ 0. 0. 0. 0. 0. 0.]
[ 0. 1. 1. 1. 1. 0.]
[ 0. 1. 1. 1. 1. 0.]
[ 0. 1. 1. 1. 1. 0.]
[ 0. 0. 0. 0. 0. 0.]]
Solution:-
This can be achieved by using the pad method of the NumPy library.
import numpy as np
print("Transformed array:")
transformed_array = np.pad(ones_arr, pad_width=1, mode='constant', constant_values=0)
print(transformed_array)
Output:
Transformed array:
[[0. 0. 0. 0. 0. 0.]
[0. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 0.]
[0. 0. 0. 0. 0. 0.]]
import numpy as np
Output:
In this approach, care has to be taken w.r.t the number of elements present in the
original array before changing the dimensions. Otherwise, it will result in the
ValueError as shown below:
import numpy as np
1 import numpy as np
2 arr = np.array([1,2,3,4,5,6,7,8])
----> 3 arr.shape = (3, 3)
4 print(arr)
import numpy as np
arr = np.array([[1,2,3]])
print("Original array: ")
print(arr)
#Swap axes
axis_swapped_arr = np.swapaxes(arr,0,1)
print("Transformed array: ")
print(axis_swapped_arr)
Output:
Original array:
[[1 2 3]]
Transformed array:
[[1]
[2]
[3]]
Conclusion
The popularity of the NumPy package has grown immensely among the data science
and python developers community ever since it was first introduced in 2005 due to
the wide range of high-performing functionalities it offers. This is why it becomes
essential to learn and be prepared for the interview questions about this package. In
this article, we have seen the most commonly asked NumPy interview questions for
freshers and experienced people, along with some questions on writing python
programs which make use of NumPy functions.
References
https://numpy.org/
https://www.w3resource.com/numpy/index.php
Interview Guides
https://www.interviewbit.com/technical-interview-questions/
https://www.interviewbit.com/coding-interview-questions/
https://www.interviewbit.com/mock-interview/
https://www.interviewbit.com/blog/
Css Interview Questions Laravel Interview Questions Asp Net Interview Questions