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读文件逐行处理的示例代码分享
Dec 27 Python
Python连接DB2数据库
Aug 27 Python
正确理解python中的关键字“with”与上下文管理器
Apr 21 Python
Python实现查看系统启动项功能示例
May 10 Python
django小技巧之html模板中调用对象属性或对象的方法
Nov 30 Python
python3实现zabbix告警推送钉钉的示例
Feb 20 Python
python自动保存百度盘资源到百度盘中的实例代码
Aug 26 Python
详解pandas获取Dataframe元素值的几种方法
Jun 14 Python
Python使用jpype模块调用jar包过程解析
Jul 29 Python
Python3利用scapy局域网实现自动多线程arp扫描功能
Jan 21 Python
python实现高效的遗传算法
Apr 07 Python
Python学习之os包使用教程详解
Mar 21 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
索尼ICF-SW100收音机评测
2021/03/02 无线电
php抽奖小程序的实现代码
2013/06/18 PHP
解析MySql与Java的时间类型
2013/06/22 PHP
解析PHP跳出循环的方法以及continue、break、exit的区别介绍
2013/07/01 PHP
PHP中spl_autoload_register函数的用法总结
2013/11/07 PHP
php中cookie实现二级域名可访问操作的方法
2014/11/11 PHP
JavaScript Archive Network 集合
2007/05/12 Javascript
JavaScript 提升运行速度之循环篇 译文
2009/08/15 Javascript
jquery实现简单的拖拽效果实例兼容所有主流浏览器(优化篇)
2013/06/28 Javascript
用Js实现的动态增加表格示例自己写的
2013/10/21 Javascript
完美解决AJAX跨域问题
2013/11/01 Javascript
JavaScript中Number.MAX_VALUE属性的使用方法
2015/06/04 Javascript
JavaScript实现算术平方根算法-代码超简单
2015/09/11 Javascript
jQuery扩展_动力节点Java学院整理
2017/07/05 jQuery
浅谈Vue SSR 的 Cookies 问题
2017/11/20 Javascript
JS设计模式之状态模式概念与用法分析
2018/02/05 Javascript
解决小程序无法触发SESSION问题
2020/02/03 Javascript
js 数据类型判断的方法
2020/12/03 Javascript
理解Python中的类与实例
2015/04/27 Python
python 定时修改数据库的示例代码
2018/04/08 Python
Python比较2个时间大小的实现方法
2018/04/10 Python
Django框架使用富文本编辑器Uedit的方法分析
2018/07/31 Python
python命令行工具Click快速掌握
2019/07/04 Python
python3 sorted 如何实现自定义排序标准
2020/03/12 Python
python自动化办公操作PPT的实现
2021/02/05 Python
python爬虫scrapy基于CrawlSpider类的全站数据爬取示例解析
2021/02/20 Python
澳大利亚领先的在线药房:Pharmacy Online(有中文站)
2020/02/22 全球购物
Cecil Mode法国在线商店:女性时尚
2021/01/08 全球购物
SOA的常见陷阱或者误解是什么
2014/10/05 面试题
物业管理大学生个人的自我评价
2013/10/10 职场文书
实习协议书范本
2014/04/22 职场文书
个人综合鉴定材料
2014/05/23 职场文书
计算机软件专业求职信
2014/06/10 职场文书
农村党建工作汇报材料
2014/10/27 职场文书
结婚通知短信大全
2015/04/17 职场文书
2015年依法行政工作总结
2015/04/29 职场文书