numpy的squeeze函数
生活随笔
收集整理的這篇文章主要介紹了
numpy的squeeze函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://blog.csdn.net/zenghaitao0128/article/details/78512715
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 "<stdin>", line 1, in <module>File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 3240, in plotret = ax.plot(*args, **kwargs)File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 1710, in innerreturn func(ax, *args, **kwargs)File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 1437, in plotfor line in self._get_lines(*args, **kwargs):File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 404, in _grab_next_argsfor seg in self._plot_args(this, kwargs):File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 384, in _plot_argsx, y = self._xy_from_xy(x, y)File "C:\Python27\lib\site-packages\matplotlib\axes\_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)) [<matplotlib.lines.Line2D object at 0x00000000146CD940>] >>> plt.show()
>>> np.squeeze(a).shape (10L,)
通過np.squeeze()函數轉換后,要顯示的數組變成了秩為1的數組,即(10,)
參考:http://blog.csdn.net/zenghaitao0128/article/details/78512715
總結
以上是生活随笔為你收集整理的numpy的squeeze函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 提高抗打击能力_如何提高心理承受能力或者
- 下一篇: FreeRTOS应用——消息队列