Find Interview Questions for Top Companies
Ques:- What is vectorization and how does it improve performance in NumPy
Right Answer:
Vectorization is the process of applying operations to entire arrays or large datasets at once, rather than using loops to process individual elements. In NumPy, this improves performance by leveraging optimized, low-level implementations that take advantage of parallel processing and reduce the overhead of Python loops, resulting in faster computations.
Ques:- What is NumPy and why is it important in Python programming
Right Answer:
NumPy is a powerful library in Python used for numerical computing. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. It is important because it enables efficient data manipulation and computation, making it essential for scientific computing, data analysis, and machine learning tasks.
Ques:- How do you stack or split arrays in NumPy
Right Answer:
You can stack arrays in NumPy using `np.vstack()` for vertical stacking, `np.hstack()` for horizontal stacking, and `np.dstack()` for depth stacking. To split arrays, use `np.split()`, `np.hsplit()`, or `np.vsplit()` depending on the desired axis.
Ques:- What is a NumPy array and how does it differ from a Python list
Asked In :- worldline global,
Right Answer:
A NumPy array is a powerful, multidimensional data structure provided by the NumPy library, designed for numerical computations. It differs from a Python list in that NumPy arrays are more efficient for large data sets, support element-wise operations, and require all elements to be of the same data type, whereas Python lists can hold mixed data types and are less efficient for numerical operations.
Ques:- What is the difference between np.zeros, np.ones, and np.empty
Right Answer:
`np.zeros` creates an array filled with zeros, `np.ones` creates an array filled with ones, and `np.empty` creates an array without initializing its values (the values will be whatever is in memory at that time).
Ques:- How do you create arrays in NumPy using different functions
Right Answer:
You can create arrays in NumPy using the following functions:

1. `np.array()`: Create an array from a list or tuple.
```python
import numpy as np
arr = np.array([1, 2, 3])
```

2. `np.zeros()`: Create an array filled with zeros.
```python
arr = np.zeros((2, 3)) # 2x3 array of zeros
```

3. `np.ones()`: Create an array filled with ones.
```python
arr = np.ones((2, 3)) # 2x3 array of ones
```

4. `np.arange()`: Create an array with a range of values.
```python
arr = np.arange(0, 10, 2) # Array with values from 0 to 10 with step 2
```

5. `np.linspace()`: Create
Ques:- How do you use boolean indexing in NumPy
Right Answer:
Boolean indexing in NumPy is done by using a boolean array to select elements from another array. For example:

```python
import numpy as np

# Create an array
arr = np.array([1, 2, 3, 4, 5])

# Create a boolean array
bool_idx = arr > 2

# Use boolean indexing to get elements greater than 2
result = arr[bool_idx] # result will be array([3, 4, 5])
```

You can also directly use a condition:

```python
result = arr[arr > 2] # result will be array([3, 4, 5])
```
Ques:- What is the difference between ndarray and array in NumPy
Right Answer:
In NumPy, `ndarray` is the core data structure that represents a multi-dimensional array, while `array` is a function used to create an `ndarray` object from a Python list or tuple. Essentially, `ndarray` is the type of the array, and `array` is the method to create it.
Ques:- What are some common performance tips when working with large datasets in NumPy
Right Answer:
1. Use contiguous arrays: Ensure arrays are stored in contiguous memory for faster access.
2. Utilize vectorization: Replace loops with NumPy's vectorized operations to speed up calculations.
3. Use appropriate data types: Choose the smallest data type that can hold your data to save memory.
4. Avoid unnecessary copies: Use views instead of copies when possible to reduce memory usage.
5. Leverage broadcasting: Use broadcasting to perform operations on arrays of different shapes without explicit looping.
6. Use in-place operations: Modify arrays in place to save memory and improve performance.
7. Optimize array creation: Use functions like `np.arange()` or `np.linspace()` instead of Python lists for faster array creation.
8. Use memory-mapped files: For very large datasets, consider using memory-mapped files to avoid loading everything into memory at once.
Ques:- How do you perform basic arithmetic operations on NumPy arrays
Right Answer:
You can perform basic arithmetic operations on NumPy arrays using standard operators. For example:

- Addition: `array1 + array2`
- Subtraction: `array1 - array2`
- Multiplication: `array1 * array2`
- Division: `array1 / array2`

