- numpy切片arr[2],arr[2,:],arr[2:,:],在shape函数下的区别
import numpy as np
X = np.arange(1,10).reshape(3,3)
运行:
print(X)
>>> [[1 2 3]
[4 5 6]
[7 8 9]]
X数组为一个三行三列的二维数组
print(X[2])
>>> [7 8 9]
print(X[2].shape)
>>> (3,)
结果为一个一维的数组,数组中有三个元素
print(X[2,:])
>>> [7 8 9]
print(X[2,:].shape)
>>> (3,)
结果为一个一维数组,数组中有是三个元素
print(X[2:,:])
>>> [[7 8 9]]
print(X[2:,:].shape)
>>> (1, 3)
结果是一个一行三列的二维数组