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 "<stdin>", line 1, in <module> File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 3240, in plot ret = ax.plot(*args, **kwargs) File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 1710, in inner return func(ax, *args, **kwargs) File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 1437, in plot for line in self._get_lines(*args, **kwargs): File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 404, in _grab_next_args for seg in self._plot_args(this, kwargs): File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 384, in _plot_args x, 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函數使用方法的文章就介紹到這瞭,更多相關numpy squeeze內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- 詳解Numpy擴充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法
- 手把手教你用Matplotlib實現數據可視化
- 詳解Python中matplotlib模塊的繪圖方式
- Python Matplotlib初階使用入門教程
- Python機器學習三大件之一numpy