欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

numpy concatenate數(shù)組拼接方法示例介紹

 更新時(shí)間:2019年05月27日 10:19:36   作者:會(huì)飛的小罐子  
這篇文章主要介紹了numpy concatenate數(shù)組拼接方法示例介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

數(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)換過(guò)程很耗時(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ù)組沒(méi)有動(dòng)態(tài)改變大小的功能,numpy.append()函數(shù)每次都會(huì)重新分配整個(gè)數(shù)組,并把原來(lái)的數(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可以不寫(xiě)
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)文章

  • 如何使用Python在2秒內(nèi)評(píng)估國(guó)際象棋位置詳解

    如何使用Python在2秒內(nèi)評(píng)估國(guó)際象棋位置詳解

    關(guān)心編程語(yǔ)言的使用趨勢(shì)的人都知道,最近幾年,國(guó)內(nèi)最火的兩種語(yǔ)言非Python與Go莫屬,下面這篇文章主要給大家介紹了關(guān)于如何使用Python在2秒內(nèi)評(píng)估國(guó)際象棋位置的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • python微信跳一跳系列之棋子定位像素遍歷

    python微信跳一跳系列之棋子定位像素遍歷

    這篇文章主要為大家詳細(xì)介紹了python微信跳一跳系列之棋子定位之像素遍歷,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • 你真的了解Python的random模塊嗎?

    你真的了解Python的random模塊嗎?

    這篇文章主要介紹了Python的random模塊的相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Python操作PostgreSql數(shù)據(jù)庫(kù)的方法(基本的增刪改查)

    Python操作PostgreSql數(shù)據(jù)庫(kù)的方法(基本的增刪改查)

    這篇文章主要介紹了Python操作PostgreSql數(shù)據(jù)庫(kù)(基本的增刪改查),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • python 爬取華為應(yīng)用市場(chǎng)評(píng)論

    python 爬取華為應(yīng)用市場(chǎng)評(píng)論

    項(xiàng)目需要爬取評(píng)論數(shù)據(jù),在此做一個(gè)記錄,這里爬取的是web端的數(shù)據(jù),以后可能會(huì)考慮爬取android app中的數(shù)據(jù)。
    2021-05-05
  • 在Python中處理XML的教程

    在Python中處理XML的教程

    這篇文章主要介紹了在Python中處理XML的教程,是Python網(wǎng)絡(luò)編程中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-04-04
  • Python Flask前后端Ajax交互的方法示例

    Python Flask前后端Ajax交互的方法示例

    這篇文章主要介紹了Python Flask前后端Ajax交互的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • python統(tǒng)計(jì)函數(shù)庫(kù)scipy.stats的用法解析

    python統(tǒng)計(jì)函數(shù)庫(kù)scipy.stats的用法解析

    今天小編就為大家分享一篇python統(tǒng)計(jì)函數(shù)庫(kù)scipy.stats的用法解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • 初探TensorFLow從文件讀取圖片的四種方式

    初探TensorFLow從文件讀取圖片的四種方式

    本篇文章主要介紹了初探TensorFLow從文件讀取圖片的四種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • Python進(jìn)階語(yǔ)法之類的繼承

    Python進(jìn)階語(yǔ)法之類的繼承

    這篇文章主要為大家介紹了Python類的繼承,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12

最新評(píng)論