在使用numpy进行矩阵运算的时候踩到的坑,原因是不能正确区分numpy.concatenate和numpy.stack在功能上的差异。

  先说numpy.concatenate,直接看文档:

numpy.concatenate((a1a2...)axis=0out=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(arraysaxis=0out=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的区别的更多相关文章

  1. Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate()

    感觉numpy.hstack()和numpy.column_stack()函数略有相似,numpy.vstack()与numpy.row_stack()函数也是挺像的. stackoverflow上也 ...

  2. [转]Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate()

    Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate() 觉得有用的话,欢迎一起讨论相互学习~Follow Me ...

  3. python 中range numpy.arange 和 numpy.linspace 的区别

    1.返回值不同 range返回一个range对象,numpy.arange和numpy.linspace返回一个数组. 2.np.arange的步长可以为小数,但range的步长只能是整数. 与Pyt ...

  4. Python的工具包[0] -> numpy科学计算 -> numpy 库及使用总结

    NumPy 目录 关于 numpy numpy 库 numpy 基本操作 numpy 复制操作 numpy 计算 numpy 常用函数 1 关于numpy / About numpy NumPy系统是 ...

  5. numpy.random.random & numpy.ndarray.astype & numpy.arange

    今天看到这样一句代码: xb = np.random.random((nb, d)).astype('float32') #创建一个二维随机数矩阵(nb行d列) xb[:, 0] += np.aran ...

  6. python numPy模块 与numpy里的数据类型、数据类型对象dtype

    学习链接:http://www.runoob.com/numpy/numpy-tutorial.html 官方链接:https://numpy.org/devdocs/user/quickstart. ...

  7. python numpy.shape 和 numpy.reshape函数

    导入numpy模块   from numpy import *   import numpy as np ############################################### ...

  8. numpy中array和asarray的区别

    array和asarray都可以将结构数据转化为ndarray,但是主要区别就是当数据源是ndarray时,array仍然会copy出一个副本,占用新的内存,但asarray不会. 举例说明: imp ...

  9. 【转】numpy中 meshgrid 和 mgrid 的区别和使用

    转自:https://www.cnblogs.com/shenxiaolin/p/8854197.html 一.meshgrid函数 meshgrid函数通常使用在数据的矢量化上. 它适用于生成网格型 ...

随机推荐

  1. Linux下VCS2014和Verdi2015的联合使用

    VCS和Verdi是IC设计中常用的两款开发工具.VCS是Synopsys公司的产品,和大家所熟知的ModeSim一样的都是EDA仿真工具.Verdi是Nocas公司(已经被Synopsys公司收购) ...

  2. [UWP]使用离散式关键帧播放动画

    这篇文章介绍离散式关键帧,并使用它做些有趣的动画. 1. 什么是离散式关键帧 以DoubleAnimationUsingKeyFrames为例,它支持四种Double的关键帧,其中EasingDoub ...

  3. [Unity2d系列教程] 001.引用外部DLL - C#

    众所周知,Unity可以支持多种语言开发, C#, JS, Boo三种方式的开发, 能够很方便的集成一些外部插件,以便调用现有的动态链接库.学过C#的都知道C#可以生成一个dll供给其他的程序调用.那 ...

  4. [注]6W运营法则教你盘活社区内容运营

    社区运营人员大体分为两种:一种是内容运营,这类人才基于产品,一般对文字以及对广告文案比较敏感:另外一种则是更多基于产品推广运营,前者需要把内容最大化地曝光,后者则是把产品推送给用户,两者的是相辅相成, ...

  5. 这个Maven依赖的问题,你敢说你没遇到过

    Maven 依赖没处理好的话经常会导致发生一些问题,非常烦.今天给大家分享一个依赖相关的问题,说不定你之前就遇到过. 问题背景 有个 ES 搜索的项目,刚开始还是好好的状态,过了一段时间,然后就发现启 ...

  6. Linux 比较常用的命令

    #磁盘空间 df -h 显示已经挂载的分区列表 du -sh [file] 估算当前使用磁盘空间 du -sk * | sort -rn 以容量大小递减排序 文件搜索 find find [file_ ...

  7. 轻松实现记录与撤销——C#中的Command模式

    Command模式属于行为模式,作为大名鼎鼎的23个设计模式之一,Command模式理解起来不如工厂模式,单例模式等那么简单直白.究其原因,行为模式着重于使用,如果没有编程实践,确实不如创造模式那么直 ...

  8. “造轮运动”之 ORM框架系列(一)~谈谈我在实际业务中的增删改查

    想想毕业已经快一年了,也就是大约两年以前,怀着满腔的热血正式跨入程序员的世界,那时候的自己想象着所热爱的技术生涯会是多么的丰富多彩,每天可以与大佬们坐在一起讨论解决各种牛逼的技术问题,喝着咖啡,翘着二 ...

  9. 概念辨析-Hardware Description还是Hardware Developing?

    https://mp.weixin.qq.com/s/j4Ndo1R4Go9IaGbhE_nsTg   Verilog, standardized as IEEE 1364, is a hardwar ...

  10. Java实现 LeetCode 523 连续的子数组和(ง •_•)ง

    523. 连续的子数组和 给定一个包含非负数的数组和一个目标整数 k,编写一个函数来判断该数组是否含有连续的子数组,其大小至少为 2,总和为 k 的倍数,即总和为 n*k,其中 n 也是一个整数. 示 ...