您好,登錄后才能下訂單哦!
這篇文章主要介紹了numpy中怎么使用squeeze函數(shù),具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
reshape函數(shù):改變數(shù)組的維數(shù)(注意不是shape大?。?/p>
>>> 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 函數(shù):從數(shù)組的形狀中刪除單維度條目,即把shape中為1的維度去掉
用法:numpy.squeeze(a,axis = None)
1)a表示輸入的數(shù)組;
2)axis用于指定需要刪除的維度,但是指定的維度必須為單維度,否則將會報錯;
3)axis的取值可為None 或 int 或 tuple of ints, 可選。若axis為空,則刪除所有單維度的條目;
4)返回值:數(shù)組
5) 不會修改原數(shù)組;
>>> 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])
體現(xiàn)在畫圖時
>>> 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()函數(shù)轉(zhuǎn)換后,要顯示的數(shù)組變成了秩為1的數(shù)組,即(10,)
感謝你能夠認真閱讀完這篇文章,希望小編分享的“numpy中怎么使用squeeze函數(shù)”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。