numpy.stack和numpy.concatenate的区别
在使用numpy进行矩阵运算的时候踩到的坑,原因是不能正确区分numpy.concatenate和numpy.stack在功能上的差异。
先说numpy.concatenate,直接看文档:
numpy.concatenate((a1, a2, ...), axis=0, out=None)
Join a sequence of arrays along an existing axis.
- Parameters
-
- a1, a2, … : sequence of array_like
-
The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default).
- axis : int, optional
-
The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0.
- out : ndarray, optional
-
If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.
- Returns
-
- res : ndarray
-
The concatenated array.
重点在这一句:在一个已经存在的维度上连接数组列。可见numpy.concatenate可以同时连接好几个数组,并且不会生成新的维度: along an existing axis。示例如下:
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
[3, 4, 6]])
>>> np.concatenate((a, b), axis=None)
array([1, 2, 3, 4, 5, 6])
再说numpy.stack:
numpy.stack(arrays, axis=0, out=None)
Join a sequence of arrays along a new axis.
The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.
New in version 1.10.0.
- Parameters
-
- arrays : sequence of array_like
-
Each array must have the same shape.
- axis : int, optional
-
The axis in the result array along which the input arrays are stacked.
- out : ndarray, optional
-
If provided, the destination to place the result. The shape must be correct, matching that of what stack would have returned if no out argument were specified.
- Returns
-
- stacked : ndarray
-
The stacked array has one more dimension than the input arrays.
和concatenate不同的是,stack Joins a sequence of arrays along a new axis.也就是说stack会生成一个新的维度。而且stack适用的条件很强,数组序列必须全部有相同的shape。用例子来说明,使用最多的大概是在第0维stack:
>>> arrays = [np.random.randn(3, 4) for _ in range(10)] # arrays是一个长度为10的List,每一个元素都是(3,4)的ndarray
>>> np.stack(arrays, axis=0).shape
(10, 3, 4)
>>> np.stack(arrays, axis=1).shape
(3, 10, 4)
>>> np.stack(arrays, axis=2).shape
(3, 4, 10)
一个清晰的区别是返回的数组比输入数组多了一维。
numpy.stack和numpy.concatenate的区别的更多相关文章
- Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate()
感觉numpy.hstack()和numpy.column_stack()函数略有相似,numpy.vstack()与numpy.row_stack()函数也是挺像的. stackoverflow上也 ...
- [转]Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate()
Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate() 觉得有用的话,欢迎一起讨论相互学习~Follow Me ...
- python 中range numpy.arange 和 numpy.linspace 的区别
1.返回值不同 range返回一个range对象,numpy.arange和numpy.linspace返回一个数组. 2.np.arange的步长可以为小数,但range的步长只能是整数. 与Pyt ...
- Python的工具包[0] -> numpy科学计算 -> numpy 库及使用总结
NumPy 目录 关于 numpy numpy 库 numpy 基本操作 numpy 复制操作 numpy 计算 numpy 常用函数 1 关于numpy / About numpy NumPy系统是 ...
- numpy.random.random & numpy.ndarray.astype & numpy.arange
今天看到这样一句代码: xb = np.random.random((nb, d)).astype('float32') #创建一个二维随机数矩阵(nb行d列) xb[:, 0] += np.aran ...
- python numPy模块 与numpy里的数据类型、数据类型对象dtype
学习链接:http://www.runoob.com/numpy/numpy-tutorial.html 官方链接:https://numpy.org/devdocs/user/quickstart. ...
- python numpy.shape 和 numpy.reshape函数
导入numpy模块 from numpy import * import numpy as np ############################################### ...
- numpy中array和asarray的区别
array和asarray都可以将结构数据转化为ndarray,但是主要区别就是当数据源是ndarray时,array仍然会copy出一个副本,占用新的内存,但asarray不会. 举例说明: imp ...
- 【转】numpy中 meshgrid 和 mgrid 的区别和使用
转自:https://www.cnblogs.com/shenxiaolin/p/8854197.html 一.meshgrid函数 meshgrid函数通常使用在数据的矢量化上. 它适用于生成网格型 ...
随机推荐
- 加密通信软件Signal 2.92版本编译安装折腾手记(Ubuntu 18.04)
加密通信软件Signal 2.92版本编译安装折腾手记(Ubuntu 18.04) 前言 加密通信软件Signal是开源的,安全性很高,号称斯诺登也推荐大家使用.既然这么好,那必然会有不少人去尝试复制 ...
- Istio VirtualService 虚拟服务
概念及示例 VirtualService 描述了一个或多个用户可寻址目标到网格内实际工作负载之间的映射 . 虚拟服务让您配置如何在服务网格内将请求路由到服务,这基于 Istio 和平台提供的基本的连通 ...
- JVM调优总结(七)-调优方法
JVM调优工具 Jconsole,jProfile,VisualVM Jconsole : jdk自带,功能简单,但是可以在系统有一定负荷的情况下使用.对垃圾回收算法有很详细的跟踪.详细说明参考这里 ...
- 我的Android知识结构图——20200507停止更新,后续通过标签或分类继续完善结构图
*持续更新中.调整中(带链接的是已经总结发布的,未带链接是待发布的) *个别知识点在多个分类中都是比较重要部分,为了分类完整性 可能多出都列出了 *每一篇都是认真总结并写出来的,若哪里有问题欢迎指正 ...
- 小智的糖果(Candy) 51nod 提高组试题
luogu AC通道! (官方数据) 题目描述 小智家里来了很多的朋友,总共有N个人,站成一排,分别编号为0到N-1,小智要给他们分糖果.但 是有的朋友有一些特殊的要求,有的人要求他左右的两个人(左边 ...
- [工具-001]C++更换EXE的ICON图标
我们都知道每个可执行文件EXE都会有自己的图标,它可以在项目生成的时候进行指认,但是有时候我们会遇到两种情况:1.没有源代码,2.我们的项目很多,一个个进行更换很耗时.本人就是因为接到这么一个需求,要 ...
- JAVA自学笔记(3)
JAVA的心动之旅 Day1 字符串String 1.0 字符串的特点以及创建一个字符串 public class Practice {//构建字符串的3+1种方法 public static voi ...
- Rocket - diplomacy - NodeImp
https://mp.weixin.qq.com/s/HgUpTCh0D94Uymj5qQk-ag 介绍NodeImp相关基础类的实现. 1. 类图 节点实现(NodeImp ...
- jchdl - RTL Module
https://mp.weixin.qq.com/s/Sr4ffU4TPPoUJpdInwWd6w jchdl Module类在概念上对应Verilog的module,作为所有用户自定义模块的父 ...
- Rocket - config - DefaultConfig
https://mp.weixin.qq.com/s/zWW00D0fb8h7_TotGD9YoQ 介绍DefaultConfig类的组成. 1. DefaultConfig Defa ...