100% found this document useful (1 vote)
123 views1 page

NumPy Programming Challenges

numpy_programming_questions

Uploaded by

Punit Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
123 views1 page

NumPy Programming Challenges

numpy_programming_questions

Uploaded by

Punit Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

10 Programming Questions on NumPy

Topics
1. Write a Python program to calculate basic statistical measures like mean, median,
standard deviation, and variance for a given 1D NumPy array.

2. Implement a Python program to sort a NumPy array along different axes. Demonstrate
how to sort a 2D array by columns and by rows.

3. Write a Python program that uses NumPy to search for a specific value in an array and
return its index. Also, demonstrate how to count the occurrences of a specific value in the
array.

4. Write a Python program to demonstrate the difference between a copy and a view in
NumPy. Modify both the original array and the view, and observe the changes in the data.

5. Write a Python program that uses the NumPy matrix library to create and manipulate a
matrix. Perform operations like addition, subtraction, and matrix multiplication.

6. Write a Python program to calculate the dot product of two vectors and the matrix
product of two matrices using NumPy’s linear algebra module.

7. Implement a Python program to solve a system of linear equations using NumPy. Use the
[Link]() function to find the solution of the system.

8. Write a Python program that computes the eigenvalues and eigenvectors of a square
matrix using NumPy’s linalg module. Demonstrate the use of these values.

9. Write a Python program that calculates the inverse and determinant of a given matrix
using NumPy’s linalg module. Also, demonstrate how to handle singular matrices.

10. Write a Python program to perform Singular Value Decomposition (SVD) on a matrix
using NumPy. Demonstrate how to reconstruct the matrix using the SVD components.

Common questions

Powered by AI

SVD is a factorization technique that decomposes a matrix into three components: U, Σ, and V*. Using np.linalg.svd(), a matrix is decomposed, and reconstruction involves multiplying these components (U @ np.diag(Σ) @ V*). SVD is fundamental in data compression and noise reduction .

NumPy automates complex numerical tasks with efficient vectorized operations, reducing runtime and improving code readability over regular Python loops. Its comprehensive functions simplify statistical calculations, matrix operations, and linear algebra, making it indispensable for scientific computing .

Using NumPy’s linalg.eig(), eigenvalues and eigenvectors of a square matrix are computed. Given a matrix 'A', calling linalg.eig(A) returns a tuple with eigenvalues and eigenvectors. These components are crucial in understanding transformations, stability, and other matrix properties in system analyses .

Matrix operations in NumPy involve using operators like '+' for addition and '-' for subtraction. For multiplication, np.matmul() is used instead of the '*' operator. Given matrices A and B, operations are performed as follows: A + B, A - B, and np.matmul(A, B) for addition, subtraction, and multiplication respectively .

NumPy provides functions like np.mean(), np.median(), np.std(), and np.var() to calculate the mean, median, standard deviation, and variance of a 1D array, respectively. For example, given an array 'arr', one can compute these measures as follows: np.mean(arr), np.median(arr), np.std(arr), and np.var(arr).

In NumPy, a copy is a complete replica of an array. Modifications to the copy do not affect the original array. A view, however, is a new array object that looks at the same data. Changes to the view will affect the original array and vice versa, because they share the same data buffer .

NumPy’s linalg.inv() calculates inverses, and linalg.det() calculates determinants. A singular matrix, which lacks an inverse, will result in an LinAlgError when using linalg.inv(). Handling singular matrices involves checking the determinant if it’s zero, indicating singularity, and possibly using pseudoinverses with linalg.pinv().

NumPy allows searching for values using np.where() to find indices of a specified value in an array, and using .count_nonzero() alongside boolean masking to count occurrences. np.where(arr == value) returns indices, whereas np.count_nonzero(arr == value) returns the count of occurrences. These methods provide different levels of detail about the value's presence in the array .

NumPy’s linalg module provides the function linalg.solve() to solve linear equations. By inputting a coefficient matrix 'A' and a dependent variable vector 'B' into linalg.solve(A, B), the system's solution vector is directly calculated. This method is efficient and leverages matrix operations .

To sort a 2D array by columns, you use np.sort() with axis=0, while for rows, use axis=1. The function np.sort(arr, axis=0) sorts each column individually, whereas np.sort(arr, axis=1) sorts each row. This distinction allows different sorting behaviors depending on whether the focus is on columns or rows .

You might also like