└── README.md
/README.md:
--------------------------------------------------------------------------------
1 | # 70 Common NumPy Interview Questions in 2025
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | #### You can also find all 70 answers here 👉 [Devinterview.io - NumPy](https://devinterview.io/questions/machine-learning-and-data-science/numpy-interview-questions)
11 |
12 |
13 |
14 | ## 1. What is _NumPy_, and why is it important in _Machine Learning_?
15 |
16 | **NumPy** (Numerical Python) is a fundamental library in Python for numerical computations. It's a versatile tool primarily used for its advanced **multi-dimensional array support**.
17 |
18 | ### Key Features
19 |
20 | - **Task-Specific Modules**: NumPy offers a rich suite of mathematical functions in areas such as linear algebra, Fourier analysis, and random number generation.
21 |
22 | - **Performance and Speed**:
23 | - Enables vectorized operations.
24 | - Many of its core functions are implemented in `C` for optimized performance.
25 | - It uses contiguous blocks of memory, providing efficient caching and reducing overhead during processing.
26 |
27 | - **Broadcasting**: NumPy allows combining arrays of different shapes during arithmetic operations, facilitating streamlined computation.
28 |
29 | - **Linear Algebra**: It provides essential linear algebra operations, including matrix multiplication and decomposition methods.
30 |
31 | ### NumPy Arrays
32 |
33 | - **Homogeneity**: NumPy arrays are homogeneous, meaning they contain elements of the same data type.
34 | - **Shape Flexibility**: Arrays can be reshaped for specific computations without data duplication.
35 | - **Simple Storage**: They use efficient memory storage and can be created from regular Python lists.
36 |
37 | ### Performance Benchmarks
38 |
39 | 1. **Contiguous Memory**: NumPy arrays ensure that all elements in a multi-dimensional array are stored in contiguous memory blocks, unlike basic Python lists.
40 |
41 | 2. **No Type Checking**: NumPy arrays are specialized for numerical data, so they don't require dynamic type checks during operations.
42 |
43 | 3. **Vectorized Computing**: NumPy obviates the need for manual looping, making computations more efficient.
44 |
45 | ### Code Example: NumPy and Efficiency
46 |
47 | Here is the Python code:
48 |
49 | ```python
50 | # Using Python Lists
51 | python_list1 = [1, 2, 3, 4, 5]
52 | python_list2 = [6, 7, 8, 9, 10]
53 | result = [a + b for a, b in zip(python_list1, python_list2)]
54 |
55 | # Using NumPy Arrays
56 | import numpy as np
57 | np_array1 = np.array([1, 2, 3, 4, 5])
58 | np_array2 = np.array([6, 7, 8, 9, 10])
59 | result = np_array1 + np_array2
60 | ```
61 | In the above example, both cases opt for element-wise addition, yet the NumPy version is more concise and efficient.
62 |
63 |
64 | ## 2. Explain how _NumPy arrays_ are different from _Python lists_.
65 |
66 | **NumPy arrays** and **Python lists** are both versatile **data structures**, but they have distinct advantages and use-cases that set them apart.
67 |
68 | ### Key Distinctions
69 |
70 | #### Storage Mechanism
71 |
72 | - **Lists**: These are general-purpose and can store various data types. Items are often stored contiguously in memory, although the list object itself is an array of references, allowing flexibility in item sizes.
73 | - **NumPy Arrays**: These are designed for homogeneous data. Elements are stored in a contiguous block of memory, making them more memory-efficient and offering faster element access.
74 |
75 | #### Underlying Optimizations
76 |
77 | - **Lists**: Are not specialized for numerical operations and tends to be slower for such tasks. They are dynamic in size, allowing for both append and pop.
78 | - **NumPy Arrays**: Are optimized for numerical computations and provide vectorized operations, which can dramatically improve performance. Array size is fixed upon creation.
79 |
80 | ### Performance Considerations
81 |
82 | - **Memory Efficiency**: NumPy arrays can be more memory-efficient, especially for large datasets, because they don't need to store type information for each individual element.
83 | - **Element-Wise Operations**: NumPy's vectorized operations can be orders of magnitude faster than traditional Python loops, which are used for element-wise operations on lists.
84 | - **Size Flexibility**: Lists can grow and shrink dynamically, which may lead to extra overhead. NumPy arrays are more memory-friendly in this regard.
85 |
86 | #### Use in Machine Learning
87 |
88 | - **Python Lists**: Typically used for general data-handling tasks, such as reading in data before converting it to NumPy arrays.
89 | - **NumPy Arrays**: The foundational data structure for numerical data in Python. Most numerical computing libraries, including TensorFlow and scikit-learn, work directly with NumPy arrays.
90 |
91 |
92 | ## 3. What are the main _attributes_ of a _NumPy ndarray_?
93 |
94 | A NumPy `ndarray` is a multi-dimensional array that offers efficiency in numerical operations. Much of its strength comes from its **resilience with large datasets** and **agility in mathematical computations**.
95 |
96 | ### Main Attributes
97 |
98 | - **Shape**: A tuple representing the size of each dimension.
99 | - **Data Type (dtype)**: The type of data stored as elements in the array.
100 | - **Strides**: The number of bytes to "jump" in memory to move from one element to the next in each dimension.
101 |
102 | ### NumPy Examples:
103 |
104 | #### Shape Attribute
105 |
106 | ```python
107 | import numpy as np
108 |
109 | # 1D Array
110 | v = np.array([1, 2, 3])
111 | print(v.shape) # Output: (3,)
112 |
113 | # 2D Array
114 | m = np.array([[1, 2, 3], [4, 5, 6]])
115 | print(m.shape) # Output: (2, 3)
116 | ```
117 |
118 | #### Data Type Attribute
119 |
120 | ```python
121 | import numpy as np
122 |
123 | arr_int = np.array([1, 2, 3])
124 | print(arr_int.dtype) # Output: int64
125 |
126 | arr_float = np.array([1.0, 2.5, 3.7])
127 | print(arr_float.dtype) # Output: float64
128 | ```
129 |
130 | #### Strides Attribute
131 |
132 | The **strides** attribute defines how many bytes one must move in memory to go to the next element along each dimension of the array. If **`x.strides = (10,1)`**, this means that:
133 |
134 |
135 | - Moving one element in the last dimension, we move **1** byte in memory --- as it is a **float64**.
136 | - Moving one element in the first dimension, we move **10** bytes in memory.
137 |
138 | ```python
139 | import numpy as np
140 |
141 | x = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int16)
142 | print(x.strides) # Output: (6, 2)
143 | ```
144 |
145 |
146 | ## 4. How do you create a _NumPy array_ from a regular _Python list_?
147 |
148 | ### Problem Statement
149 |
150 | The task is to create a **NumPy array** from a standard Python list.
151 |
152 | ### Solution
153 |
154 | Several routes exist to transform a standard Python list into a NumPy array. Regardless of the method, it's crucial to have the `numpy` package installed.
155 |
156 | #### Using `numpy.array()`
157 |
158 | This is the most straightforward method.
159 |
160 | #### Implementation
161 |
162 | Here, I demonstrate how to convert a basic Python list to a NumPy array with `numpy.array()`. While it works for most cases, be cautious with nested lists as they have significant differences in behavior compared to Python lists.
163 |
164 | #### Code
165 |
166 | Here's the Python code:
167 |
168 | ```python
169 | import numpy as np
170 |
171 | python_list = [1, 2, 3]
172 |
173 | numpy_array = np.array(python_list)
174 | print(numpy_array)
175 | ```
176 |
177 | #### Output
178 |
179 | The output displays the NumPy array `[1 2 3]`.
180 |
181 | #### Using `numpy.asarray()`
182 |
183 | This is another method to convert a Python list into a NumPy array. The difference from `numpy.array()` is primarily in how it handles inputs like other NumPy arrays and nested lists.
184 |
185 | #### When to Use `numpy.asarray()`
186 |
187 | The function `numpy.asarray()` is beneficial when you're uncertain whether the input is a NumPy array or a list. It converts non-array types to arrays but leaves already existing NumPy arrays unchanged.
188 |
189 | #### Using `numpy.fromiter()`
190 |
191 | This method is useful when you have an iterable and want to create a NumPy array from its elements. An important point to consider is that the iterable is consumed as part of the array-creation process.
192 |
193 | #### Using `numpy.arange()` and `numpy.linspace()`
194 |
195 | If your intention is to create sequences of numbers, such as equally spaced data for plotting, NumPy offers specialized methods.
196 |
197 | - `numpy.arange(start, stop, step)` generates an array with numbers between `start` and `stop`, using `step` as the increment.
198 |
199 | - `numpy.linspace(start, stop, num)` creates an array with `num` equally spaced elements between `start` and `stop`.
200 |
201 |
202 | ## 5. Explain the concept of _broadcasting_ in _NumPy_.
203 |
204 | **Broadcasting** in NumPy is a powerful feature that enables efficient operations on arrays of different shapes without explicit array replication. It works by duplicating the elements along different axes and then carrying out the operation through these 'virtual' repetitions.
205 |
206 | ### Broadcasting Mechanism
207 |
208 | 1. **Axes Alignment**: Arrays with fewer dimensions are padded with additional axes on their leading side to match the shape of the other array.
209 |
210 | 2. **Compatible Dimensions**: For two arrays to be broadcast-compatible, at each axis, their sizes are either equal or one of them is 1.
211 |
212 | ### Example: Adding Scalars to Arrays
213 |
214 | When adding a scalar to an array, it's as if the scalar is broadcast to match the shape of the array before the addition:
215 |
216 | ```python
217 | import numpy as np
218 |
219 | arr = np.array([1, 2, 3])
220 | scalar = 10
221 | result = arr + scalar
222 |
223 | print(result) # Outputs: [11, 12, 13]
224 | ```
225 |
226 | ### Visual Representation
227 |
228 | The example below demonstrates what happens at each step of the **three-dimensional** array addition `arr` + `addition_vector`:
229 |
230 | ```python
231 | import numpy as np
232 |
233 | arr = np.array(
234 | [
235 | [[1, 2, 3], [4, 5, 6]],
236 | [[7, 8, 9], [10, 11, 12]]
237 | ]
238 | )
239 |
240 | addition_vector = np.array([1, 10, 100])
241 | sum_result = arr + addition_vector
242 |
243 | print(f"Array:\n{arr}\n\nAddition Vector:\n{addition_vector}\n\nResult:\n{sum_result}")
244 | ```
245 |
246 | The broadcasting process, along with the output, is visually depicted in the code.
247 |
248 | ### Real-world Application: Visualizing Multidimensional Data
249 |
250 | NumPy broadcasting is invaluable in applications where visualizing or analyzing **multidimensional named data** is essential, permitting easy manipulations without resorting to loops or explicit data copying.
251 |
252 | For instance, matching a three-dimensional RGB image (represented by a 3D NumPy array) with a 1D intensity array prior to modifying the image's pixels is simplified through broadcasting.
253 |
254 |
255 | ## 6. What are the _data types_ supported by _NumPy arrays_?
256 |
257 | **NumPy** deals with a variety of data types, which it refers to as **dtypes**.
258 |
259 | ### NumPy Data Types
260 |
261 | NumPy data types build upon the primitive types offered by the machine:
262 |
263 | 1. **Basic Types**: `int`, `float`, and `bool`.
264 |
265 | 2. **Floating Point Types**: `np.float16`, `np.float32`, and `np.float64`.
266 |
267 | 3. **Complex Numbers**: `np.complex64` and `np.complex128`.
268 |
269 | 4. **Integers**: `np.int8`, `np.int16`, `np.int32`, and `np.int64`, along with their unsigned variants.
270 |
271 | 5. **Boolean**: Represents `True` or `False`.
272 |
273 | 6. **Strings**: `np.str_`.
274 |
275 | 7. **Datetime64**: Date and time data with time zone information.
276 |
277 | 8. **Object**: Allows any data type.
278 |
279 | 9. **Categories and Structured Arrays**: Specialized for categorical data and structured records.
280 |
281 | **NumPy** enables you to define arrays with the specific data types:
282 |
283 | ```python
284 | import numpy as np
285 |
286 | my_array = np.array([1, 2, 3]) # Defaults to int64
287 | float_array = np.array([1.5, 2.5, 3.5], dtype=np.float16)
288 | bool_array = np.array([True, False, True], dtype=np.bool)
289 |
290 | # Specifying the dtype of string
291 | str_array = np.array(['cat', 'dog', 'elephant'], dtype=np.str_)
292 | ```
293 |
294 |
295 | ## 7. How do you inspect the _shape_ and _size_ of a _NumPy array_?
296 |
297 | You can examine the **shape** and **size** of a NumPy array using two key attributes: `shape` and `size`.
298 |
299 | ### Code Example: Shape and Size Attributes
300 |
301 | Here is the Python code:
302 |
303 | ```python
304 | import numpy as np
305 |
306 | # Create a 2D array
307 | arr = np.array([[1, 2, 3], [4, 5, 6]])
308 |
309 | # Access shape and size attributes
310 | shape = arr.shape
311 | size = arr.size
312 |
313 | print("Shape:", shape) # Outputs: (2, 3)
314 | print("Size:", size) # Outputs: 6
315 | ```
316 |
317 |
318 | ## 8. What is the difference between a _deep copy_ and a _shallow copy_ in _NumPy_?
319 |
320 | In NumPy, you can create **shallow** and **deep** copies using the `.copy()` method.
321 |
322 | Each type of copy preserves ndarray data in a different way, impacting their link to the original array and potential impact of one on the other.
323 |
324 |
325 | ### Shallow Copy
326 |
327 | A shallow copy creates a new array object, but it does not duplicate the actual **data**. Instead, it points to the data of the original array. Modifying the shallow copy will affect the original array and vice versa.
328 |
329 |
330 | The shallow copy is a view of the original array. You can create it either by calling `.copy()` method on an array or using a slice operation.
331 |
332 | Here is an example:
333 |
334 | ```python
335 | import numpy as np
336 |
337 | original = np.array([1, 2, 3])
338 | shallow = original.copy()
339 |
340 | # Modifying the shallow copy
341 | shallow[0] = 100 # Modifications do not affect the original
342 | print(shallow) # [100, 2, 3]
343 | print(original) # [1, 2, 3]
344 |
345 | # Modifying the original
346 | original[1] = 200
347 | print(shallow) # [100, 200, 3] # The shallow copy is affected
348 | print(original) # [1, 200, 3]
349 | ```
350 |
351 | ### Deep Copy
352 |
353 | A deep copy creates a new array as well as creates separate copies of arrays and their data. **Modifying a deep copy does not affect the original array**, and vice versa.
354 |
355 | In NumPy, you can achieve a deep copy using the same `.copy()` method but with the `order='K'` parameter, or by using `np.array(array, copy=True)`. Here is an example:
356 |
357 | ```python
358 | import numpy as np
359 |
360 | # For a 1D array:
361 | original_deep = np.array([1, 2, 3], copy=True) # This creates a deep copy
362 | original_deep[0] = 100 # Modifications do not affect the original
363 | print(original_deep) # [100, 2, 3]
364 | print(original) # [1, 2, 3]
365 |
366 | # For a 2D array:
367 | original_2d = np.array([[1, 2], [3, 4]])
368 | deep_2d = original_2d.copy(order='K') # Deep copy with 'K'
369 | deep_2d[0, 0] = 100
370 | print(deep_2d) # [[100, 2], [3, 4]]
371 | print(original_2d) # [[1, 2], [3, 4]]
372 |
373 |
374 | ## 9. How do you perform _element-wise operations_ in _NumPy_?
375 |
376 | **Element-wise operations** in NumPy use broadcasting to efficiently apply a single operation to multiple elements in a NumPy array.
377 |
378 | ### Key Functions
379 |
380 | - **Basic Math Functions**: `np.add()`, `np.subtract()`, `np.multiply()`, `np.divide()`, `np.power()`, `np.mod()`
381 | - **Trigonometric Functions**: `np.sin()`, `np.cos()`, `np.tan()`, `np.arcsin()`, `np.arccos()`, `np.arctan()`
382 | - **Rounding**: `np.round()`, `np.floor()`, `np.ceil()`, `np.trunc()`
383 | - **Exponents and Logarithms**: `np.exp()`, `np.log()`, `np.log10()`
384 | - **Other Elementary Functions**: `np.sqrt()`, `np.cbrt()`, `np.square()`
385 | - **Absolute and Sign Functions**: `np.abs()`, `np.sign()`
386 | - **Advanced Array Operations**: `np.dot()`, `np.inner()`, `np.outer()`
387 |
388 | ### Example: Basic Math Operations
389 |
390 | Here is the Python code:
391 |
392 | ```python
393 | import numpy as np
394 |
395 | # Generating the arrays
396 | arr1 = np.array([1, 2, 3, 4])
397 | arr2 = np.array([5, 6, 7, 8])
398 |
399 | # Element-wise addition
400 | print(np.add(arr1, arr2)) # Output: [ 6 8 10 12]
401 |
402 | # Element-wise subtraction
403 | print(np.subtract(arr1, arr2)) # Output: [-4 -4 -4 -4]
404 |
405 | # Element-wise multiplication
406 | print(np.multiply(arr1, arr2)) # Output: [ 5 12 21 32]
407 |
408 | # Element-wise division
409 | print(np.divide(arr2, arr1)) # Output: [5. 3. 2.33333333 2. ]
410 |
411 | # Element-wise power
412 | print(np.power(arr1, 2)) # Output: [ 1 4 9 16]
413 |
414 | # Element-wise modulo
415 | print(np.mod(arr2, arr1)) # Output: [0 0 1 0]
416 | ```
417 |
418 |
419 | ## 10. What are _universal functions_ (_ufuncs_) in _NumPy_?
420 |
421 | In **NumPy**, a **Universal Function** (ufunc) is a function that operates element-wise on **ndarrays**, optimizing performance.
422 |
423 | Whether it's a basic arithmetic operation, advanced math function, or a comparison, ufuncs are designed to process data fast.
424 |
425 | ### Key Features
426 |
427 | - **Element-Wise Operation**: Ufuncs process each element in an ndarray individually. This technique reduces the need for explicit loops in Python, leading to enhanced efficiency.
428 |
429 | - **Broadcasting**: Ufuncs integrate seamlessly with **NumPy's broadcasting rules**, making them versatile.
430 |
431 | - **Code Optimization**: These functions utilize low-level array-oriented operations for optimized execution.
432 |
433 | - **Type Conversion**: You can specify the data type for output ndarray, or let NumPy determine the optimal type automatically for you.
434 |
435 | - **Multi-Threaded Execution**: Ufuncs are highly compatible with multi-threading to expedite computation.
436 |
437 | ### Ufunc Categories
438 |
439 | 1. **Unary Ufuncs**: Operate on a single ndarray.
440 |
441 | Example: $\exp(5)$
442 |
443 | 2. **Binary Ufuncs**: Perform operations between two distinct arrays.
444 |
445 | Example: $10 + \cos(\text{{arr1}})$
446 |
447 | ### Code Example: Unique Advantages of Using Ufuncs
448 |
449 | - Ufuncs Empower Faster Computing:
450 | - Regex and String Operations: Ufuncs are quicker and more efficient compared to list comprehension and string methods.
451 | - Set Operations: Ufuncs enable rapid union, intersection, and set difference with ndarrays.
452 |
453 | - Enhanced NumPy Functions:
454 | - Log and Exponential Functions: NumPy provides faster and more accurate methods than standard Python math functions.
455 | - Trigonometric Functions: Ufuncs are vectorized, offering faster calculations for arrays of angles.
456 | - Special Functions: NumPy features an array of special mathematical functions, including Bessel functions and gamma functions, optimized for array computations.
457 |
458 | ```python
459 | import numpy as np
460 |
461 | arr = np.array([1, 2, 3])
462 |
463 | # Using ".prod()" reduces redundancy and accelerates functional operation.
464 | result = arr.prod()
465 | print(result)
466 |
467 | # Accessing unique elements via ufunc "np.unique" is more streamlined and quicker.
468 | unique_elements = np.unique(arr)
469 | print(unique_elements)
470 | ```
471 |
472 |
473 | ## 11. How do you perform _matrix multiplication_ using _NumPy_?
474 |
475 | ### Problem Statement
476 |
477 | The task is to explain how to perform **matrix multiplication** using **NumPy**.
478 |
479 | ### Solution
480 |
481 | NumPy's `np.dot()` function or the `@` operator is used for both **matrix multiplication** and **dot product**.
482 |
483 | #### Matrix Multiplication
484 |
485 | Two matrices are multiplied using the `np.dot()` function.
486 |
487 | - $C = A \times B$ where $A$ is a $2 \times 3$ matrix and $B$ is a $3 \times 2$ matrix.
488 |
489 | $$
490 | C = \begin{bmatrix} A_{11} & A_{12} & A_{13} \\ A_{21} & A_{22} & A_{23} \end{bmatrix} \times \begin{bmatrix} B_{11} & B_{12} \\ B_{21} & B_{22} \\ B_{31} & B_{32} \end{bmatrix}
491 | $$
492 |
493 | $$
494 | C = \begin{bmatrix} A_{11} \times B_{11} + A_{12} \times B_{21} + A_{13} \times B_{31} & A_{11} \times B_{12} + A_{12} \times B_{22} + A_{13} \times B_{32} \\ A_{21} \times B_{11} + A_{22} \times B_{21} + A_{23} \times B_{31} & A_{21} \times B_{12} + A_{22} \times B_{22} + A_{23} \times B_{32} \end{bmatrix}
495 | $$
496 |
497 | #### Broadcasting in NumPy
498 |
499 | NumPy has a built-in capability, known as **broadcasting**, for performing operations on arrays of different shapes. If the shapes of two arrays are not compatible for an element-wise operation, NumPy uses broadcasting to make the shapes compatible.
500 |
501 | #### Implementation
502 |
503 | Here is the Python code using NumPy:
504 |
505 | ```python
506 | import numpy as np
507 |
508 | A = np.array([[1, 2, 3], [4, 5, 6]])
509 | B = np.array([[7, 8], [9, 10], [11, 12]])
510 |
511 | # Matrix Multiplication
512 | C = np.dot(A, B)
513 | # The result is: [[ 58 64] [139 154]]
514 | ```
515 |
516 |
517 | ## 12. Explain how to _invert a matrix_ in _NumPy_.
518 |
519 | ### Problem Statement
520 |
521 | The goal is to **invert a matrix** using NumPy.
522 |
523 | ### Solution
524 |
525 | In NumPy, you can use the `numpy.linalg.inv` function to find the inverse of a matrix.
526 |
527 | #### Conditions:
528 |
529 | 1. The matrix must be square, i.e., it should have an equal number of rows and columns.
530 | 2. The matrix should be non-singular (have a non-zero determinant).
531 |
532 | #### Algorithm Steps:
533 |
534 | 1. Import NumPy: `import numpy as np`
535 | 2. Define the matrix: `A = np.array([[4, 7], [2, 6]])`
536 | 3. Compute the matrix inverse: `A_inv = np.linalg.inv(A)`
537 |
538 | #### Implementation
539 |
540 | Here's the complete Python code:
541 |
542 | ```python
543 | import numpy as np
544 |
545 | # Define the matrix
546 | A = np.array([[4, 7], [2, 6]])
547 |
548 | # Compute the matrix inverse
549 | A_inv = np.linalg.inv(A)
550 | print(A_inv)
551 | ```
552 |
553 | The output for the given matrix `A` is:
554 |
555 | ```
556 | [[ 0.6 -0.7]
557 | [-0.2 0.4]]
558 | ```
559 |
560 |
561 | ## 13. How do you calculate the _determinant_ of a _matrix_?
562 |
563 | ### Problem Statement
564 |
565 | The **determinant** of a matrix is a scalar value that can be derived from the elements of a **square matrix**.
566 |
567 | Calculating the determinant of a matrix is a fundamental operation in linear algebra, with applications in finding the **inverse of a matrix**, solving systems of linear equations, and more.
568 |
569 | ### Solution
570 |
571 | #### Method 1: Numerical Calculation
572 |
573 | For a numeric $n \times n$ matrix, the determinant is calculated using **Laplace's expansion** along rows or columns. This method is computationally expensive, with a time complexity of $O(n!)$.
574 |
575 | #### Method 2: Matrix Decomposition
576 |
577 | An alternative, more efficient approach involves using **matrix decomposition** methods such as **LU decomposition** or **Cholesky decomposition**. However, these methods are more complex and are not commonly used for determinant calculation alone.
578 |
579 | #### Method 3: NumPy Function
580 |
581 | The most convenient and efficient method, especially for large matrices, is to make use of the `numpy.linalg.det` function, which internally utilizes LU decomposition.
582 |
583 | #### Implementation
584 |
585 | Here is Python code:
586 |
587 | ```python
588 | import numpy as np
589 |
590 | # Define the matrix
591 | A = np.array([[1, 2], [3, 4]])
592 |
593 | # Calculate the determinant
594 | det_A = np.linalg.det(A)
595 | print("Determinant of A:", det_A)
596 | ```
597 |
598 | #### Output
599 |
600 | ```
601 | Determinant of A: -2.0
602 | ```
603 |
604 | ### Key Insight
605 |
606 | The determinant of a matrix is crucial in various areas of mathematics and engineering, including linear transformations, volume scaling factors, and the characteristic polynomial of a matrix, often used in Eigenvalues and Eigenvectors calculations.
607 |
608 |
609 | ## 14. What is the use of the `_axis_` parameter in _NumPy functions_?
610 |
611 | The `_axis_` parameter in **NumPy** enables operations to be carried out along a specific axis of a multi-dimensional array, providing more granular control over results.
612 |
613 | ### Functions with `_axis_` Parameter
614 |
615 | Many NumPy functions incorporate the `_axis_` parameter to modify behavior based on the specified axis value.
616 |
617 | ### Common Functions
618 |
619 | - **Math Operations**: Functions such as `mean`, `sum`, `std`, and `min` perform element-wise operations or aggregations, allowing you to focus on specific axes.
620 |
621 | - **Array Manipulation**: `concatenate`, `split`, and others enable flexible array operations while considering the specified axis.
622 |
623 | - **Numerical Analysis**: Functions like `trapezoid` and `Simpsons` provide integration along a specific axis, especially useful for multi-dimensional datasets.
624 |
625 | ### Practical Examples
626 |
627 | #### Mean Calculation
628 |
629 | Suppose you have the following dataset representing quiz scores:
630 |
631 | ```python
632 | import numpy as np
633 |
634 | # Quiz scores for five students across four quizzes
635 | scores = np.array([[8, 6, 7, 9],
636 | [4, 7, 6, 8],
637 | [3, 5, 9, 2],
638 | [4, 6, 2, 8],
639 | [5, 2, 7, 9]])
640 | ```
641 |
642 | You can calculate the mean scores for each quiz with:
643 |
644 | ```python
645 | # axis=0 calculates the mean along the first dimension (students)
646 | quiz_means = np.mean(scores, axis=0)
647 | ```
648 |
649 | #### Splitting Arrays
650 |
651 | Consider you want to separate a dataset into two based on a specific criterion. You can do this using `split`:
652 |
653 | ```python
654 | # Assign students into two groups based on the mean quiz score
655 | group1, group2 = np.split(scores, [2], axis=1)
656 | ```
657 |
658 | In this case, it splits the `scores` array into two arrays at column index 2, resulting in `group1` containing scores from the first two quizzes and `group2` from the last two quizzes.
659 |
660 | #### Integration over Multi-dimensional Arrays
661 |
662 | NumPy provides functions to integrate arrays along different axes. For example, using the `trapz` function can calculate the area under the curve represented by the array:
663 |
664 | ```python
665 | # Define a 2D array representing a surface
666 | surface = np.array([[1, 2, 3, 4],
667 | [2, 3, 4, 5]])
668 |
669 | # Perform integration along axis 0
670 | area_under_curve = np.trapz(surface, axis=0)
671 | ```
672 |
673 |
674 | ## 15. How do you _concatenate_ two _arrays_ in _NumPy_?
675 |
676 | ### Problem Statement
677 |
678 | The task is to combine two $\text{NumPy}$ arrays. Concatenation can occur **horizontally** (column-wise) or **vertically** (row-wise).
679 |
680 | ### Solution
681 |
682 | In `NumPy`, we can concatenate arrays using the `numpy.concatenate()`, `numpy.hstack()`, or `numpy.vstack()` functions.
683 |
684 | #### Key Points
685 |
686 | - `numpy.concatenate()`: Combines arrays along a specified **axis**.
687 | - `numpy.hstack()`: Stacks arrays horizontally.
688 | - `numpy.vstack()`: Stacks arrays vertically.
689 |
690 | Let's explore these methods in more detail.
691 |
692 | #### Implementation
693 |
694 | Here is the Python code:
695 |
696 | ```python
697 | import numpy as np
698 |
699 | # Sample arrays
700 | arr1 = np.array([[1, 2], [3, 4]])
701 | arr2 = np.array([[5, 6], [7, 8]])
702 |
703 | # Concatenation along rows (vertically)
704 | print(np.concatenate((arr1, arr2), axis=0)) # Output: [[1 2] [3 4] [5 6] [7 8]]
705 |
706 | # Concatenation along columns (horizontally)
707 | print(np.concatenate((arr1, arr2), axis=1)) # Output: [[1 2 5 6] [3 4 7 8]]
708 |
709 | # Stacking horizontally
710 | print(np.hstack((arr1, arr2))) # Output: [[1 2 5 6] [3 4 7 8]]
711 |
712 | # Stacking vertically
713 | print(np.vstack((arr1, arr2))) # Output: [[1 2] [3 4] [5 6] [7 8]]
714 | ```
715 |
716 |
717 |
718 |
719 | #### Explore all 70 answers here 👉 [Devinterview.io - NumPy](https://devinterview.io/questions/machine-learning-and-data-science/numpy-interview-questions)
720 |
721 |
722 |
723 |
724 |
725 |
726 |
727 |
728 |
--------------------------------------------------------------------------------