摘自https://docs.scipy.org

1.The Basics

1.1 numpy 数组基础

  NumPy’s array class is called ndarray.

  ndarray.ndim    

    the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.

  ndarray.shape

the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For
a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the
rank, or number of dimensions, ndim.

ndarray.size

the total number of elements of the array. This is equal to the product of the elements of shape.

  ndarray.dtype

    an object describing the type of the elements in the array. One can create or specify dtype’s using standard

    Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64
    are some examples.

Example:

>>> import numpy as np
>>> a = np.arange(15).reshape(3, 5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
>>> a.shape
(3, 5)
>>> a.ndim
2
>>> a.dtype.name
'int64'
>>> a.itemsize
8
>>> a.size
15
>>> type(a)
<type 'numpy.ndarray'>
>>> b = np.array([6, 7, 8])
>>> b
array([6, 7, 8])
>>> type(b)
<type 'numpy.ndarray'>

1.2 Array Creation 数组生成

  You can create an array from a regular Python list or tuple using the array function. The type of the resulting array is deduced from the type of the elements in the sequences. A frequent error consists in calling array with multiple numeric arguments, rather than providing a single list of numbers as an argument.(常见错误是把数值作为参数创建数组,应该传入list或者tuple)

>>> a = np.array(1,2,3,4) # WRONG
>>> a = np.array([1,2,3,4]) # RIGHT

  The function zeros creates an array full of zeros, the function ones creates an array full of ones, and the function empty creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64. (常见错误: np.zeros(3,4) ,正确应该为 np.zeros( (3,4) )).

  To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists.  

  It is usually better to use the function linspace that receives as an argument the number of elements that we want, instead of the step

1.3 Basic Operations 基础运算

  Arithmetic operators on arrays apply elementwise.

 b**2
array([0, 1, 4, 9])

  numpy product:

>>> A = np.array( [[1,1],
... [0,1]] )
>>> B = np.array( [[2,0],
... [3,4]] )
>>> A*B # elementwise product
array([[2, 0],
[0, 4]])
>>> A.dot(B) # matrix product
array([[5, 4],
[3, 4]])
>>> np.dot(A, B) # another matrix product
array([[5, 4],
[3, 4]])

  Some operations, such as += and *=, act in place to modify an existing array rather than create a new one.

  When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as upcasting).

2 Shape Manipulation

2.1  Changing the shape of an array

2.2 Stacking together different arrays

https://www.douban.com/note/518335786/?type=like

  The function column_stack stacks 1D arrays as columns into a 2D array. It is equivalent to hstack only for 2D arrays; On the other hand, the function row_stack is equivalent to vstack for any input arrays. In general, for arrays of with more than two dimensions, hstack stacks along their second axes, vstack stacks along their first axes, and concatenate allows for an optional arguments giving the number of the axis along which the concatenation should happen.

  在anaconda中,python源代码中,查看row_stack的定义结果指向了vstack,查看column_stack指向了和hstack,且hstack和vstack都是用的concatenate操作实现的。故row_stack和vstack等价,column和hstack等价。

vstack(),等价于row_stack() 和 np.concatenate(tup, axis=0)

  Stack arrays in sequence vertically (row wise).

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

hstack(),等价于column_stack() 和 np.concatenate(tup, axis=1)

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

dstack(), 等价于np.concatenate(tup, axis=2)

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

concatenate() 默认axis = 0
np.c_[]

np.r_[] 分别添加行和列

np.insert

【Python】numpy 数组拼接、分割的更多相关文章

  1. python numpy 数组拼接

    我就写一下我遇到的,更多具体的请看Python之Numpy数组拼接,组合,连接 >>> aarray([0, 1, 2],       [3, 4, 5],       [6, 7, ...

  2. Python之Numpy数组拼接,组合,连接

    转自:https://www.douban.com/note/518335786/?type=like ============改变数组的维度==================已知reshape函数 ...

  3. numpy数组 拼接

    转载自:https://blog.csdn.net/zyl1042635242/article/details/43162031 数组拼接方法一 首先将数组转成列表,然后利用列表的拼接函数append ...

  4. python numpy数组操作

    数组的创建 import numpy as np arr1 = np.array([3,10,8,7,34,11,28,72]) arr2 = np.array(((8.5,6,4.1,2,0.7), ...

  5. Python Numpy 数组的初始化和基本操作

    一.基础: Numpy的主要数据类型是ndarray,即多维数组.它有以下几个属性: ndarray.ndim:数组的维数 ndarray.shape:数组每一维的大小 ndarray.size:数组 ...

  6. python numpy数组中的复制问题

    vector = numpy.array([5, 10, 15, 20]) equal_to_ten_or_five = (vector == 10) | (vector == 5) vector[e ...

  7. numpy——>数组拼接np.concatenate

    语法:np.concatenate((a1, a2, ...), axis=0) 1.默认是 axis = 0,也就是说对0轴(行方向)的数组对象,进行其垂直方向(axis=1)的拼接(即数据整行整行 ...

  8. numpy数组的分割与合并

    合并 np.newaxis import numpy as np a=np.array([1,2,3])[:,np.newaxis]#变成列向量 b=np.array([4,5,6])[:,np.ne ...

  9. python numpy数组操作2

    数组的四则运算 在numpy模块中,实现四则运算的计算既可以使用运算符号,也可以使用函数,具体如下例所示: #加法运算 import numpy as npmath = np.array([98,83 ...

随机推荐

  1. Android 虚化图片的方法

    Android 虚化图片 模糊图片 图片毛玻璃效果. 效果如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaDNjNGxlbm92bw==/font/ ...

  2. php解码“&#”编码的中文用函数html_entity_decode()

    遇到类似 ' 这种编码的字,我们可以用html_entity_decode()函数来解码. html_entity_decode() 函数把 HTML 实体转换为字符. 语法 html_entity_ ...

  3. gdb ../sysdeps/i386/elf/start.S: No such file or directory

    使用 gdb 调试的时候 输入 l 之后出现下列信息 (gdb) l 1 ../sysdeps/i386/elf/start.S: No such file or directory. in ../s ...

  4. Spring MVC获得HttpServletRequest

    以下代码是获得Spring MVC中的HttpServletRequest ServletRequestAttributes attr = (ServletRequestAttributes) Req ...

  5. spring4 maven3 mybatis

    1 新建maven工程 http://www.cnblogs.com/quanyongan/archive/2013/04/21/3033838.html 如果在第三步中出现错误,比如类似: Coul ...

  6. python 中读取excel

    第一步:  先下载一个xlrd 包 # pip install xlrd import xlrd from datetime import date, datetime file = '学生信息表.x ...

  7. Grunt是什么,以及它的一些使用方法

    ♥什么是Grunt Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器.grunt是基于node 更多插件请访问:http://www.gruntjs.n ...

  8. memcached系列

    memcached系列:http://blog.csdn.net/xingxing513234072/article/category/2462865

  9. java基本类型和包装类的区别(转)

    int 是基本类型,直接存数值 Integer是类,产生对象时用一个引用指向这个对象 Java把内存划分成两种:一种是栈内存,另一种是堆内存 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数 ...

  10. Git with SVN

    1)GIT是分布式的,SVN不是: 这 是GIT和其它非分布式的版本控制系统,例如SVN,CVS等,最核心的区别.好处是跟其他同事不会有太多的冲突,自己写的代码放在自己电脑上,一段时间后再提交.合并, ...