numpy.concatenate

import numpy as np

a = np.array([[1, 2], [3, 4]])

a.shape
Out[3]: (2, 2)

b = np.array([[5, 6]])

b.shape
Out[5]: (1, 2)

np.concatenate((a, b))
Out[6]:
array([[1, 2],
       [3, 4],
       [5, 6]])

c= np.concatenate((a, b))

c.shape
Out[8]: (3, 2)

d = np.concatenate((a, b), axis=0)

d.shape
Out[10]: (3, 2)

e = np.concatenate((a, b), axis=1)
Traceback (most recent call last):

  File "<ipython-input-11-05a280a2cb02>", line 1, in <module>
    e = np.concatenate((a, b), axis=1)

ValueError: all the input array dimensions except for the concatenation axis must match exactly

e = np.concatenate((a, b.T), axis=1)

e.shape
Out[13]: (2, 3)

import numpy as np

a = np.array([[1, 2], [3, 4]])

a.shape
Out[3]: (2, 2)

b = np.array([[5, 6]])

b.shape
Out[5]: (1, 2)

np.concatenate((a, b))
Out[6]:
array([[1, 2],
[3, 4],
[5, 6]])

c= np.concatenate((a, b))

c.shape
Out[8]: (3, 2)

d = np.concatenate((a, b), axis=0)

d.shape
Out[10]: (3, 2)

e = np.concatenate((a, b), axis=1)
Traceback (most recent call last):

File "<ipython-input-11-05a280a2cb02>", line 1, in <module>
e = np.concatenate((a, b), axis=1)

ValueError: all the input array dimensions except for the concatenation axis must match exactly

e = np.concatenate((a, b.T), axis=1)

e.shape
Out[13]: (2, 3)

e
Out[14]:
array([[1, 2, 5],
[3, 4, 6]])

时间: 2024-08-25 14:45:26

numpy.concatenate的相关文章

[Python Cookbook] Numpy Array Joint Methods: Append, Extend &amp; Concatenate

数组拼接方法一 思路:首先将数组转成列表,然后利用列表的拼接函数append().extend()等进行拼接处理,最后将列表转成数组. 示例1: import numpy as np a=np.array([1,2,5]) b=np.array([10,12,15]) a_list=list(a) b_list=list(b) a_list.extend(b_list) a_list [1, 2, 5, 10, 12, 15] a=np.array(a_list) a array([ 1,  2

Pandas -- Merge,join and concatenate

Merge, join, and concatenate pandas provides various facilities for easily combining together Series, DataFrame, and Panel objects with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type o

numpy教程:数组操作

http://blog.csdn.net/pipisorry/article/details/39496831 Array manipulation routines numpy数组基本操作,包括copy, shape, 转换(类型转换), type, 重塑等等.这些操作应该都可以使用numpy.fun(array)或者array.fun()来调用. Basic operations copyto(dst, src[, casting, where])Copies values from one

python之numpy的基本使用

一.numpy概述 numpy(Numerical Python)提供了python对多维数组对象的支持:ndarray,具有矢量运算能力,快速.节省空间.numpy支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库. 二.创建ndarray数组 ndarray:N维数组对象(矩阵),所有元素必须是相同类型. ndarray属性:ndim属性,表示维度个数:shape属性,表示各维度大小:dtype属性,表示数据类型. 创建ndarray数组函数: 代码示例: # -*-

numpy 使用详解

numpy.arange([start, ]stop, [step, ]dtype=None) 返回数值均匀分布的数组 >>> np.arange(3) array([0, 1, 2]) >>> np.arange(3,7) array([3, 4, 5, 6]) >>> np.arange(3,7,2) array([3, 5] numpy.reshape(a, newshape, order='C') ndarray.reshape(shape,

numpy array转置与两个array合并

我们知道,用 .T 或者 .transpose() 都可以将一个矩阵进行转置. 但是一维数组转置的时候有个坑,光transpose没有用,需要指定shape参数, 在array中,当维数>=2,时这个成立,但=1时,就不成立了,如: In [7]: yOut[7]: array([0, 0, 0, 0, 0]) In [14]: y.TOut[14]: array([0, 0, 0, 0, 0]) In [15]: y.transpose()Out[15]: array([0, 0, 0, 0,

numpy数组 拼接

转载自:https://blog.csdn.net/zyl1042635242/article/details/43162031 数组拼接方法一 首先将数组转成列表,然后利用列表的拼接函数append().extend()等进行拼接处理,最后将列表转成数组. 例1: >>> import numpy as np>>> a=np.array([1,2,5])>>> b=np.array([10,12,15])>>> a_list=lis

python——Numpy库

Numpy库 英文官方文档:https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html Numpy库中的数组对象:N维数组类型:ndarray 1)      ndarray的作用: a)      数组对象性可以去掉元素间运算所需的循环,使一维向量更像单个数据. b)     设置专门的数组对象,经过优化,可以提升这类应用的运算速度. 2)      ndarray是一个多维数组对象,有两部分组成: 实际的数据 和 描述这些数据

Python之Numpy详细教程

NumPy - 简介 NumPy 是一个 Python 包. 它代表 “Numeric Python”. 它是一个由多维数组对象和用于处理数组的例程集合组成的库. Numeric,即 NumPy 的前身,是由 Jim Hugunin 开发的. 也开发了另一个包 Numarray ,它拥有一些额外的功能. 2005年,Travis Oliphant 通过将 Numarray 的功能集成到 Numeric 包中来创建 NumPy 包. 这个开源项目有很多贡献者. NumPy 操作 使用NumPy,开