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


Posted in Python onMay 27, 2019

数组拼接方法一

思路:首先将数组转成列表,然后利用列表的拼接函数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 相关文章推荐
Python version 2.7 required, which was not found in the registry
Aug 26 Python
python处理Excel xlrd的简单使用
Sep 12 Python
python select.select模块通信全过程解析
Sep 20 Python
python基于twisted框架编写简单聊天室
Jan 02 Python
python实现自动网页截图并裁剪图片
Jul 30 Python
Python代码使用 Pyftpdlib实现FTP服务器功能
Jul 22 Python
Django model 中设置联合约束和联合索引的方法
Aug 06 Python
python实现删除列表中某个元素的3种方法
Jan 15 Python
Python常用库大全及简要说明
Jan 17 Python
pyinstaller 3.6版本通过pip安装失败的解决办法(推荐)
Jan 18 Python
解决pytorch-yolov3 train 报错的问题
Feb 18 Python
python中的socket实现ftp客户端和服务器收发文件及md5加密文件
Apr 01 Python
详解Numpy中的数组拼接、合并操作(concatenate, append, stack, hstack, vstack, r_, c_等)
May 27 #Python
python安装numpy和pandas的方法步骤
May 27 #Python
numpy库与pandas库axis=0,axis= 1轴的用法详解
May 27 #Python
Python之NumPy(axis=0 与axis=1)区分详解
May 27 #Python
Python3.7 新特性之dataclass装饰器
May 27 #Python
Python3多目标赋值及共享引用注意事项
May 27 #Python
Python中字符串String的基本内置函数与过滤字符模块函数的基本用法
May 27 #Python
You might like
百度地图经纬度转换到腾讯地图/Google 对应的经纬度
2015/08/28 PHP
thinkphp下MySQL数据库读写分离代码剖析
2017/04/18 PHP
Yii2框架可逆加密简单实现方法
2017/08/25 PHP
php 中phar包的使用教程详解
2018/10/26 PHP
tp5修改(实现即点即改)
2019/10/18 PHP
jquery api参考 visualjquery 中国线路 速度快
2007/11/30 Javascript
nodejs入门详解(多篇文章结合)
2012/03/07 NodeJs
javascript正则表达式定义(语法)总结
2016/01/08 Javascript
基于RequireJS和JQuery的模块化编程日常问题解析
2016/04/14 Javascript
javascript实现的猜数小游戏完整实例代码
2016/05/10 Javascript
微信小程序 倒计时组件实现代码
2016/10/24 Javascript
详解js中常规日期格式处理、月历渲染和倒计时函数
2016/12/28 Javascript
JavaScript严格模式详解
2017/01/16 Javascript
js实现下拉菜单效果
2017/03/01 Javascript
详解原生js实现offset方法
2017/06/15 Javascript
vue+axios+element ui 实现全局loading加载示例
2018/09/11 Javascript
利用layer实现表单完美验证的方法
2019/09/26 Javascript
Python 可爱的大小写
2008/09/06 Python
Python操作RabbitMQ服务器实现消息队列的路由功能
2016/06/29 Python
Python中进程和线程的区别详解
2017/10/29 Python
基于python中的TCP及UDP(详解)
2017/11/06 Python
python中numpy的矩阵、多维数组的用法
2018/02/05 Python
Python Web框架之Django框架文件上传功能详解
2019/08/16 Python
Python PIL库图片灰化处理
2020/04/07 Python
Python QTimer实现多线程及QSS应用过程解析
2020/07/11 Python
Python通过yagmail实现发送邮件代码解析
2020/10/27 Python
MCM英国官网:奢侈皮具制品
2017/04/18 全球购物
英国男士时尚购物网站:Stuarts London
2017/10/22 全球购物
斯凯奇新西兰官网:SKECHERS新西兰
2018/02/22 全球购物
保洁主管岗位职责
2013/11/20 职场文书
教育学专业毕业生的自我评价
2013/11/21 职场文书
财务总监管理岗位职责
2014/03/08 职场文书
文明工地标语
2014/06/16 职场文书
研讨会通知
2015/04/27 职场文书
Golang二维数组的使用方式
2021/05/28 Golang
《辉夜大小姐想让我告白》第三季正式预告
2022/03/20 日漫