numpy.concatenate函数用法详解

作者:houyushui 时间:2022-04-09 06:18:52 

这个concatenate用于将矩阵合并,他将沿着已经存在的轴合并一个矩阵,相关参数有(a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind",其中第一个参数是用户输入的矩阵, 这些输入的矩阵必须要在将要合并的对应的轴上有相同的形状,

官方文档的机器翻译:矩阵必须具有相同的形状,除非是与轴对应的尺寸(默认为第一个)。

numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")

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=1的轴,进行合并:

a = np.arange(3*3).reshape((3,3))
b = np.arange(3*4).reshape((3,4))

a,b
(array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]]),
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]]))

np.concatenate([a,b],axis=1)
array([[ 0,  1,  2,  0,  1,  2,  3],
      [ 3,  4,  5,  4,  5,  6,  7],
      [ 6,  7,  8,  8,  9, 10, 11]])

上面是沿着列进行合并,尽管他们的列数不同,但是他们的行数相同,因此也可以合并。

 axis int, optional
      The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0.

如果将axis设置为None,那么将对给出的矩阵先进行展平,即先将其转换为一维数组,再合并,默认的axis参数是0:

np.concatenate([a,b],axis=None)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  0,  1,  2,  3,  4,  5,  6,  7,
       8,  9, 10, 11])

casting {‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional
       Controls what kind of data casting may occur. Defaults to ‘same_kind’.

下面给出一些可能触发的错误:

np.concatenate(a,b,axis=None)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-36-0e550a3d06f6> in <module>
----> 1 np.concatenate(a,b,axis=None)
 
<__array_function__ internals> in concatenate(*args, **kwargs)
 
TypeError: concatenate() got multiple values for argument 'axis'

这个类型错误发生的原因是,将要合并的两个数组未添加括号的就作为参数输入了

正确的形式如下:

np.concatenate([a,b],axis=None)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  0,  1,  2,  3,  4,  5,  6,  7,
       8,  9, 10, 11])

或者:

c = (a,b)
np.concatenate(c,axis=None)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  0,  1,  2,  3,  4,  5,  6,  7,
       8,  9, 10, 11])

来源:https://blog.csdn.net/houyushui/article/details/116894753

标签:numpy,concatenate
0
投稿

猜你喜欢

  • 国内外字体网站(font)的整理

    2007-10-14 09:58:00
  • python获取交互式ssh shell的方法

    2021-11-25 13:15:21
  • MySQL分区的功能和限制讲解

    2010-10-14 14:03:00
  • Oracle常用命令大全集

    2010-07-21 13:18:00
  • 详解python itertools功能

    2022-12-05 23:07:31
  • python3使用print打印带颜色的字符串代码实例

    2022-01-20 10:30:49
  • 跟老齐学Python之关于循环的小伎俩

    2022-07-20 07:03:36
  • 向上不间断(无缝)滚动图片js代码

    2007-09-24 20:22:00
  • Python实现的递归神经网络简单示例

    2022-03-26 04:19:48
  • Pandas时间序列重采样(resample)方法中closed、label的作用详解

    2023-01-31 17:46:36
  • Tensorflow轻松实现XOR运算的方式

    2022-10-20 13:20:41
  • PyCharm关闭碍眼的波浪线图文详解

    2023-07-27 08:47:53
  • Highcharts 图表中图例显示状态存储的功能设计详解

    2023-05-30 02:01:09
  • 跟老齐学Python之网站的结构

    2021-08-22 05:55:54
  • Django 项目布局方法(值得推荐)

    2022-08-22 12:44:22
  • python中的selenium安装的步骤(浏览器自动化测试框架)

    2022-11-13 03:18:53
  • CSS框架的利与弊

    2007-12-06 12:59:00
  • pandas中merge()函数的用法解读

    2023-10-02 08:49:46
  • python中itertools模块zip_longest函数详解

    2023-01-02 09:09:35
  • 通过Python中的CGI接口讲解什么是WSGI

    2022-08-10 15:04:24
  • asp之家 网络编程 m.aspxhome.com