NumPy Assignment– 5
Reshaping & Manipulating Arrays
Basic Questions
- Create a 1D array using ‘np.arange(12)’ and reshape it to shape ‘(3, 4)’ using ‘reshape()’; print both the original shape and the new shape.
- From the ‘(3, 4)’ array, produce a 1D view using ‘ravel()’ and a 1D copy using ‘flatten()’; modify the first element of the ‘ravel()’ result to prove view semantics, then show that the ‘flatten()’ result is independent.
- Create a ‘(2, 3)’ array and print its transpose using ‘transpose()’ and ‘.T’; verify shapes before and after.
- Make a ‘(2, 3, 4)’ array and swap axes 0 and 2 using ‘swapaxes(0, 2)’; print the resulting shape.
- Using the same ‘(2, 3, 4)’ array, move axis 0 to the end using ‘moveaxis(0, -1)’; print shapes of source and result.
- Create two arrays ‘a = np.arange(6).reshape(2, 3)’ and ‘b = np.arange(6, 12).reshape(2, 3)’; concatenate them along axis 0 with ‘np.concatenate’ and print.
- Concatenate ‘a’ and ‘b’ along axis 1; print the result and its shape.
- Vertically stack two ‘(2, 3)’ arrays using ‘np.vstack([a, b])’ and print the output shape.
- Horizontally stack two ‘(2, 3)’ arrays using ‘np.hstack([a, b])’ and print the output shape.
- Create a ‘(4, 4)’ array and split it into two equal subarrays along axis 1 using ‘np.hsplit()’; print both parts.
- Split the same ‘(4, 4)’ array into two equal subarrays along axis 0 using ‘np.vsplit()’; print both parts.
- Use ‘np.split()’ to divide ‘np.arange(10)’ into five equal chunks; print each chunk.
- Use ‘np.array_split()’ to divide ‘np.arange(10)’ into three nearly equal chunks; print chunk sizes.
- Stack three 1D arrays ‘x, y, z’ (each length 4) into a single 2D array using ‘np.stack([x, y, z], axis=0)’; print shape and content.
- Stack the same ‘x, y, z’ along a new last axis using ‘np.stack([x, y, z], axis=-1)’; print shape and show one element access.
- Create two ‘(2, 2)’ arrays and stack them depth-wise with ‘np.dstack([A, B])’; print shape ‘(2, 2, 2)’.
- Verify that ‘ravel()’ returns a view for a C-contiguous array by checking that changing the parent reflects in the raveled output without recreating it.
- Show that ‘flatten()’ always returns a copy by modifying the flattened result and confirming the source is unchanged.
- Reshape ‘np.arange(24)’ into ‘(2, 3, 4)’ using ‘reshape()’ and print slices that demonstrate how elements are laid out across axes.
- Start with a ‘(3, 4)’ array and reshape it to ‘(4, 3)’ using ‘reshape()’ with ‘-1’ for one dimension; print the inferred size.
Intermediate Questions
- Create ‘np.arange(24)’ and reshape to ‘(4, 6)’; then transpose to ‘(6, 4)’; finally ravel to 1D; print shapes at each step.
- Build a ‘(3, 4, 5)’ array and perform ‘swapaxes(1, 2)’ then ‘moveaxis(0, -1)’; print intermediate shapes to explain axis movement.
- Show the effect of ‘order=”F”‘ versus default in ‘reshape()’ by reshaping the same 1D array to ‘(3, 4)’ in both orders and printing the first column to compare.
- Concatenate three ‘(2, 3)’ arrays along axis 0 using ‘np.concatenate’; then achieve the same result with chained ‘np.vstack’; verify equality.
- Create a ‘(5,)’ array and split into ‘[[head], [middle…], [tail]]’ with ‘np.array_split’ at indices ‘[1, 4]’; print each part.
- Form a grid by ‘np.hstack’ of two ‘(2, 2)’ blocks to make ‘(2, 4)’, then ‘np.vstack’ two copies to make ‘(4, 4)’; print the final grid.
- Use ‘np.stack’ to create a 3D tensor from three ‘(3, 3)’ matrices along a new axis 0; then index the middle slice and print it.
- Create a ‘(2, 3, 4)’ tensor; produce a view with ‘transpose(1, 0, 2)’; modify one element and show the change appears in the original (view semantics).
- Use ‘np.dstack’ to combine three grayscale ‘(3, 3)’ arrays into a pseudo-RGB ‘(3, 3, 3)’ volume; print shape and a sample pixel vector.
- Show that ‘np.hsplit’ on a ‘(3, 6)’ array with ‘indices_or_sections=3’ returns three ‘(3, 2)’ blocks; print shapes to confirm.
- Demonstrate uneven split: use ‘np.array_split’ on a ‘(2, 7)’ array into 3 parts; print shapes and contents to show near-even distribution.
- Build a checkerboard-style tiling by horizontally stacking two distinct ‘(2, 2)’ patterns and vertically stacking the result with itself; print the final ‘(4, 4)’.
- Create a ‘(4, 5)’ array; move the last axis to the front with ‘moveaxis(-1, 0)’ to get ‘(5, 4)’; explain the transformation by printing element coordinates before/after.
- Starting with ‘np.arange(60).reshape(3, 4, 5)’, flatten only the last two axes into shape ‘(3, 20)’ using ‘reshape’ and verify by checking element continuity with ‘ravel()’.
- Given three 1D arrays of different lengths, pad the shorter ones using ‘np.concatenate’ with zeros to match the longest, then ‘np.stack’ them into a 2D array; print before/after shapes.
- Create a ‘(6, 4)’ array; split into top ‘(2, 4)’, middle ‘(2, 4)’, bottom ‘(2, 4)’ via ‘np.vsplit’; modify the middle block and reassemble with ‘np.vstack’; print the result.
- For a ‘(2, 3, 4)’ array, produce three different views: ‘(3, 2, 4)’ via ‘swapaxes’, ‘(4, 3, 2)’ via ‘transpose’, and ‘(2, 4, 3)’ via ‘moveaxis’; verify shapes.
- Use ‘reshape(-1, 2)’ to convert a 1D array of length 14 into 2-column rows; show what happens when the length is not divisible by 2 by catching the exception and then fixing it with trimming.
- Demonstrate creating column and row vectors from a 1D array using ‘reshape(-1, 1)’ and ‘reshape(1, -1)’; then form an outer sum with ‘np.concatenate’ or ‘np.hstack/np.vstack’ to visualize.
- Build a simple “band” matrix by stacking: start with a zero ‘(5, 5)’, create three diagonals as 1D arrays, reshape/stack them to place on main and adjacent diagonals using ‘np.concatenate’ and ‘transpose’; print the final matrix.
Advanced Questions
- Construct a tiled mosaic: given a base ‘(2, 3)’ pattern ‘P’, create a ‘(6, 9)’ mosaic by repeating ‘P’ using combinations of ‘np.hstack’ and ‘np.vstack’ (no ‘np.tile’); verify shape and patterns.
- Given a time-series matrix ‘(T, F)’ (T steps × F features), reshape it into overlapping windows of length ‘L’ to shape ‘(T-L+1, L, F)’ using ‘reshape’, ‘stride-like slicing’ via ‘ravel/reshape’ logic, and stacking with ‘np.stack’; print one sample window to confirm (stay within allowed APIs).
- For a 4D tensor ‘(B, C, H, W)’, move channels to last to obtain ‘(B, H, W, C)’ using ‘moveaxis’; then flatten spatial dims to shape ‘(B, H*W, C)’ using ‘reshape’; print shapes along the path.
- Slice a ‘(6, 6)’ array into four quadrants with ‘np.vsplit’ and ‘np.hsplit’, swap the top-right and bottom-left quadrants with ‘np.hstack/np.vstack’, and reassemble; print before/after.
- Implement depth stacking: build three ‘(4, 4)’ layers and combine them into a ‘(4, 4, 3)’ volume with ‘np.dstack’; then split back into layers using ‘np.dsplit’ equivalent via slicing and ‘np.stack’; verify equality.
- Normalize columns of a ‘(m, n)’ array to unit length: compute column norms (shape ‘(n,)’), reshape/broadcast to ‘(1, n)’, then divide; do this purely with ‘reshape’ and concatenation/stacking tools; print column norms after.
- Given ‘np.arange(48).reshape(3, 4, 4)’, reorder axes to ‘(4, 3, 4)’ using a combination of ‘transpose’ and ‘swapaxes’; confirm that values align by checking a few coordinates.
- Create a 3D “RGB stripe” cube of shape ‘(8, 8, 3)’ by stacking three ‘(8, 8)’ channels: red stripes across rows, green across columns, blue zeros; build channels via reshaping and concatenate with ‘np.dstack’; print the top-left 3×3×3 block.
- Emulate block matrix assembly: given four ‘(3, 3)’ blocks ‘A, B, C, D’, assemble a ‘(6, 6)’ matrix using ‘np.hstack’ and ‘np.vstack’; then extract the blocks back with ‘np.vsplit’ and ‘np.hsplit’ and verify each equals the original.
- From a 1D array length 36, create a sliding window matrix of size ‘(n_windows, 5)’ where windows are contiguous segments of length 5 (no loops): use ‘np.concatenate’ and clever ‘reshape’ to stage overlapping views, finally ‘np.stack’ selected slices; print the first and last window to verify.