21 August 2020
Code
# Import library
import numpy as np
# Create an array
x = np.array([[5,2,0],
[1,7,6],
[3,9,4]])
print(x)
[[5 2 0]
[1 7 6]
[3 9 4]]
.
Sort along axis=1 (default):
Caution ! Sorting changes positions of the array by axis. So the adjacent elements may shift.
# Sort
y = x.copy()
y.sort()
print('Sorted array (default)(axis=1):\n',y)
print(type(y))
print('Size:',np.size(y))
Sorted array (default)(axis=1):
[[0 2 5]
[1 6 7]
[3 4 9]]
<class 'numpy.ndarray'>
Size: 9
.
Sort along axis=0
# Sort
y = x.copy()
y.sort(axis=0)
print('Sorted array (axis=0):\n',y)
print(type(y))
print('Size:',np.size(y))
Sorted array (axis=0):
[[1 2 0]
[3 7 4]
[5 9 6]]
<class 'numpy.ndarray'>
Size: 9
.
Any errors in code above?
Please send a message.