NumPy Practical Exercise (Updated)
Objective:
Practice using NumPy to solve practical problems involving numerical computation and
array manipulation.
Instructions:
- Use only NumPy for all operations (no pure Python loops unless explicitly mentioned).
- Write clean, commented code.
- Display outputs where applicable using print().
Section 1: Array Creation & Properties
1. Create Arrays
- Create a 1D array of all even numbers between 10 and 30.
- Create a 3×3 matrix with values from 0 to 8.
- Create a 4×4 identity matrix.
2. Check Array Properties
- Print shape, size, data type, and number of dimensions of the 3×3 matrix.
Section 2: Indexing, Slicing, and Reshaping
3. Array Indexing and Slicing
- Extract the 2nd column of the 3×3 matrix.
- Get the first 2 rows using slicing.
4. Reshape an Array
- Convert the 1D even number array (from Q1) into a 2D array with 2 rows.
Section 3: Operations and Broadcasting
5. Broadcasting Operations
- Add 5 to every element of the 4×4 identity matrix.
- Multiply every row of a 3×3 array A by [1, 2, 3].
6. Element-wise Operations
- Given two arrays A = [Link]([1, 2, 3]) and B = [Link]([4, 5, 6]), compute:
- Element-wise addition
- Element-wise product
- Dot product
Section 4: Statistics and Masking
7. Statistics
- Create a random array of shape (5, 4) with values from a normal distribution.
- Compute mean and standard deviation along axis 0 and 1.
8. Boolean Masking
- From the array in Q7, extract all elements greater than the overall mean.
Section 5: Linear Algebra
9. Matrix Multiplication
- Create two compatible matrices and perform matrix multiplication.
Section 6: New Challenges
10. Sorting Arrays
- Create a 1D array of 15 random integers between 0 and 100.
- Sort the array in ascending order.
- Sort the same array in descending order.
11. Find Unique Values
- Given an array: arr = [Link]([1, 2, 2, 3, 4, 4, 4, 5]), find:
- All unique values.
- The frequency/count of each unique value.
12. Cumulative Operations
- Create a 1D array from 1 to 10.
- Compute its cumulative sum.
- Compute its cumulative product.
13. Conditional Replacement
- Create a 5×5 array of random integers between 0 and 20.
- Replace all values greater than 10 with -1.
- Replace all even numbers with 0.
Bonus Challenges
14. Simulate Dice Rolls
- Simulate rolling two 6-sided dice 1000 times using NumPy. Count how many times
the sum is 7.
15. Find Duplicates
- Generate a random array of integers between 0 and 20 (size 30). Find all duplicated
values.