0% found this document useful (0 votes)
7 views2 pages

Numpy Assignment: Beginner to Advanced Tasks

The document outlines a comprehensive NumPy assignment divided into three levels: Easy, Intermediate, and Advanced. Each level contains a series of tasks that involve creating and manipulating NumPy arrays, performing mathematical operations, and applying advanced techniques such as matrix operations and linear regression. The tasks range from basic array creation and operations to complex concepts like eigenvalues, SVD decomposition, and simulations.

Uploaded by

projectajinkya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Numpy Assignment: Beginner to Advanced Tasks

The document outlines a comprehensive NumPy assignment divided into three levels: Easy, Intermediate, and Advanced. Each level contains a series of tasks that involve creating and manipulating NumPy arrays, performing mathematical operations, and applying advanced techniques such as matrix operations and linear regression. The tasks range from basic array creation and operations to complex concepts like eigenvalues, SVD decomposition, and simulations.

Uploaded by

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

Numpy Assignment

Easy Level

1. Create a NumPy Array: Create a 1D NumPy array containing the numbers from 1 to 10.

2. Array Operations: Add, subtract, multiply, and divide two NumPy arrays of the same shape.

3. Array Slicing: Given a 2D NumPy array, extract a 3x3 subarray from it.

4. Array Shape: Create a 2D NumPy array of shape (3, 4) and find its shape, size, and number
of dimensions.

5. Linspace and Arange: Generate an array of 50 evenly spaced values between 0 and 10 using
`[Link]()`. Then, generate an array of numbers from 0 to 10 with a step of 0.5 using
`[Link]()`.

6. Zeros and Ones: Create a 5x5 array filled with zeros and another filled with ones.

7. Identity Matrix: Create a 4x4 identity matrix using NumPy.

8. Element-wise Operations: Perform element-wise addition, subtraction, multiplication, and


division on two arrays.

9. Basic Statistics: Given an array, find the minimum, maximum, mean, and standard deviation.

10. Random Numbers: Generate a 3x3 array of random integers between 1 and 50.

Intermediate Level

11. Matrix Multiplication: Multiply two 2D arrays (matrices) using NumPy's `dot` function.

12. Array Reshaping: Reshape a 1D array of 12 elements into a 3x4 2D array.

13. Boolean Indexing: Given an array of integers, filter out the elements that are greater than a
certain value using boolean indexing.

14. Sorting Arrays: Sort a 1D array of random integers in ascending and descending order.

15. Unique Elements: Find the unique elements in a NumPy array and their counts using
`[Link]()`.

16. Array Broadcasting: Perform element-wise multiplication of a 3x3 matrix by a 1D array of


length 3 using broadcasting.
17. Stacking Arrays: Stack two 1D arrays horizontally and vertically using `[Link]()` and
`[Link]()`.

18. Transpose a Matrix: Transpose a 3x3 matrix and verify the result.

19. Fancy Indexing: Given a 2D array, use fancy indexing to extract a 2x2 submatrix from it.

20. Random Sampling: Generate a random sample of 5 elements from a given 1D array without
replacement.

Advanced Level

21. Inverse of a Matrix: Calculate the inverse of a 3x3 matrix using `[Link]()` and verify the
result by multiplying it with the original matrix.

22. Eigenvalues and Eigenvectors: Compute the eigenvalues and eigenvectors of a 3x3 matrix
using NumPy.

23. Matrix Determinant: Calculate the determinant of a 4x4 matrix using `[Link]()`.

24. Broadcasting and Vectorization: Given a 2D array, subtract the mean of each row from the
elements of that row using broadcasting.

25. SVD Decomposition: Perform Singular Value Decomposition (SVD) on a 3x3 matrix using
`[Link]()`.

26. Polynomials: Represent a polynomial using NumPy arrays and evaluate it at a given set of
points using `[Link]()`.

27. Linear Regression: Implement linear regression using NumPy to fit a line to a set of data
points.

28. Fourier Transform: Apply the Fast Fourier Transform (FFT) to a 1D signal and plot its
frequency spectrum.

29. Solving Linear Equations: Solve a system of linear equations using `[Link]()`.

30. Simulation of Random Walks: Simulate a random walk in 1D using NumPy and plot the
trajectory of the walk.

Common questions

Powered by AI

To create a 2D NumPy array, you can use the `numpy.array()` function, specifying a shape of (3, 4) to generate a 3-row, 4-column matrix. To determine its shape, use `array.shape`, which returns a tuple of dimensions. To find the total number of elements, use `array.size`, and for the number of dimensions, use `array.ndim` .

Using `np.linspace()`, you can generate an array of 50 evenly spaced values between 0 and 10, which is useful when precise control over intervals is needed. The `np.arange()` function generates values from 0 to 10 with a step size, e.g., 0.5, which is more straightforward but less flexible in terms of specifying total elements .

A 1D random walk simulation involves generating a sequence of steps, typically using a random function like `np.random.choice([-1, 1], size=n)` for steps left or right. Cumulatively summing these steps gives the trajectory. Applications include modeling stock market changes, animal foraging paths, or diffusion of particles, representing randomness in various domains .

Boolean indexing extracts elements contingent on conditions, providing a mask array of Booleans. Applying `array[mask]` yields the filtered subset. Use cases include data cleansing, feature selection in machine learning, and conditional data manipulation where only preferred data subsets are needed .

The Fourier Transform, using `np.fft.fft()`, converts a time domain signal into its frequency components, revealing periodic frequencies. FFT is a computationally efficient method to achieve this transformation. It is crucial for signal processing, enabling analysis of frequency behavior, such as identifying dominant frequency bases .

NumPy supports element-wise operations like addition, subtraction, multiplication, and division, executed directly between arrays of the same shape. These operations are inherently vectorized, allowing simultaneous processing of array elements, leading to significant computational improvements over traditional loops, highlighting NumPy's performance optimization .

Broadcasting in NumPy allows for operations between arrays of different shapes. In this case, the 1D array of length 3 is treated as a column vector and implicitly expanded to match the 3x3 matrix dimensions. This results in element-wise multiplication across each row of the matrix, achieving the desired computational efficiency without explicit looping .

Eigenvalues and eigenvectors represent fundamental properties of matrices, characterizing their transformations. In NumPy, use `np.linalg.eig()` to compute them for a 3x3 matrix. This computation is essential for understanding linear mappings, stability of systems, and in the diagonalization of matrices for simplifications .

SVD decomposes a matrix into three separate matrices (U, Σ, V*), representing orthogonal transformations and scaling factors. In NumPy, it's performed using `np.linalg.svd()`, providing insights into the matrix properties like rank, range, and null space, facilitating both dimensionality reduction and signal processing applications .

`np.linalg.solve()` efficiently handles linear systems by sidestepping the computational expense and numerical inaccuracy associated with directly computing the matrix inverse. It uses factorization techniques for stability, making it ideal for large systems where precision and performance are critical .

You might also like