此函数从给定数组的维度中删除一维条目,该功能需要两个参数。

numpy.squeeze(arr, axis)
Sr.No. Parameter & 描述
1

arr

输入数组

2

axis

int或int的元组。选择维度中一维条目的子集

import numpy as np  
x = np.arange(9).reshape(1,3,3) 

print 'Array X:' 
print x 
print '\n'  
y = np.squeeze(x) 

print 'Array Y:' 
print y 
print '\n'  

print 'The shapes of X and Y array:' 
print x.shape, y.shape

其输出如下-

Array X:
[[[0 1 2]
 [3 4 5]
 [6 7 8]]]

Array Y:
[[0 1 2]
 [3 4 5]
 [6 7 8]]

The shapes of X and Y array:
(1, 3, 3) (3, 3)

参考链接

https://www.learnfk.com/numpy/numpy-squeeze.html