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函数通常使用在数据的矢量化上. 它适用于生成网格型 ...
随机推荐
- SpringMVC笔记总结
文章所有代码见:gitee 1.回顾MVC 1.1.什么是MVC MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范. 是将业务逻辑.数据.显示分离 ...
- corosync+pacemaker实现httpd高可用
corosync+pacemaker 官方网址 https://clusterlabs.org/ 一.开源高可用了解 OPEN SOURCE HIGH AVAILABILITY CLUSTER STA ...
- 实验五:shell脚本编程
项目 内容 这个作业属于哪个课程 班级课程的主页链接 这个作业的要求在哪里 作业要求链接地址 学号-姓名 17043133-木腾飞 作业学习目标 1.了解shell 脚本的概念及应用2.掌握shell ...
- Pandas读取文件报错UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte
pandas读取文件时报UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start by ...
- CSS综合案例
代码示例:新闻页面: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- [JavaWeb基础] 018.Struts2 Action通配符使用
Struts2中有一个很牛逼的action通配符,可以用来简化action配置,以我们将要讲解的案例来说,如果我们要对一个学生信息进行增加,删除,修改,那么按照原来的做法,我们需要写3个Action来 ...
- require.js与IDEA的配合
本文主要讲述在html中使用requirejs时,如何让IDEA更加智能识别javascript的方法. 测试时的目录结构,一种典型的 thinkphp 的结构,同时,在 a.thml 中通过 req ...
- 如何利用 Python 爬虫实现给微信群发新闻早报?(详细)
1. 场景 经常有小伙伴在交流群问我,每天的早报新闻是怎么获取的? 其实,早期使用的方案,是利用爬虫获取到一些新闻网站的标题,然后做了一些简单的数据清洗,最后利用 itchat 发送到指定的社群中. ...
- Blazor WebAssembly 修仙之途 - 组件与数据绑定
一.前言 在第一篇文章中,有提到过组件(Component)这个概念.组件在 Blazor 中是必不可少的,UI 全靠它组装起来,和前端的 JS 组件是一个意思,比如:vue component.re ...
- PIX防火墙配置A/S故障切换
PIX防火墙配置A/S故障切换 1.基本命令 failover show failover failover lan enable failover lan interface zwish e2 fa ...