📘
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:
Input:
arr = np.arange(6), new_shape = (2, 3)
Output:
{'original_shape': [6], 'new_shape': [2, 3], 'array': [[0, 1, 2], [3, 4, 5]]}Reasoning:
6 elements reshaped to 2 rows x 3 columns
Constraints:
- Use numpy reshape() method
- new_shape will be compatible with array size
- Return shapes as lists
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.