NumPy Assignment– 3
Array Operations & Broadcasting
Basic Questions
- Create two 1D arrays [1, 2, 3] and [4, 5, 6]. Perform element-wise addition, subtraction, multiplication, and division. Print results.
- Create a 1D array of numbers 1–5. Add a scalar 10 to the array. Print the updated array.
- Multiply a 2D 3×3 matrix by a scalar 5. Print the resulting matrix.
- Demonstrate np.add and np.subtract with two arrays [10, 20, 30] and [1, 2, 3].
- Create an array [2, 4, 6, 8] and divide by scalar 2 using np.divide. Print the result.
- Use np.multiply to multiply two arrays [1,2,3] and [4,5,6]. Print the result.
- Create an array [4, 9, 16, 25]. Use np.sqrt to calculate square roots.
- Apply np.exp on array [1, 2, 3] and print the result.
- Apply np.log on array [1, np.e, np.e**2] and print the result.
- Use np.power to calculate cube of [1,2,3,4].
- Demonstrate broadcasting: add scalar 100 to a 2×3 matrix of numbers 1–6.
- Create a 1D array [1,2,3] and a column vector [[1],[2],[3]]. Add them to show broadcasting.
- Generate two arrays of shape (3,) and (3,3). Add them using broadcasting.
- Show that subtraction with broadcasting works: [1,2,3] – 2×3 matrix of ones.
- Create a 1D array [10,20,30,40]. Multiply it element-wise by [1,2,3,4].
- Demonstrate vectorization: compute squares of [1,2,3,4,5] without loops (use **2).
- Use np.add and np.power to calculate (a+b)^2 for a=[1,2,3], b=[4,5,6].
- Compare element-wise multiplication [1,2,3]*[4,5,6] with matrix multiplication using @.
- Generate an array np.arange(1,11). Add 5, multiply by 2, and divide by 3 (scalar ops).
- Apply chained operations: for array [2,4,6,8], compute ((arr + 2) * 3) – 5. Print final result.
Intermediate Questions
- Create two 3×3 arrays of random integers (1–10). Perform element-wise addition, subtraction, multiplication, and division.
- Create an array of numbers 1–5. Compute square, cube, and 4th power using np.power.
- Generate a 2×3 matrix and add a 1D array [10,20,30] to it using broadcasting. Print result.
- Create an array of 5 values. Apply np.sin, np.cos, and np.tan on it.
- Use np.exp to compute exponential growth for [0,1,2,3].
- Apply np.log10 on [1,10,100,1000]. Print results.
- Multiply a column vector (shape (3,1)) by a row vector (shape (1,3)) to create a 3×3 multiplication table.
- Demonstrate broadcasting with a 4×1 array and a 1×4 array. Add them and print the result.
- Create a 3×3 identity matrix and add a 1D array [5,5,5] using broadcasting.
- Generate two arrays a=[1,2,3] and b=[4,5,6]. Compute (a+b)*(a-b) using ufuncs.
- Show vectorization: calculate sum of squares of first 1000 numbers using np.arange and **2.
- Compare runtime of summing first 1,000,000 numbers using Python loop vs NumPy vectorization. Print both times.
- Compute element-wise modulus of two arrays [10,20,30] and [3,7,9] using np.mod.
- Create an array [1.1, 1.5, 2.8, 3.3]. Apply np.floor, np.ceil, and np.round.
- Generate a 4×4 random matrix. Add a 1D array of shape (4,) using broadcasting.
- Demonstrate np.maximum and np.minimum with two random arrays of shape (5,).
- Create an array [1,2,3,4,5]. Apply cumulative operations: np.cumsum and np.cumprod.
- Build a temperature converter: given Celsius array [0,20,37,100], convert to Fahrenheit using vectorized formula.
- Normalize an array [10,20,30,40,50] by subtracting mean and dividing by std using broadcasting.
- Compare the memory size of a Python list of 1,000,000 ints vs NumPy array of same length. Print sizes.
Advanced Questions
- Create a 1000×1000 random matrix. Add a 1D array of shape (1000,) using broadcasting. Verify result shape.
- Generate a 3D array of shape (2,3,4). Add a 1D array of shape (4,) using broadcasting. Print result.
- Create a 4×4 random integer matrix. Subtract its column means from each column using broadcasting.
- Build a 5×5 multiplication table using broadcasting with np.arange.
- 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).
- Vectorize Euclidean distance: given two 2D arrays of points A (m×2) and B (n×2), compute all pairwise distances using broadcasting.
- Generate two 1D arrays a=np.arange(1,6) and b=np.arange(10,15). Use broadcasting to compute outer sum and outer product.
- Write a program to normalize each row of a 2D array to sum 1 using broadcasting.
- Compare time taken to compute dot product of two vectors using Python loop vs NumPy vectorization for 1,000,000 elements.
- Create a 3D array (shape 5×5×5). Subtract the mean of each 2D slice using broadcasting and verify zero-mean slices.