arr = np.array([1, 2, 3])
print(arr.shape) # Output: (3,)
2D Array: A two-dimensional array, like a matrix or table.
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape) # Output: (2, 3)
3D Array: A three-dimensional array, like a collection of matrices.
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr.shape) # Output: (2, 2, 2)
Importance of Array Shape:
- Data Manipulation
- Compatibility
- Performance
Reshaping Arrays :
You can change the shape of an array using the reshape()
function, as long as the total number of elements remains the same.
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped_arr = arr.reshape(2, 3)
print(reshaped_arr)
# Output:
# [[1 2 3]
# [4 5 6]]
Understanding the shape of NumPy arrays is fundamental for anyone working with numerical data in Python, providing a foundation for more advanced data processing and analysis.