These operations are element-wise and can also be performed with scalars.
Ques:- What are broadcasting rules in NumPy
Right Answer:
Broadcasting rules in NumPy allow arrays of different shapes to be used together in arithmetic operations. The rules are:

1. If the arrays have a different number of dimensions, the shape of the smaller-dimensional array is padded with ones on the left side until both shapes are the same.
2. The sizes of the dimensions are compared element-wise:
- If they are the same, they are compatible.
- If one of them is 1, it can be stretched to match the other size.
- If they are different and neither is 1, the arrays are not compatible and cannot be broadcast together.
Ques:- How do you slice and index arrays in NumPy
Right Answer:
In NumPy, you can slice and index arrays using the following syntax:

1. **Basic indexing**: Use square brackets with indices.
```python
array[index]
```

2. **Slicing**: Use a colon to specify a range.
```python
array[start:stop:step]
```

3. **Multi-dimensional arrays**: Use commas to separate indices for each dimension.
```python
array[row_index, column_index]
```

4. **Slicing multi-dimensional arrays**:
```python
array[start_row:end_row, start_col:end_col]
```

Example:
```python
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
slice = arr[0:2, 1:3] # This will give you [[2, 3], [5,
Ques:- What are some common functions for array manipulation in NumPy
Right Answer:
Some common functions for array manipulation in NumPy include:

1. `np.reshape()`
2. `np.flatten()`
3. `np.concatenate()`
4. `np.vstack()`
5. `np.hstack()`
6. `np.split()`
7. `np.transpose()`
8. `np.roll()`
9. `np.repeat()`
10. `np.unique()`
Ques:- How do you reshape, flatten, or transpose arrays in NumPy
Right Answer:
To reshape an array in NumPy, use `numpy.reshape()`. To flatten an array, use `numpy.flatten()` or `numpy.ravel()`. To transpose an array, use `numpy.transpose()` or the `.T` attribute.
Ques:- What is the difference between copy and view in NumPy
Right Answer:
In NumPy, a copy creates a new array that is a duplicate of the original array, and changes to the copy do not affect the original. A view, on the other hand, is a reference to the original array, so changes to the view will affect the original array.
Ques:- How do you perform aggregation functions like sum, mean, or std in NumPy
Right Answer:
You can perform aggregation functions in NumPy using the following methods:

- **Sum**: `numpy.sum(array)`
- **Mean**: `numpy.mean(array)`
- **Standard Deviation**: `numpy.std(array)`

Replace `array` with your NumPy array variable.
Ques:- What is the purpose of the axis parameter in NumPy functions
Right Answer:
The axis parameter in NumPy functions specifies the dimension along which the operation is performed. For example, in a 2D array, axis=0 refers to operations along the rows (downward), while axis=1 refers to operations along the columns (across).
Ques:- How do you generate random numbers using NumPy
Asked In :- Metrics4 Analytics,
Right Answer:
You can generate random numbers using NumPy by importing the library and using functions from the `numpy.random` module. For example:

```python
import numpy as np

# Generate a single random number between 0 and 1
random_number = np.random.rand()

# Generate an array of 5 random numbers between 0 and 1
random_array = np.random.rand(5)

# Generate random integers between a specified range
random_integers = np.random.randint(low=0, high=10, size=5)
```
Ques:- What are masked arrays and when would you use them
Right Answer:
Masked arrays are arrays that allow you to hide or ignore certain elements based on a mask (a boolean array). You would use them when you need to handle data with missing or invalid values without removing those elements from the array.
Ques:- How do you handle missing or NaN values in NumPy
Right Answer:
You can handle missing or NaN values in NumPy using the following methods:

1. **Using `numpy.isnan()`**: To identify NaN values.
2. **Using `numpy.nan_to_num()`**: To replace NaN values with a specified number (e.g., 0).
3. **Using `numpy.nanmean()`**: To calculate the mean while ignoring NaN values.
4. **Using `numpy.nanstd()`**: To calculate the standard deviation while ignoring NaN values.
5. **Using boolean indexing**: To filter out NaN values from an array.

Example:
```python
import numpy as np

# Example array
arr = np.array([1, 2, np.nan, 4])

# Replace NaN with 0
arr_no_nan = np.nan_to_num(arr)

# Calculate mean ignoring NaN
mean_value = np.nanmean(arr)
```


AmbitionBox Logo

What makes Takluu valuable for interview preparation?

1 Lakh+
Companies
6 Lakh+
Interview Questions
50K+
Job Profiles
20K+
Users