numpy中实现矩阵的切片不丢失维度的几种方法
在输出numpy矩阵的一维切片的时候,经常会遇到一种情况是把维度为1的那个维度直接丢了,就像执行了np.squeeze语句一样。例如:
import numpy as np
a = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
print('a.shape = ',a.shape)
b = a[:,0]
print('b.shape = ',b.shape)
c = np.array([[1],[4],[7]])
print('c.shape = ',c.shape)
print('np.squeeze(c).shape = ',np.squeeze(c).shape)
# output:
# a.shape = (3, 3)
# b.shape