NumPy is one of those core Python libraries which are highly powerful and can save a lot of time for you because it handles all your numerical calculations for array-based data. Its efficient array processing capabilities make it indispensable for tasks involving large datasets or mathematical operations.
This library finds its application in everything starting from data science and machine learning, and ending with scientific modelling. Its use is very widespread in the field due to its performance, flexibility, and great ease of use with other Python libraries. Understanding NumPy provides a good background for handling a lot of advanced computing tasks.
In this blog, we will discuss more than 70 requisite NumPy interview questions. These are sectionalised for fresh graduates, intermediate level and experts. You will get more familiar with Numpy as you go through these different questions and answers.
NumPy Interview Questions and Answers for Freshers
1. How do you install NumPy?
You can install NumPy using pip, the Python package manager. Open a terminal or
command prompt and run:
pip install numpy
For specific versions:
pip install numpy==1.21.0
To verify the installation:
import numpy as np
print(np.__version__)
2. How do I create a NumPy array?
You can create a NumPy array using the np.array() function.
Efficient Data Handling: When you want a perfect data handling tool, NumPy offers you whole array manipulations, which are not just fast but also optimised for memory usage.
Integration:Libraries like Pandas, SciPy and Matplotlib are well-integrated for perfect use.
Operations in Maths: This also covers linear algebra functions, Fourier transform functions, and functions to generate random numbers.
4. Can you distinguish between single precision and double precision when you are using a NumPy array?
Aspect
Single Precision (float32)
Double Precision (float64)
Bit Size
32 bits
64 bits
Precision
Approximately 7 decimal digits
Approximately 16 decimal digits
Memory Usage
Uses less memory per element (4 bytes)
Uses more memory per element (8 bytes)
Performance
Faster computations due to smaller size
Slower compared to single-precision
Use Cases
Graphics, machine learning models where speed is critical
Scientific computations requiring high precision
Range
±3.4e−38 to ±3.4e+38
±1.7e−308 to ±1.7e+308
5. You have to find the dot product of two arrays. Find it with the help of NumPy.
Use the np.dot() function.
Example:
import numpy as np
a = np.array([10, 12, 14])
b = np.array([3, 2, 1])
dot_product = np.dot(a, b)
print(dot_product) # Output: 68
23. How can you create an array with the same values?
Use np.full():
arr = np.full((3, 3), 7)
print(arr)
# Output:
# [[7 7 7]
# [7 7 7]
# [7 7 7]]
Use np.ones() or np.zeros():
ones = np.ones((2, 2))
zeros = np.zeros((2, 2))
24. Your task is to compute the Fourier transformation in a signal. Write the procedure for the same using NumPy.
You can calculate a signal’s Fourier transform with NumPy’s np.fft.fft() function. This takes in the time domain of a signal into the frequency domain. Thus enabling you to look at exactly what frequencies exist. That is:
import numpy as np
signal = np.array([0, 1, 0, -1])
fft_result = np.fft.fft(signal)
print(fft_result)
25. How do you remove missing or null values from a NumPy array?
To remove missing or null values from a NumPy array, you can use boolean indexing or filtering. Although NumPy does not natively support NaN handling like Pandas, you can still identify NaN values using np.isnan().
This function is particularly useful in linear algebra, where diagonal elements play a key role in operations like eigenvalue decomposition.
29. What is negative indexing in NumPy arrays?
Negative indexing is very useful when you don’t know the exact size of the array. All you have to do is assign an index to -1, which ultimately means the last index in the array, then -2 to the second last index in the array, and so on.
30. What is np.sum() in NumPy and np.sum() in Python?
Aspect
np.sum()
sum()
Library
NumPy
Built-in Python
Input Types
NumPy arrays, lists, tuples
Iterables like lists, tuples, etc.
Performance
Optimised for large numerical data
Slower for large datasets
Axis Parameter
Supports multi-dimensional arrays with axis argument
Only works with 1D iterables
Data Types
Handles various numerical data types efficiently
Primarily for integers and floats
Return Type
Returns a NumPy scalar or array
Returns a Python scalar
Broadcasting
Supports broadcasting for element-wise summation
Does not support broadcasting
31. Can you create a plot in NumPy?
NumPy itself does not have plotting functions. However, you can use NumPy in conjunction with Matplotlib, in order to create various different types of plots. For example, you can create data with the help of NumPy and then use Matplotlib to make interesting plots.
Example:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title("Sine Wave")
plt.show()
Here, NumPy generates the data, while Matplotlib handles the plotting. This combination is widely used in data visualisation tasks.
45. How do we check for an empty array (or zero elements array)?
To determine if a NumPy array is empty, use the size attribute.
Method: Using size Attribute
import numpy as np
empty_array = np.array([])
non_empty_array = np.array([1, 2, 3])
if empty_array.size == 0:
print("The array is empty.")
else:
print("The array is not empty.")
if non_empty_array.size == 0:
print("The array is empty.")
else:
print("The array is not empty.")
Output:
The array is empty.
The array is not empty.
Alternative Method: Using len()
if len(empty_array) == 0:
print(“The array is empty.”)
Note:
size is reliable for multidimensional arrays.
An array with shape (0, 3) is considered empty.
46. How can you find the dimensions and shape of a NumPy array?
NumPy provides attributes to determine an array’s structure.
Key Attributes:
ndim: Number of dimensions.
shape: Tuple indicating the size in each dimension.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# Element-wise addition
c = np.add(a, b)
print(c) # Output: [5 7 9]
# Trigonometric function
angles = np.array([0, np.pi/2, np.pi])
sine = np.sin(angles)
print(sine)
# Output: [0.0, 1.0, 0.0]
NumPy Interview Questions and Answers for Experience Professionals
56. Is SIMD used by NumPy?
Yes, NumPy implements SIMD (single instruction, multiple data) through custom low-level libraries such as BLAS and LAPACK. These libraries allow NumPy to perform the same operations on multiple data points simultaneously, specially optimised for calculating large numbers. By taking advantage of SIMD instructions, NumPy can handle vector addition. Even efficiently multiplies matrices and more. This parallel processing capability greatly accelerates array operations, making NumPy a powerful tool for scientific computing and data analysis. Modern CPUs with advanced SIMD features also help increase NumPy performance. This ensures that complex mathematical operations are carried out quickly.
57. How is vstack() different from hstack() in NumPy?
Function
Description
Example
vstack()
Stacks arrays vertically (row-wise)
Combines two 1D arrays as rows in a 2D array
hstack()
Stacks arrays horizontally (column-wise)
Concatenates two 1D arrays side by side
Example:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
v = np.vstack((a, b))
print(v)
# Output:
Hare, array_split() will ensure if all elements are included or not, even if splits could be uneven.
60. What happens when the split() method is used for splitting NumPy arrays?
When you use split() method, it will divide any given array into equal parts. But you must give a divisible number, which can equally divide the array into that divisible number of splits only.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
# Split into 3 equal parts
split = np.split(arr, 3)
print(split)
Use split() when you need equal-sized sub-arrays and the array size allows it.
61. How is Vectorization Related to Broadcasting in NumPy?
Vectorisation and broadcasting are interconnected features that enhance NumPy’s efficiency.
Vectorisation:
Definition: Performing operations on entire arrays without explicit loops.
Benefit: Faster and more readable code.
Broadcasting:
Definition: Automatically expanding arrays to compatible shapes for operations.
Purpose: Enables operations on arrays of different sizes.
Relation: When you use vectorization, it generally relies on broadcasting, which is used to perform seamless element-wise operations, across varying array shapes.
Example:
import numpy as np
a = np.array([1, 2, 3])
b = 2
result = a * b # Broadcasting scalar to array
print(result)
# Output: [2 4 6]
Here, broadcasting allows the scalar b to be multiplied with each element of array a via vectorisation.
62. How do you find the local peaks (or maxima) in a 1-D NumPy Array?
Identifying local maxima involves comparing each element with its neighbours.
Steps:
Compare each element (excluding the first and last) with its adjacent elements.
These methods adjust the array’s dimensions, facilitating various computational and data processing tasks.
66. Write a program for inserting space between characters of all elements in a NumPy array.
To insert spaces between characters, use NumPy’s string operations.
Example:
import numpy as np
arr = np.array(['Hello', 'World'])
# Insert space between characters
spaced = np.char.join(' ', arr)
print(spaced)
# Output:
# ['H e l l o' 'W o r l d']
Alternative Method:
spaced = np.array([‘ ‘.join(list(s)) for s in arr])
print(spaced)
Both approaches effectively add spaces between characters in each string element of the array.
67. Write a program for creating an integer array with values belonging to the range 10 and 60.
Use np.arange() or np.linspace() to create the desired range.
Using arange():
import numpy as np
arr = np.arange(10, 61) # 10 to 60 inclusive
print(arr)
Using linspace() with dtype=int:
arr = np.linspace(10, 60, num=51, dtype=int)
print(arr)
Output:
[10 11 12 … 58 59 60]
These methods generate an integer array containing values from 10 to 60 efficiently.
68. What is the difference between NumPy’s arange() and linspace() functions?
arange() and linspace() both create sequences of numbers but differ in parameters and usage.
Function
Description
Parameters
arange()
Creates array with evenly spaced values based on start, stop, step
start, stop, step
linspace()
Creates array with a specified number of evenly spaced values between start and stop
start, stop, num
Key Differences:
Step vs. Number of Points: arange() uses step size, while linspace() specifies the number of points.
Inclusivity: linspace() includes the stop value by default (endpoint=True), whereas arange() excludes it.
Floating Point Precision: linspace() is preferred for precise control over the number of elements.
69. Compare NumPy’s array() and asarray() functions.
array() and asarray() both create NumPy arrays but differ in copying behavior.
Function
Description
Copy Behavior
array()
Creates a new array object from input data
Always makes a copy
asarray()
Converts input to array without copying if possible
Does not copy if input is already an array
Example:
import numpy as np
lst = [1, 2, 3]
arr1 = np.array(lst)
arr2 = np.asarray(lst)
# If input is already an array
existing_arr = np.array([4, 5, 6])
arr3 = np.array(existing_arr) # Creates a copy
arr4 = np.asarray(existing_arr) # References existing array
print(arr3 is existing_arr) # Output: False
print(arr4 is existing_arr) # Output: True
Choose based on whether a new copy is necessary or referencing is sufficient.
70. Explain the difference between NumPy’s flatten() and ravel() functions.
Feature
flatten()
ravel()
Functionality
Returns a copy of the array flattened into 1D
Returns a flattened view of the array if possible
Memory
Always creates a new copy
Returns a view when possible, otherwise a copy
Modifiability
Changes to the flattened array do not affect the original
Changes affect the original array if a view is returned
Method Type
Method of ndarray
Method of ndarray
Usage Scenario
When an independent 1D array is needed
When a flattened array is needed without extra memory
71. How do NumPy’s where() and nonzero() methods differ?
Aspect
np.where()
np.nonzero()
Purpose
Select elements based on a condition
Find indices of non-zero elements
Usage
np.where(condition, x, y) or np.where(condition)
np.nonzero(array)
Return Type
Tuple of arrays for indices or modified array
Tuple of arrays containing indices
Flexibility
Can return elements from two arrays based on condition
Only returns indices where condition is True
Common Use
Conditional selection and assignment
Identifying locations of non-zero values
72. How is the broadcasting of arrays different from standard element-wise operations?
Feature
Broadcasting
Standard Element-Wise Operations
Definition
Automatically expands arrays to match shapes
Requires arrays to have identical shapes
Flexibility
Allows operations on arrays with different dimensions
Limited to same-shaped arrays
Mechanism
Follows specific rules to align array dimensions
Directly applies operations element by element
Use Cases
Performing operations between scalar and array, or differently shaped arrays
Operations on arrays of the same shape
Performance
Optimises memory and computation by avoiding explicit replication
May require explicit replication for shape matching
73. How does NumPy’s np.save() differ from np.savetxt()?
Feature
np.save()
np.savetxt()
File Format
Binary .npy format
Text-based formats like .txt, .csv
Data Types
Supports all NumPy data types, including objects
Primarily numerical data, limited to strings
Efficiency
Faster and more space-efficient due to binary storage
Slower and larger file sizes due to text encoding
Compatibility
Best used with NumPy for loading via np.load()
Can be opened with text editors and other software
Human-Readable
Not human-readable
Human-readable
Usage Scenario
Saving and loading NumPy arrays for computational use
Exporting data for sharing, analysis in other tools
Conclusion
NumPy remains an indispensable tool for anyone working with numerical data in Python. Its powerful array structures and optimised performance make it ideal for tasks ranging from simple data manipulation to complex scientific computations. Whether you’re a beginner or an experienced professional, mastering NumPy can significantly enhance your data processing capabilities.
This comprehensive list of over 70 interview questions and answers covers all essential aspects, helping you build confidence and demonstrate your expertise effectively. Embrace NumPy to streamline your workflows and excel in data-driven roles. Learn Python and its libraries like NumPy with the Integrated Program in Data Science, Artificial Intelligence & Machine Learning offered by Hero Vired in collaboration with Open Learning and get a professional certificate.
FAQs
How is NumPy used in data analysis?
NumPy is used to handle large datasets efficiently. It provides powerful array operations, enabling quick data manipulation, statistical analysis, and mathematical computations essential for data analysis tasks.
Can NumPy be integrated with other Python libraries?
Yes, NumPy integrates seamlessly with libraries like Pandas, Matplotlib, SciPy, TensorFlow, and others. This is to make numerical operations efficient and increase efficiency and efficiency.
Is NumPy suitable for machine learning applications?
Absolutely. NumPy is the basis for machine learning for tasks such as data pre-processing. Feature engineering and the use of algorithms that require fast and efficient numerical computations.
How does NumPy improve performance compared to standard Python lists?
NumPy arrays are stored more efficiently in memory and leverage optimised C libraries for computations, making operations faster and more scalable than using standard Python lists.
What are some common functions used in NumPy for data manipulation?
Common functions include reshape(), transpose(), concatenate(), split(), where(), and various statistical functions like mean(), sum(), and std().
Can NumPy handle missing data?
While NumPy itself has limited support for missing data, it can represent missing values using NaN. For more advanced handling, it is often used alongside Pandas, which offers robust features for managing missing data.
What are the benefits of using NumPy for numerical computations?
Benefits include increased efficiency, memory performance and various mathematical functions. It also includes easy integration with other libraries easily and the ability to perform complex operations with simple, easy-to-read code.
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.