37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
import numpy as np
|
|
"""
|
|
This script demonstrates how to reshape NumPy arrays into different dimensions.
|
|
Functionality:
|
|
1. Creates a 1D NumPy array with values from 0 to 11.
|
|
2. Reshapes the 1D array into a 3x4 2D array and prints it.
|
|
3. Reshapes the same 1D array into a 2x2x3 3D array and prints it.
|
|
4. Creates another 1D NumPy array with values from 0 to 4.
|
|
5. Reshapes this array into a 2D column vector (shape: 5x1) and prints it.
|
|
Usage:
|
|
- Demonstrates the use of `np.arange` for array creation.
|
|
- Shows how to use the `reshape` method for changing array dimensions.
|
|
"""
|
|
|
|
# Creating a 1D array from 0 to 11 inclusive
|
|
array = np.arange(12)
|
|
# Reshaping the array to a 3x4 2D array (matrix)
|
|
reshaped_array_2d = array.reshape(3, 4)
|
|
print(reshaped_array_2d)
|
|
# Reshaping the array to a 2x2x3 3D array
|
|
reshaped_array_3d = array.reshape(2, 2, 3)
|
|
print(reshaped_array_3d)
|
|
|
|
|
|
import numpy as np
|
|
# Creating a 1D array from 0 to 4 inclusive
|
|
array = np.arange(5)
|
|
|
|
# Reshaping the array to a 2D array with one column
|
|
reshaped_array = array.reshape(-1, 1)
|
|
print(reshaped_array)
|
|
|
|
array = np.arange(18)
|
|
|
|
# Reshaping the array to a 2D array with three columns
|
|
reshaped_array = array.reshape(-1, 3)
|
|
print(reshaped_array) |