NumPy Assignment– 7
Advanced NumPy Concepts
Basic Questions
- Create a structured array with fields ‘name’ (string), ‘age’ (int), and ‘marks’ (float) for 3 students; print the array and access only the ‘name’ field.
- Create a record array with fields ‘id’ and ‘score’; print the first row and access ‘score’ column using attribute access.
- Create a masked array from [10, 20, -999, 30] where -999 is treated as missing; print the masked array and its filled version with 0.
- Apply a mask to an array [1, 2, 3, 4, 5] where values greater than 3 are hidden; print the masked and unmasked values.
- Create an array [5,3,8,6,7]; sort it with np.sort() and also print the indices returned by np.argsort().
- Sort a 2D array row-wise and column-wise using np.sort() with axis argument.
- Create an array [10,20,30,40,50]; use np.where() to find indices of elements greater than 25.
- Use np.nonzero() to find indices of non-zero elements from [0,1,0,2,3,0].
- Create a sorted array [10,20,30,40,50]; use np.searchsorted() to find where 35 and 10 should be inserted.
- Create an array [1,2,3,2,1,4,5]; find unique values with np.unique().
- Find the union of [1,2,3] and [3,4,5] using np.union1d().
- Find the intersection of [1,2,3,4] and [3,4,5,6] using np.intersect1d().
- Find the difference of [1,2,3,4] and [3,4,5,6] using np.setdiff1d().
- Create a 3×3 array with values 1–9 in row-major order and print its flattened version with order=’C’.
- Print the same array flattened with order=’F’ (column-major); compare outputs.
- Create two arrays of shape (3,1) and (1,3); add them using broadcasting and print the 3×3 result.
- Demonstrate broadcasting by subtracting the mean of each column from a 3×3 array.
- Optimize a row sum: compute sums of a 2D array by broadcasting instead of using a Python loop.
- Show memory size difference of a Python list vs NumPy array of 1 million integers using __sizeof__() and .nbytes.
- Verify contiguity of an array by printing .flags[‘C_CONTIGUOUS’] and .flags[‘F_CONTIGUOUS’].
Intermediate Questions
- Create a structured array with employees’ ‘id’, ‘name’, and ‘salary’; sort the array by ‘salary’.
- Create a record array with ‘x’ and ‘y’ coordinates; compute Euclidean distance for each record.
- Create a masked array with random integers 1–20, masking all even numbers; print unmasked odd values.
- Generate a masked array from a 4×4 random array by masking values greater than 50.
- Sort a 2D random integer array along axis 0 and axis 1 separately; compare results.
- Use np.argsort() on [40,10,20,30] to get sorting indices and use them to manually rearrange the array.
- Generate 20 random integers (1–100). Use np.where() to find all indices divisible by 7.
- Use np.searchsorted() to insert multiple values [15,55,75] into [10,20,30,40,50,60]; print the insertion indices.
- Use np.nonzero() to get positions of all odd numbers in an array [2,4,6,7,9,12,15].
- Create two arrays with duplicates. Use np.union1d() and np.intersect1d() to get unique union and intersection.
- Use np.setdiff1d() to find students present in class A [1,2,3,4] but not in class B [3,4,5].
- Generate a (3,3) array and flatten it using both ‘C’ and ‘F’ order. Compare memory layouts.
- Reshape a (4,6) array into (6,4) using order=’F’. Print both arrays to show layout differences.
- Compute row-wise mean of a (5,5) matrix using broadcasting instead of a loop.
- Normalize each column of a (4,4) random array by broadcasting division with column max.
- Use broadcasting to compute pairwise absolute differences of array [1,2,3,4] with itself.
- Create a (1000,1000) random array. Compare performance of np.sum() vs manual Python loops.
- Compare memory and speed difference between a C-ordered and F-ordered (1000,1000) array when transposed.
- Demonstrate how np.argsort() and np.take_along_axis() can be combined to sort rows of a 2D array.
- Implement z-score normalization of a (10,5) random dataset using broadcasting.
Advanced Questions
- Create a structured array for products (‘id’, ‘name’, ‘price’, ‘stock’). Sort by ‘price’ and then filter with stock < 50.
- Build a record array for students with fields ‘roll’, ‘marks1’, ‘marks2’, ‘marks3’; compute total marks and sort by it.
- Create a masked array for rainfall data [0,20,0,35,0,50] where 0 means missing; compute mean rainfall ignoring masked values.
- Create a 5×5 random matrix. Mask diagonal elements and compute mean of remaining values.
- Implement quicksort manually using np.argsort() on a random array of size 15.
- From a sorted array [10,20,30,40,50], insert values [5,25,60] at correct positions using np.searchsorted() and np.insert().
- Perform set operations on large arrays (size 1000) to find unique, intersection, and difference; measure performance.
- Compare memory layout performance: time arr.sum(axis=0) on (2000,2000) C-ordered vs F-ordered arrays.
- Optimize computation of all pairwise distances between 100 2D points using broadcasting instead of loops.
- Demonstrate advanced broadcasting trick: center a (1000,5) dataset by subtracting column means and then scale by column std—all in one line without loops.