- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章numpy concatenate数组拼接方法示例介绍由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
数组拼接方法一 。
思路:首先将数组转成列表,然后利用列表的拼接函数append()、extend()等进行拼接处理,最后将列表转成数组.
示例1:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>>
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
>>> 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
>>> 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
>>>
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:更多示例 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
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
)
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://blog.csdn.net/qq_38150441/article/details/80488800 。
最后此篇关于numpy concatenate数组拼接方法示例介绍的文章就讲到这里了,如果你想了解更多关于numpy concatenate数组拼接方法示例介绍的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在尝试创建一个包含 int[][] 项的数组 即 int version0Indexes[][4] = { {1,2,3,4}, {5,6,7,8} }; int version1Indexes[
我有一个整数数组: private int array[]; 如果我还有一个名为 add 的方法,那么以下有什么区别: public void add(int value) { array[va
当您尝试在 JavaScript 中将一个数组添加到另一个数组时,它会将其转换为一个字符串。通常,当以另一种语言执行此操作时,列表会合并。 JavaScript [1, 2] + [3, 4] = "
根据我正在阅读的教程,如果您想创建一个包含 5 列和 3 行的表格来表示这样的数据... 45 4 34 99 56 3 23 99 43 2 1 1 0 43 67 ...它说你可以使用下
我通常使用 python 编写脚本/程序,但最近开始使用 JavaScript 进行编程,并且在使用数组时遇到了一些问题。 在 python 中,当我创建一个数组并使用 for x in y 时,我得
我有一个这样的数组: temp = [ 'data1', ['data1_a','data1_b'], ['data2_a','data2_b','data2_c'] ]; // 我想使用 toStr
rent_property (table name) id fullName propertyName 1 A House Name1 2 B
这个问题在这里已经有了答案: 关闭13年前。 Possible Duplicate: In C arrays why is this true? a[5] == 5[a] array[index] 和
使用 Excel 2013。经过多年的寻找和适应,我的第一篇文章。 我正在尝试将当前 App 用户(即“John Smith”)与他的电子邮件地址“jsmith@work.com”进行匹配。 使用两个
当仅在一个边距上操作时,apply 似乎不会重新组装 3D 数组。考虑: arr 1),但对我来说仍然很奇怪,如果一个函数返回一个具有尺寸的对象,那么它们基本上会被忽略。 最佳答案 这是一个不太理
我有一个包含 GPS 坐标的 MySQL 数据库。这是我检索坐标的部分 PHP 代码; $sql = "SELECT lat, lon FROM gps_data"; $stmt=$db->query
我需要找到一种方法来执行这个操作,我有一个形状数组 [批量大小, 150, 1] 代表 batch_size 整数序列,每个序列有 150 个元素长,但在每个序列中都有很多添加的零,以使所有序列具有相
我必须通过 url 中的 json 获取文本。 层次结构如下: 对象>数组>对象>数组>对象。 我想用这段代码获取文本。但是我收到错误 :org.json.JSONException: No valu
enter code here- (void)viewDidLoad { NSMutableArray *imageViewArray= [[NSMutableArray alloc] init];
知道如何对二维字符串数组执行修剪操作,例如使用 Java 流 API 进行 3x3 并将其收集回相同维度的 3x3 数组? 重点是避免使用显式的 for 循环。 当前的解决方案只是简单地执行一个 fo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有来自 ASP.NET Web 服务的以下 XML 输出: 1710 1711 1712 1713
如果我有一个对象todo作为您状态的一部分,并且该对象包含数组列表,则列表内部有对象,在这些对象内部还有另一个数组listItems。如何更新数组 listItems 中 id 为“poi098”的对
我想将最大长度为 8 的 bool 数组打包成一个字节,通过网络发送它,然后将其解压回 bool 数组。已经在这里尝试了一些解决方案,但没有用。我正在使用单声道。 我制作了 BitArray,然后尝试
我们的数据库中有这个字段指示一周中的每一天的真/假标志,如下所示:'1111110' 我需要将此值转换为 boolean 数组。 为此,我编写了以下代码: char[] freqs = weekday
我是一名优秀的程序员,十分优秀!