NumPy Assignment– 1

NumPy Basics

Basic Questions

  1. 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’.
  2. Use ‘np.arange()’ to generate an array of numbers from 0 to 20 with step 2. Print the array and its shape.
  3. Generate 10 evenly spaced numbers between 5 and 50 using ‘np.linspace()’. Print the result.
  4. Create a 3×3 zero matrix using ‘np.zeros((3,3))’. Print the matrix and its attributes (‘ndim’, ‘shape’).
  5. Create a 4×4 matrix of ones using ‘np.ones((4,4))’. Print the matrix and check its ‘dtype’.
  6. Create a 2×5 array filled with value 7 using ‘np.full((2,5), 7)’. Print the array.
  7. Generate a 3×3 identity matrix using ‘np.eye(3)’. Print it.
  8. Generate a 5×5 identity matrix using ‘np.identity(5)’. Print it.
  9. Generate an array of 5 random floats between 0 and 1 using ‘np.random.rand(5)’. Print the array.
  10. Create a 3×3 random array from a standard normal distribution using ‘np.random.randn(3,3)’. Print the matrix.
  11. Generate 10 random integers between 10 and 50 using ‘np.random.randint(10, 50, 10)’. Print the array.
  12. Create a 1D array of 6 elements and print its attributes (‘ndim’, ‘shape’, ‘size’).
  13. Generate a 2×4 array of random integers between 1 and 100. Print the array and check its ‘dtype’.
  14. 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.
  15. 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.
  16. Use ‘np.arange()’ to create an array of numbers from 100 to 120. Print its shape and size.
  17. Generate 15 equally spaced numbers between 0 and 1 using ‘np.linspace()’. Print the array.
  18. Create a 2×3 array of ones with ‘dtype=int’. Print the array and its ‘dtype’.
  19. Create a 3×3 zero array of float type and print its ‘itemsize’.
  20. Generate a 2×5 array of random integers from 0 to 9. Print the array and its shape.

Intermediate Questions

  1. Create a 4×4 array of random integers between 10 and 99. Print the array and each attribute: ‘ndim’, ‘shape’, ‘dtype’, ‘itemsize’, ‘size’.
  2. Generate an array using ‘np.arange(1, 21)’ and reshape it to (4,5). Print both the original and reshaped array.
  3. Generate 25 evenly spaced numbers between -5 and 5 using ‘np.linspace()’ and reshape into (5,5). Print the result.
  4. Create a 6×6 identity matrix and verify that all diagonal values are 1 using boolean indexing.
  5. Create a 3×3 array of random floats using ‘np.random.rand(3,3)’ and print the max, min, and mean.
  6. Generate a 5×5 array of random integers between 1 and 50. Print only its first row and last column.
  7. Create a 1D array using ‘np.arange(20)’. Make a copy and modify its first five elements to 100. Print both arrays.
  8. Create a 1D array using ‘np.arange(10, 60, 5)’. Make a view and set the last three elements to -1. Print both arrays.
  9. Generate a 3×3 matrix of ones. Multiply the matrix by 7 using broadcasting. Print the result.
  10. Create a 4×4 zero matrix. Use ‘np.fill_diagonal()’ to fill the diagonal with 9. Print the matrix.
  11. Generate a 5×5 random integer array from 10–99. Extract the 2×2 sub-matrix from the top-left corner.
  12. Create an array of 12 random integers between 1 and 100. Reshape it into (3,4). Print both the array and its transpose.
  13. Create an array of 50 numbers from 1 to 50 using ‘np.arange()’. Find and print even numbers using boolean indexing.
  14. Generate a 1D array of 10 random integers between 1 and 20. Replace all values greater than 10 with 0. Print the updated array.
  15. Create a 2×5 random float array using ‘np.random.randn’. Round all values to 2 decimal places using ‘np.round’. Print the array.
  16. Generate a 1D array of 6 random integers. Convert the array to float type using ‘astype()’ and print both arrays.
  17. Use ‘np.linspace()’ to create an array of 9 numbers between 1 and 100. Reshape it into (3,3) and print.
  18. Create a 4×4 array of random integers from 0–9. Replace all odd numbers with -1. Print the result.
  19. Demonstrate shallow vs deep copy: create an array, create both a ‘view’ and a ‘copy’, modify the original, and print all three.
  20. Generate a 2×6 random integer array. Print its flattened version using ‘ravel()’ and ‘flatten()’.

Advanced Questions

  1. Generate a 10×10 random integer matrix (1–100). Print its ‘ndim’, ‘shape’, ‘dtype’, ‘size’, and ‘itemsize’.
  2. Create a 20-element array using ‘np.arange()’. Reshape it into (4,5) and then into (10,2). Print both reshaped arrays.
  3. Build a 5×5 matrix of random floats. Replace all values below 0.5 with 0, otherwise keep them. Print the updated matrix.
  4. Generate 100 random integers between 1 and 100. Find and print the unique values using ‘np.unique()’.
  5. Create a 7×7 identity matrix. Change all diagonal values to random integers between 1 and 9. Print the matrix.
  6. Write a program to simulate 10 dice rolls using ‘np.random.randint(1, 7, 10)’. Print results and their frequencies.
  7. Generate an array of 20 random integers between 1 and 50. Split it into 4 equal parts using ‘np.split’. Print all sub-arrays.
  8. Create a 6×6 random integer matrix (1–20). Extract all elements greater than 10 into a new array. Print it.
  9. 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()’.
  10. Generate a 3x3x3 random integer array (0–9). Print its shape, then reshape it into a 9×3 matrix.