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用于指定需要?jiǎng)h除的維度,但是指定的維度必須為單維度,否則將會(huì)報(bào)錯(cuò);
3)axis的取值可為None 或 int 或 tuple of ints, 可選。若axis為空,則刪除所有單維度的條目;
4)返回值:數(shù)組
5) 不會(huì)修改原數(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)在畫圖時(shí)
>>> 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,)
通過(guò)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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 數(shù)據(jù)類(dataclass)的具體使用
本文主要介紹了python 數(shù)據(jù)類(dataclass)的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Python序列的推導(dǎo)式實(shí)現(xiàn)代碼
推導(dǎo)式是可以從一個(gè)數(shù)據(jù)序列構(gòu)建另一個(gè)新的數(shù)據(jù)序列(的一種結(jié)構(gòu)體),是python的一種獨(dú)有特性,在python中共有三種推導(dǎo),列表推導(dǎo)式和字典推導(dǎo)式,集合推導(dǎo)式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-07-07盤點(diǎn)總結(jié)Python爬蟲(chóng)常用庫(kù)(附官方文檔)
在信息時(shí)代,數(shù)據(jù)是無(wú)處不在的寶藏,從網(wǎng)頁(yè)內(nèi)容、社交媒體帖子到在線商店的產(chǎn)品信息,互聯(lián)網(wǎng)上存在著大量的數(shù)據(jù)等待被收集和分析,Python爬蟲(chóng)是一種強(qiáng)大的工具,用于從互聯(lián)網(wǎng)上獲取和提取數(shù)據(jù)2023-11-11一文帶你掌握Python自然語(yǔ)言處理庫(kù)SpaCy
SpaCy是一個(gè)非常強(qiáng)大的Python自然語(yǔ)言處理庫(kù),它包含了眾多強(qiáng)大功能,如詞性標(biāo)注、命名實(shí)體識(shí)別、依賴關(guān)系解析等等,這篇文章的目標(biāo)是幫助你了解SpaCy的基本功能和如何使用,需要的朋友可以參考下2023-07-07django實(shí)現(xiàn)web接口 python3模擬Post請(qǐng)求方式
今天小編就為大家分享一篇django實(shí)現(xiàn)web接口 python3模擬Post請(qǐng)求方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11使用Python實(shí)現(xiàn)博客上進(jìn)行自動(dòng)翻頁(yè)
這篇文章主要介紹了使用Python實(shí)現(xiàn)博客上進(jìn)行自動(dòng)翻頁(yè),需要的朋友可以參考下2017-08-08