NumPy Assignment– 1
NumPy Basics
Basic Questions
- Create a 1D NumPy array of integers from 1 to 10 using ‘np.array()’. Print the array and its ‘ndim’, ‘shape’, ‘dtype’, ‘itemsize’, and ‘size’.
- Use ‘np.arange()’ to generate an array of numbers from 0 to 20 with step 2. Print the array and its shape.
- Generate 10 evenly spaced numbers between 5 and 50 using ‘np.linspace()’. Print the result.
- Create a 3×3 zero matrix using ‘np.zeros((3,3))’. Print the matrix and its attributes (‘ndim’, ‘shape’).
- Create a 4×4 matrix of ones using ‘np.ones((4,4))’. Print the matrix and check its ‘dtype’.
- Create a 2×5 array filled with value 7 using ‘np.full((2,5), 7)’. Print the array.
- Generate a 3×3 identity matrix using ‘np.eye(3)’. Print it.
- Generate a 5×5 identity matrix using ‘np.identity(5)’. Print it.
- Generate an array of 5 random floats between 0 and 1 using ‘np.random.rand(5)’. Print the array.
- Create a 3×3 random array from a standard normal distribution using ‘np.random.randn(3,3)’. Print the matrix.
- Generate 10 random integers between 10 and 50 using ‘np.random.randint(10, 50, 10)’. Print the array.
- Create a 1D array of 6 elements and print its attributes (‘ndim’, ‘shape’, ‘size’).
- Generate a 2×4 array of random integers between 1 and 100. Print the array and check its ‘dtype’.
- Create an array ‘a’ with values [1, 2, 3, 4, 5] using ‘np.array()’. Create a copy using ‘a.copy()’ and modify the copy. Print both arrays to show independence.
- Create an array ‘b’ with values [10, 20, 30, 40, 50] using ‘np.array()’. Create a view using ‘b.view()’ and modify the view. Print both arrays to show dependency.
- Use ‘np.arange()’ to create an array of numbers from 100 to 120. Print its shape and size.
- Generate 15 equally spaced numbers between 0 and 1 using ‘np.linspace()’. Print the array.
- Create a 2×3 array of ones with ‘dtype=int’. Print the array and its ‘dtype’.
- Create a 3×3 zero array of float type and print its ‘itemsize’.
- Generate a 2×5 array of random integers from 0 to 9. Print the array and its shape.
Intermediate Questions
- Create a 4×4 array of random integers between 10 and 99. Print the array and each attribute: ‘ndim’, ‘shape’, ‘dtype’, ‘itemsize’, ‘size’.
- Generate an array using ‘np.arange(1, 21)’ and reshape it to (4,5). Print both the original and reshaped array.
- Generate 25 evenly spaced numbers between -5 and 5 using ‘np.linspace()’ and reshape into (5,5). Print the result.
- Create a 6×6 identity matrix and verify that all diagonal values are 1 using boolean indexing.
- Create a 3×3 array of random floats using ‘np.random.rand(3,3)’ and print the max, min, and mean.
- Generate a 5×5 array of random integers between 1 and 50. Print only its first row and last column.
- Create a 1D array using ‘np.arange(20)’. Make a copy and modify its first five elements to 100. Print both arrays.
- Create a 1D array using ‘np.arange(10, 60, 5)’. Make a view and set the last three elements to -1. Print both arrays.
- Generate a 3×3 matrix of ones. Multiply the matrix by 7 using broadcasting. Print the result.
- Create a 4×4 zero matrix. Use ‘np.fill_diagonal()’ to fill the diagonal with 9. Print the matrix.
- Generate a 5×5 random integer array from 10–99. Extract the 2×2 sub-matrix from the top-left corner.
- Create an array of 12 random integers between 1 and 100. Reshape it into (3,4). Print both the array and its transpose.
- Create an array of 50 numbers from 1 to 50 using ‘np.arange()’. Find and print even numbers using boolean indexing.
- Generate a 1D array of 10 random integers between 1 and 20. Replace all values greater than 10 with 0. Print the updated array.
- Create a 2×5 random float array using ‘np.random.randn’. Round all values to 2 decimal places using ‘np.round’. Print the array.
- Generate a 1D array of 6 random integers. Convert the array to float type using ‘astype()’ and print both arrays.
- Use ‘np.linspace()’ to create an array of 9 numbers between 1 and 100. Reshape it into (3,3) and print.
- Create a 4×4 array of random integers from 0–9. Replace all odd numbers with -1. Print the result.
- Demonstrate shallow vs deep copy: create an array, create both a ‘view’ and a ‘copy’, modify the original, and print all three.
- Generate a 2×6 random integer array. Print its flattened version using ‘ravel()’ and ‘flatten()’.
Advanced Questions
- Generate a 10×10 random integer matrix (1–100). Print its ‘ndim’, ‘shape’, ‘dtype’, ‘size’, and ‘itemsize’.
- Create a 20-element array using ‘np.arange()’. Reshape it into (4,5) and then into (10,2). Print both reshaped arrays.
- Build a 5×5 matrix of random floats. Replace all values below 0.5 with 0, otherwise keep them. Print the updated matrix.
- Generate 100 random integers between 1 and 100. Find and print the unique values using ‘np.unique()’.
- Create a 7×7 identity matrix. Change all diagonal values to random integers between 1 and 9. Print the matrix.
- Write a program to simulate 10 dice rolls using ‘np.random.randint(1, 7, 10)’. Print results and their frequencies.
- Generate an array of 20 random integers between 1 and 50. Split it into 4 equal parts using ‘np.split’. Print all sub-arrays.
- Create a 6×6 random integer matrix (1–20). Extract all elements greater than 10 into a new array. Print it.
- Demonstrate difference between copy and view on slicing: create a 1D array, slice it, modify the slice, and print both the original and slice. Then repeat with ‘copy()’.
- Generate a 3x3x3 random integer array (0–9). Print its shape, then reshape it into a 9×3 matrix.