本篇文章小编给大家分享一下numpy的squeeze函数使用方法代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
reshape函数:改变数组的维数(注意不是shape大小)
>>> e= np.arange(10) >>> e 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]]]) >>> e.reshape(1,1,10) array([[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]]) >>> e.reshape(1,10,1) array([[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]])
squeeze 函数:从数组的形状中删除单维度条目,即把shape中为1的维度去掉
用法:numpy.squeeze(a,axis = None)
1)a表示输入的数组;
2)axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;
3)axis的取值可为None 或 int 或 tuple of ints, 可选。若axis为空,则删除所有单维度的条目;
4)返回值:数组
5) 不会修改原数组;
>>> a = e.reshape(1,1,10) >>> a array([[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]]) >>> np.squeeze(a) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
体现在画图时
>>> plt.plot(a) Traceback (most recent call last): File "", line 1, in File "C:Python27libsite-packagesmatplotlibpyplot.py", line 3240, in plot ret = ax.plot(*args, **kwargs) File "C:Python27libsite-packagesmatplotlib__init__.py", line 1710, in inner return func(ax, *args, **kwargs) File "C:Python27libsite-packagesmatplotlibaxes_axes.py", line 1437, in plot for line in self._get_lines(*args, **kwargs): File "C:Python27libsite-packagesmatplotlibaxes_base.py", line 404, in _grab_next_args for seg in self._plot_args(this, kwargs): File "C:Python27libsite-packagesmatplotlibaxes_base.py", line 384, in _plot_args x, y = self._xy_from_xy(x, y) File "C:Python27libsite-packagesmatplotlibaxes_base.py", line 246, in _xy_from_xy "shapes {} and {}".format(x.shape, y.shape)) ValueError: x and y can be no greater than 2-D, but have shapes (1L,) and (1L, 1L, 10L) >>> plt.plot(np.squeeze(a)) [ ] >>> plt.show()
>>> np.squeeze(a).shape (10L,)
通过np.squeeze()函数转换后,要显示的数组变成了秩为1的数组,即(10,)
庇护所的日子 安卓版v15.0.5
庇护所的日子是一款像素画风的冒险游戏,场景制作细腻,玩家们需
梦想城镇华为版 安卓最新版v12.1.0
梦想城镇华为版是一款玩法非常丰富的模拟经营类手游,玩家将在一
胡椒大厨去广告 最新版v1.4.134
胡椒大厨去广告是一款非常好玩的模拟经营类游戏,玩家在这里可以
谁是首富红包版 安卓版v1.0.59
谁是首富红包版是游戏的特殊版本,为游戏加入了掉红包的功能,为
梦想超市旧版本 安卓版v3.2.5
梦想超市旧版本,这是一款玩法独特的模拟经营手游,由合肥乐堂动