Reshape NumPy Array
Problem Statement
Reshape a 1D NumPy array into a 2D array with specified dimensions.
Background
Reshaping is crucial for preparing data for machine learning models. The reshape() method changes array dimensions without changing data.
Your Task
Write a function reshape_array(arr, new_shape) that:
- Reshapes the input array to new_shape
- Returns a dictionary with:
- "original_shape": Original shape as list
- "new_shape": New shape as list
- "array": Reshaped array as nested list
Output Format
Return a dictionary with exactly these three keys.
Example
arr = np.arange(6), new_shape = (2, 3)
{'original_shape': [6], 'new_shape': [2, 3], 'array': [[0, 1, 2], [3, 4, 5]]}- Input:
arris a 1D array of the six integers , so its shape has a single dimension of length .new_shapeasks for rows and columns. - Compatibility: a reshape only rearranges how the same values are grouped, so the element count must match: .
- Filling order: values are laid out row by row. Row takes the first values ; row takes the next values . In general, the value at row , column is the original element at position (e.g. row , column is position ).
- Output:
original_shapeis[6],new_shapeis[2, 3], andarrayis[[0, 1, 2], [3, 4, 5]]: one inner list per row, each of length . - Notice that the 1D shape is still a one-element list
[6], not the bare number , and reading the rows of the result left to right gives back to in the original order.
Constraints
- Use numpy reshape() method
- new_shape will be compatible with array size
- Return shapes as lists