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,)
參考:http://blog.csdn.net/zenghaitao0128/article/details/78512715
到此這篇關(guān)于numpy的squeeze函數(shù)使用方法的文章就介紹到這了,更多相關(guān)numpy squeeze內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 數(shù)據(jù)類(dataclass)的具體使用
本文主要介紹了python 數(shù)據(jù)類(dataclass)的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03django實現(xiàn)web接口 python3模擬Post請求方式
今天小編就為大家分享一篇django實現(xiàn)web接口 python3模擬Post請求方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11