numpy concatenate數(shù)組拼接方法示例介紹
數(shù)組拼接方法一
思路:首先將數(shù)組轉(zhuǎn)成列表,然后利用列表的拼接函數(shù)append()、extend()等進(jìn)行拼接處理,最后將列表轉(zhuǎn)成數(shù)組。
示例1:
>>> import numpy as np >>> a=np.array([1,2,5]) >>> b=np.array([10,12,15]) >>> a_list=list(a) >>> b_list=list(b) >>> a_list.extend(b_list) >>> a_list [1, 2, 5, 10, 12, 15] >>> a=np.array(a_list) >>> a array([ 1, 2, 5, 10, 12, 15])
該方法只適用于簡(jiǎn)單的一維數(shù)組拼接,由于轉(zhuǎn)換過程很耗時(shí)間,對(duì)于大量數(shù)據(jù)的拼接一般不建議使用。
數(shù)組拼接方法二
思路:numpy提供了numpy.append(arr, values, axis=None)函數(shù)。對(duì)于參數(shù)規(guī)定,要么一個(gè)數(shù)組和一個(gè)數(shù)值;要么兩個(gè)數(shù)組,不能三個(gè)及以上數(shù)組直接append拼接。
示例2:
>>> a=np.arange(5)
>>> a
array([0, 1, 2, 3, 4])
>>> np.append(a,10)
array([ 0, 1, 2, 3, 4, 10])
>>> a
array([0, 1, 2, 3, 4])
>>> b=np.array([11,22,33])
>>> b
array([11, 22, 33])
>>> np.append(a,b)
array([ 0, 1, 2, 3, 4, 11, 22, 33])
>>> a
array([[1, 2, 3],
[4, 5, 6]])
>>> b=np.array([[7,8,9],[10,11,12]])
>>> b
array([[ 7, 8, 9],
[10, 11, 12]])
>>> np.append(a,b)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
numpy的數(shù)組沒有動(dòng)態(tài)改變大小的功能,numpy.append()函數(shù)每次都會(huì)重新分配整個(gè)數(shù)組,并把原來的數(shù)組復(fù)制到新數(shù)組中。
數(shù)組拼接方法三
思路:numpy提供了numpy.concatenate((a1,a2,...), axis=0)函數(shù)。能夠一次完成多個(gè)數(shù)組的拼接。其中a1,a2,...是數(shù)組類型的參數(shù)
示例3:
>>> a=np.array([1,2,3])
>>> b=np.array([11,22,33])
>>> c=np.array([44,55,66])
>>> np.concatenate((a,b,c),axis=0) # 默認(rèn)情況下,axis=0可以不寫
array([ 1, 2, 3, 11, 22, 33, 44, 55, 66]) #對(duì)于一維數(shù)組拼接,axis的值不影響最后的結(jié)果
>>> a=np.array([[1,2,3],[4,5,6]])
>>> b=np.array([[11,21,31],[7,8,9]])
>>> np.concatenate((a,b),axis=0)
array([[ 1, 2, 3],
[ 4, 5, 6],
[11, 21, 31],
[ 7, 8, 9]])
>>> np.concatenate((a,b),axis=1) #axis=1表示對(duì)應(yīng)行的數(shù)組進(jìn)行拼接
array([[ 1, 2, 3, 11, 21, 31],
[ 4, 5, 6, 7, 8, 9]])
對(duì)numpy.append()和numpy.concatenate()兩個(gè)函數(shù)的運(yùn)行時(shí)間進(jìn)行比較
示例4:
>>> from time import clock as now >>> a=np.arange(9999) >>> b=np.arange(9999) >>> time1=now() >>> c=np.append(a,b) >>> time2=now() >>> print time2-time1 28.2316728446 >>> a=np.arange(9999) >>> b=np.arange(9999) >>> time1=now() >>> c=np.concatenate((a,b),axis=0) >>> time2=now() >>> print time2-time1 20.3934997107
可知,concatenate()效率更高,適合大規(guī)模的數(shù)據(jù)拼接
PS:更多示例
import numpy as np
a = np.array([[1, 2], [3, 4]])
a.shape
Out[3]: (2, 2)
b = np.array([[5, 6]])
b.shape
Out[5]: (1, 2)
np.concatenate((a, b))
Out[6]:
array([[1, 2],
[3, 4],
[5, 6]])
c= np.concatenate((a, b))
c.shape
Out[8]: (3, 2)
d = np.concatenate((a, b), axis=0)
d.shape
Out[10]: (3, 2)
e = np.concatenate((a, b), axis=1)
Traceback (most recent call last):
File "<ipython-input-11-05a280a2cb02>", line 1, in <module>
e = np.concatenate((a, b), axis=1)
ValueError: all the input array dimensions except for the concatenation axis must match exactly
e = np.concatenate((a, b.T), axis=1)
e.shape
Out[13]: (2, 3)
import numpy as np
a = np.array([[1, 2], [3, 4]])
a.shape
Out[3]: (2, 2)
b = np.array([[5, 6]])
b.shape
Out[5]: (1, 2)
np.concatenate((a, b))
Out[6]:
array([[1, 2],
[3, 4],
[5, 6]])
c= np.concatenate((a, b))
c.shape
Out[8]: (3, 2)
d = np.concatenate((a, b), axis=0)
d.shape
Out[10]: (3, 2)
e = np.concatenate((a, b), axis=1)
Traceback (most recent call last):
File "<ipython-input-11-05a280a2cb02>", line 1, in <module>
e = np.concatenate((a, b), axis=1)
ValueError: all the input array dimensions except for the concatenation axis must match exactly
e = np.concatenate((a, b.T), axis=1)
e.shape
Out[13]: (2, 3)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pyqt4教程之實(shí)現(xiàn)半透明的天氣預(yù)報(bào)界面示例
這篇文章主要介紹了pyqt4實(shí)現(xiàn)半透明的天氣預(yù)報(bào)界面示例,需要的朋友可以參考下2014-03-03
教你怎么用python實(shí)現(xiàn)字符串轉(zhuǎn)日期
今天教各位小伙伴怎么用python實(shí)現(xiàn)字符串轉(zhuǎn)日期,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴很有幫助,需要的朋友可以參考下2021-05-05
對(duì)Python 窗體(tkinter)樹狀數(shù)據(jù)(Treeview)詳解
今天小編就為大家分享一篇對(duì)Python 窗體(tkinter)樹狀數(shù)據(jù)(Treeview)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
數(shù)據(jù)驅(qū)動(dòng)測(cè)試DDT之Selenium讀取Excel文件
這篇文章主要為大家介紹了數(shù)據(jù)驅(qū)動(dòng)測(cè)試DDT之Selenium讀取Excel文件,2021-11-11
利用Python腳本生成sitemap.xml的實(shí)現(xiàn)方法
最近項(xiàng)目中需要用腳本生成sitemap,中間學(xué)習(xí)了一下sitemap的格式和lxml庫(kù)的用法。把結(jié)果記錄一下,方便以后需要直接拿來用。下面這篇文章主要介紹了利用Python腳本生成sitemap.xml的實(shí)現(xiàn)方法,需要的朋友可以參考借鑒,一起來看看吧。2017-01-01
Python3實(shí)現(xiàn)爬取簡(jiǎn)書首頁(yè)文章標(biāo)題和文章鏈接的方法【測(cè)試可用】
這篇文章主要介紹了Python3實(shí)現(xiàn)爬取簡(jiǎn)書首頁(yè)文章標(biāo)題和文章鏈接的方法,結(jié)合實(shí)例形式分析了Python3基于urllib及bs4庫(kù)針對(duì)簡(jiǎn)書網(wǎng)進(jìn)行文章抓取相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
python fuzzywuzzy模塊模糊字符串匹配詳細(xì)用法
這篇文章主要介紹了使用Python完成公司名稱和地址的模糊匹配的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python?Pywinauto輕松實(shí)現(xiàn)Windows桌面自動(dòng)化詳解
pywinauto這個(gè)Python庫(kù)可以讓你像魔法師一樣操控Windows?GUI,輕松模擬鼠標(biāo)鍵盤操作,自動(dòng)化Notepad,Excel甚至企業(yè)級(jí)軟件,下面小編就來和大家詳細(xì)介紹一下它的使用吧2025-05-05
python中的Numpy二維數(shù)組遍歷與二維數(shù)組切片后遍歷效率比較
這篇文章主要介紹了python中的Numpy二維數(shù)組遍歷與二維數(shù)組切片后遍歷效率比較,在python-numpy使用中,可以用雙層?for循環(huán)對(duì)數(shù)組元素進(jìn)行訪問,也可以切片成每一行后進(jìn)行一維數(shù)組的遍歷,下面小編擊來舉例介紹吧,需要的朋友可以參考一下2022-03-03

