numpy concatenate数组拼接方法示例介绍

yipeiwu_com5年前Python基础

数组拼接方法一

思路:首先将数组转成列表,然后利用列表的拼接函数append()、extend()等进行拼接处理,最后将列表转成数组。

示例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])

该方法只适用于简单的一维数组拼接,由于转换过程很耗时间,对于大量数据的拼接一般不建议使用。 

数组拼接方法二

思路:numpy提供了numpy.append(arr, values, axis=None)函数。对于参数规定,要么一个数组和一个数值;要么两个数组,不能三个及以上数组直接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的数组没有动态改变大小的功能,numpy.append()函数每次都会重新分配整个数组,并把原来的数组复制到新数组中。

数组拼接方法三

思路:numpy提供了numpy.concatenate((a1,a2,...), axis=0)函数。能够一次完成多个数组的拼接。其中a1,a2,...是数组类型的参数

示例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) # 默认情况下,axis=0可以不写
array([ 1, 2, 3, 11, 22, 33, 44, 55, 66]) #对于一维数组拼接,axis的值不影响最后的结果

 

>>> 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表示对应行的数组进行拼接
array([[ 1, 2, 3, 11, 21, 31],
    [ 4, 5, 6, 7, 8, 9]])

对numpy.append()和numpy.concatenate()两个函数的运行时间进行比较

示例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()效率更高,适合大规模的数据拼接

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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python3.7.0的安装步骤

python3.7.0的安装步骤

如何安装Python的操作步骤: 1.第一步先去python的官方网站下载python的安装包 地址: https://www.python.org/downloads/ 根据自己的系...

windows 下python+numpy安装实用教程

如题,今天兜兜转转找了很多网站帖子,一个个环节击破,最后装好费了不少时间。 希望这个帖子能帮助有需要的人,教你一篇帖子搞定python+numpy,节约科研时间。 水平有限,难免存在不足...

Python图像处理之gif动态图的解析与合成操作详解

Python图像处理之gif动态图的解析与合成操作详解

本文实例讲述了Python图像处理之gif动态图的解析与合成操作。分享给大家供大家参考,具体如下: gif动态图是在现在已经司空见惯,朋友圈里也经常是一言不合就斗图。这里,就介绍下如何使...

pytorch AvgPool2d函数使用详解

我就废话不多说了,直接上代码吧! import torch import torch.nn as nn import torch.nn.functional as F from to...

python计算圆周长、面积、球体体积并画出圆

python计算圆周长、面积、球体体积并画出圆

输入半径,计算圆的周长、面积、球体体积,并画出这个圆。拖动条、输入框和图像控件的数据保持一致! Fedora下测试通过 复制代码 代码如下:#https://github.com/Rob...