[Python Cookbook] Numpy Array Joint Methods: Append, Extend & Concatenate
数组拼接方法一
思路:首先将数组转成列表,然后利用列表的拼接函数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拼接。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()效率更高,适合大规模的数据拼接
数组拼接方法四
Use vstack to stack arrays in sequence vertically (row wise).
p = np.ones([2, 3], int)
np.vstack([p, 2*p])
Output:
array([[1, 1, 1],
[1, 1, 1],
[2, 2, 2],
[2, 2, 2]])
Use hstack to stack arrays in sequence horizontally (column wise).
np.hstack([p, 2*p])
Output:
array([[1, 1, 1, 2, 2, 2],
[1, 1, 1, 2, 2, 2]])
-----------------------------------------------
作者:故乡月zyl
来源:CSDN
原文:https://blog.csdn.net/zyl1042635242/article/details/43162031
[Python Cookbook] Numpy Array Joint Methods: Append, Extend & Concatenate的更多相关文章
- [Python Cookbook] Numpy Array Slicing and Indexing
1-D Array Indexing Use bracket notation [ ] to get the value at a specific index. Remember that inde ...
- [Python Cookbook] Numpy Array Manipulation
1. Reshape: The np.reshape() method will give a new shape to an array without changing its data. Not ...
- Python 将numpy array由浮点型转换为整型
Python 将numpy array由浮点型转换为整型 ——使用numpy中的astype()方法可以实现,如:
- [Python Cookbook] Numpy: Multiple Ways to Create an Array
Convert from list Apply np.array() method to convert a list to a numpy array: import numpy as np myl ...
- python的numpy.array
为什么要用numpy Python中提供了list容器,可以当作数组使用.但列表中的元素可以是任何对象,因此列表中保存的是对象的指针,这样一来,为了保存一个简单的列表[1,2,3].就需要三个指针和三 ...
- 【python】numpy array特殊数据统一处理
array中的某些数据坏掉,想要统一处理,找到了这个方法,做个笔记. 比如,把数组中所有小于0的数字置为0 import numpy as np t = np.array([-2, -1, 0, 1, ...
- python 中 numpy array 中的维度
简介 numpy 创建的数组都有一个shape属性,它是一个元组,返回各个维度的维数.有时候我们可能需要知道某一维的特定维数. 二维情况 >>> import numpy as np ...
- [Python Cookbook] Numpy: Iterating Over Arrays
1. Using for-loop Iterate along row axis: import numpy as np x=np.array([[1,2,3],[4,5,6]]) for i in ...
- [Python Cookbook] Numpy: How to Apply a Function to 1D Slices along the Given Axis
Here is a function in Numpy module which could apply a function to 1D slices along the Given Axis. I ...
随机推荐
- destoon 配置文件config.inc.php参数说明
$CFG['db_host']数据库服务器,可以包括端口号,一般为localhost $CFG['db_user']数据库用户名,一般为root $CFG['db_pass']数据库密码 $CFG[' ...
- phpstudy2016+phpstorm2017-3+xdebug+chrome
1. 勾选Xdebug 后 phpstudy 会自动重启服务 [XDebug] xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug" ...
- Confluence 导出为 PDF 格式 - 导出多个页面或者整个空间
使用 Confluence 的空间导出功能,你可以将多个页面或者整个 Confluence 站点转换为 PDF 文件. 希望使用空间导出功能,你需要 导出空间(Export Space)权限.请查看 ...
- Ubuntu系统里的python
Ubuntu系统里,默认安装python2.7.x版本的python,直接执行python命令,打开的将是python 2.7.x版本:python3版本的需要自行安装,安装成功后,执行python3 ...
- 创建Django项目并将其部署在腾讯云上
这段时间在做scrapy爬虫,对爬出来的数据基于Django做了统计与可视化,本想部署在腾讯云上玩玩,但是因为以前没有经验遇到了一些问题,在这里记录一下: 首先说下Django的创建与配置: 1. 创 ...
- Ubuntu 15 下 Qt 配置mysql链接及基本操作
序 最近需要在Linux下做一个unix网络编程项目,选择了Ubuntu 最新版本15.04 : 开发环境:Qt 5 数据库: MySQL 安装Qt 和 MySQL 简要介绍一下软件的安装! 安装Qt ...
- Python中的属性访问与描述符
Python中的属性访问与描述符 请给作者点赞--> 原文链接 在Python中,对于一个对象的属性访问,我们一般采用的是点(.)属性运算符进行操作.例如,有一个类实例对象foo,它有一个nam ...
- noip2017行记
前奏 写了所有的变量名在linux下测,结果发现并没什么用...听说将所有的变量加上下划线,加上自己的名字作为前缀.. lgj,“感觉今年有网络流,今年要立个flag”,zjr“你咋不上天儿” 在车上 ...
- HDU 5111 Alexandra and Two Trees 树链剖分 + 主席树
题意: 给出两棵树,每棵树的节点都有一个权值. 同一棵树上的节点的权值互不相同,不同树上节点的权值可以相同. 要求回答如下询问: \(u_1 \, v_1 \, u_2 \, v_2\):询问第一棵树 ...
- python 模块相互import
模块A中import B,而在模块B中import A.这时会怎么样呢?这个在Python列表中由RobertChen给出了详细解释,抄录如下: [A.py] from B import D clas ...