在使用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. Unity 游戏框架搭建 2019 (四十八/四十九) MonoBehaviourSimplify 中的消息策略完善&关于发送事件的简单封装

    MonoBehaviourSimplify 中的消息策略完善 在上一篇,笔者说,MonoBehaviourSimplify 中的消息策略还有一些小问题.我们在这篇试着解决一下. 先贴出来代码: usi ...

  2. 【JAVA习题六】输入两个正整数m和n,求其最大公约数

    import java.util.Scanner; public class Oujilide欧几里得 { public static void main(String[] args) { // TO ...

  3. Eclipse中java文件选中变量名,相同变量都变色显示 .

    第一步设置高亮显示的颜色: Window-->preferences-->General-->Editors-->Text Editors-->Annotations-- ...

  4. DataFrame-选择与切片

    取得DataFrame对象reviews的description列的前10个值(或者说reviews前10行的description列): reviews.iloc[:10].loc[:,'descr ...

  5. 【JVM】堆体系结构及其内存调优

    堆体系结构 一个JVM实例只存在一个堆内存,堆内存的大小是可调节的.类加载器读取类文件后,需要把类.方法.常量.变量放在堆内存中,保存所有引用类型的真实信息,以方便执行器指向,堆内存分为三个部分:年轻 ...

  6. 分布式事务专题笔记(三)分布式事务解决方案之TCC(三阶段提交)

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1.什么是TCC事务 TCC是Try.Confifirm.Cancel三个词语的缩写,TCC要求每个分支 ...

  7. Java实现蓝桥杯第八届决赛 对局匹配

    标题:对局匹配 小明喜欢在一个围棋网站上找别人在线对弈.这个网站上所有注册用户都有一个积分,代表他的围棋水平. 小明发现网站的自动对局系统在匹配对手时,只会将积分差恰好是K的两名用户匹配在一起.如果两 ...

  8. Java实现 LeetCode 89 格雷编码

    89. 格雷编码 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异. 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 示例 1: 输 ...

  9. Java实现 LeetCode 84 柱状图中最大得矩形

    84. 柱状图中最大的矩形 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的 ...

  10. Java实现 LeetCode 39 组合总和

    39. 组合总和 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字 ...