numpy的squeeze函数blog.csdn/zenghaitao0128/article/details/78512715
reshape函数:改变数组的维数(注意不是shape⼤⼩)
1. >>> e= np.arange(10)
numpy库不具有的功能有2. >>> e
3. array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> e.reshape(1,1,10)
array([[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]])
1. >>> e.reshape(1,1,10)
2. array([[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]])
3. >>> e.reshape(1,10,1)
4. array([[[0],
5.        [1],
6.        [2],
7.        [3],
8.        [4],
9.        [5],
10.        [6],
11.        [7],
12.        [8],
13.        [9]]])
squeeze 函数:从数组的形状中删除单维度条⽬,即把shape中为1的维度去掉
⽤法:numpy.squeeze(a,axis = None)
1.  1)a表⽰输⼊的数组;
2.  2)axis⽤于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;
3.  3)axis的取值可为None 或 int 或 tuple of ints, 可选。若axis为空,则删除所有单维度的条⽬;
4.  4)返回值:数组
5.  5) 不会修改原数组;
1. >>> a = e.reshape(1,1,10)
2. >>> a
3. array([[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]])
4. >>> np.squeeze(a)
5. array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
体现在画图时
1. >>> plt.plot(a)
2. Traceback (most recent call last):
3.  File "<stdin>", line 1, in <module>
4.  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 3240, in plot
5.    ret = ax.plot(*args, **kwargs)
6.  File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 1710, in inner
7.    return func(ax, *args, **kwargs)
8.  File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 1437, in plot
9.    for line in self._get_lines(*args, **kwargs):
10.  File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 404, in _grab_next_args
11.    for seg in self._plot_args(this, kwargs):
12.  File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 384, in _plot_args
13.    x, y = self._xy_from_xy(x, y)
14.  File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 246, in _xy_from_xy
15.    "shapes {} and {}".format(x.shape, y.shape))
16. ValueError: x and y can be no greater than 2-D, but have shapes (1L,) and (1L, 1L, 10L)
17. >>> plt.plot(np.squeeze(a))
18. [<matplotlib.lines.Line2D object at 0x00000000146CD940>]
19. >>> plt.show()
1. >>> np.squeeze(a).shape
2. (10L,)
通过np.squeeze()函数转换后,要显⽰的数组变成了秩为1的数组,即(10,)参考:blog.csdn/zenghaitao0128/article/details/78512715