NumPy Assignment– 3

Array Operations & Broadcasting

Basic Questions

  1. Create two 1D arrays [1, 2, 3] and [4, 5, 6]. Perform element-wise addition, subtraction, multiplication, and division. Print results.
  2. Create a 1D array of numbers 1–5. Add a scalar 10 to the array. Print the updated array.
  3. Multiply a 2D 3×3 matrix by a scalar 5. Print the resulting matrix.
  4. Demonstrate np.add and np.subtract with two arrays [10, 20, 30] and [1, 2, 3].
  5. Create an array [2, 4, 6, 8] and divide by scalar 2 using np.divide. Print the result.
  6. Use np.multiply to multiply two arrays [1,2,3] and [4,5,6]. Print the result.
  7. Create an array [4, 9, 16, 25]. Use np.sqrt to calculate square roots.
  8. Apply np.exp on array [1, 2, 3] and print the result.
  9. Apply np.log on array [1, np.e, np.e**2] and print the result.
  10. Use np.power to calculate cube of [1,2,3,4].
  11. Demonstrate broadcasting: add scalar 100 to a 2×3 matrix of numbers 1–6.
  12. Create a 1D array [1,2,3] and a column vector [[1],[2],[3]]. Add them to show broadcasting.
  13. Generate two arrays of shape (3,) and (3,3). Add them using broadcasting.
  14. Show that subtraction with broadcasting works: [1,2,3] – 2×3 matrix of ones.
  15. Create a 1D array [10,20,30,40]. Multiply it element-wise by [1,2,3,4].
  16. Demonstrate vectorization: compute squares of [1,2,3,4,5] without loops (use **2).
  17. Use np.add and np.power to calculate (a+b)^2 for a=[1,2,3], b=[4,5,6].
  18. Compare element-wise multiplication [1,2,3]*[4,5,6] with matrix multiplication using @.
  19. Generate an array np.arange(1,11). Add 5, multiply by 2, and divide by 3 (scalar ops).
  20. Apply chained operations: for array [2,4,6,8], compute ((arr + 2) * 3) – 5. Print final result.

Intermediate Questions

  1. Create two 3×3 arrays of random integers (1–10). Perform element-wise addition, subtraction, multiplication, and division.
  2. Create an array of numbers 1–5. Compute square, cube, and 4th power using np.power.
  3. Generate a 2×3 matrix and add a 1D array [10,20,30] to it using broadcasting. Print result.
  4. Create an array of 5 values. Apply np.sin, np.cos, and np.tan on it.
  5. Use np.exp to compute exponential growth for [0,1,2,3].
  6. Apply np.log10 on [1,10,100,1000]. Print results.
  7. Multiply a column vector (shape (3,1)) by a row vector (shape (1,3)) to create a 3×3 multiplication table.
  8. Demonstrate broadcasting with a 4×1 array and a 1×4 array. Add them and print the result.
  9. Create a 3×3 identity matrix and add a 1D array [5,5,5] using broadcasting.
  10. Generate two arrays a=[1,2,3] and b=[4,5,6]. Compute (a+b)*(a-b) using ufuncs.
  11. Show vectorization: calculate sum of squares of first 1000 numbers using np.arange and **2.
  12. Compare runtime of summing first 1,000,000 numbers using Python loop vs NumPy vectorization. Print both times.
  13. Compute element-wise modulus of two arrays [10,20,30] and [3,7,9] using np.mod.
  14. Create an array [1.1, 1.5, 2.8, 3.3]. Apply np.floor, np.ceil, and np.round.
  15. Generate a 4×4 random matrix. Add a 1D array of shape (4,) using broadcasting.
  16. Demonstrate np.maximum and np.minimum with two random arrays of shape (5,).
  17. Create an array [1,2,3,4,5]. Apply cumulative operations: np.cumsum and np.cumprod.
  18. Build a temperature converter: given Celsius array [0,20,37,100], convert to Fahrenheit using vectorized formula.
  19. Normalize an array [10,20,30,40,50] by subtracting mean and dividing by std using broadcasting.
  20. Compare the memory size of a Python list of 1,000,000 ints vs NumPy array of same length. Print sizes.

Advanced Questions

  1. Create a 1000×1000 random matrix. Add a 1D array of shape (1000,) using broadcasting. Verify result shape.
  2. Generate a 3D array of shape (2,3,4). Add a 1D array of shape (4,) using broadcasting. Print result.
  3. Create a 4×4 random integer matrix. Subtract its column means from each column using broadcasting.
  4. Build a 5×5 multiplication table using broadcasting with np.arange.
  5. Implement sigmoid function 1/(1+np.exp(-x)) for array x=np.linspace(-10,10,100). Plot the result (if plotting allowed, else print first 10 values).
  6. Vectorize Euclidean distance: given two 2D arrays of points A (m×2) and B (n×2), compute all pairwise distances using broadcasting.
  7. Generate two 1D arrays a=np.arange(1,6) and b=np.arange(10,15). Use broadcasting to compute outer sum and outer product.
  8. Write a program to normalize each row of a 2D array to sum 1 using broadcasting.
  9. Compare time taken to compute dot product of two vectors using Python loop vs NumPy vectorization for 1,000,000 elements.
  10. Create a 3D array (shape 5×5×5). Subtract the mean of each 2D slice using broadcasting and verify zero-mean slices.