Num py - printing arrays
When printing arrays, NumPy follows a specific layout:
- The last axis is printed from left to right.
- The second-to-last axis is printed from top to bottom.
- The rest of the axes are also printed from top to bottom, with each slice separated by an empty line.
Here are examples demonstrating how arrays are printed based on their dimensions:
One-dimensional array (1D):
a = np.arange(6) print(a) # Output: # [0 1 2 3 4 5]
Two-dimensional array (2D):
b = np.arange(12).reshape(4, 3)
print(b)
# Output:
# [[ 0 1 2]
# [ 3 4 5]
# [ 6 7 8]
# [ 9 10 11]]
Three-dimensional array (3D):
c = np.arange(24).reshape(2, 3, 4)
print(c)
# Output:
# [[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
#
# [[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]]
If an array is too large to be printed, NumPy will automatically skip the central part and only print the corners. You can change this behavior using
set_printoptions to force NumPy to print the entire arraynp.set_printoptions(threshold=sys.maxsize) # Import sys module first
This command disables the truncation of large arrays when printing.
Comments
Post a Comment