【Python】numpy 数组拼接、分割
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 数组拼接、分割的更多相关文章
- python numpy 数组拼接
我就写一下我遇到的,更多具体的请看Python之Numpy数组拼接,组合,连接 >>> aarray([0, 1, 2], [3, 4, 5], [6, 7, ...
- Python之Numpy数组拼接,组合,连接
转自:https://www.douban.com/note/518335786/?type=like ============改变数组的维度==================已知reshape函数 ...
- numpy数组 拼接
转载自:https://blog.csdn.net/zyl1042635242/article/details/43162031 数组拼接方法一 首先将数组转成列表,然后利用列表的拼接函数append ...
- 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), ...
- Python Numpy 数组的初始化和基本操作
一.基础: Numpy的主要数据类型是ndarray,即多维数组.它有以下几个属性: ndarray.ndim:数组的维数 ndarray.shape:数组每一维的大小 ndarray.size:数组 ...
- python numpy数组中的复制问题
vector = numpy.array([5, 10, 15, 20]) equal_to_ten_or_five = (vector == 10) | (vector == 5) vector[e ...
- numpy——>数组拼接np.concatenate
语法:np.concatenate((a1, a2, ...), axis=0) 1.默认是 axis = 0,也就是说对0轴(行方向)的数组对象,进行其垂直方向(axis=1)的拼接(即数据整行整行 ...
- numpy数组的分割与合并
合并 np.newaxis import numpy as np a=np.array([1,2,3])[:,np.newaxis]#变成列向量 b=np.array([4,5,6])[:,np.ne ...
- python numpy数组操作2
数组的四则运算 在numpy模块中,实现四则运算的计算既可以使用运算符号,也可以使用函数,具体如下例所示: #加法运算 import numpy as npmath = np.array([98,83 ...
随机推荐
- Nonblocking Memory Refresh&2018ISCA/Security& 非阻塞内存刷新
Abstract 我们提议的非阻塞刷新工作是一次刷新内存块中的一部分数据,并在内存块中使用冗余数据,如RS码,在块中计算块的刷新/不可读数据以满足读取请求.作为概念的证明,我们将非阻塞刷新应用于服务器 ...
- Java源代码之LinkedHashMap
Java源代码之LinkedHashMap 转载请注明出处:http://blog.csdn.net/itismelzp/article/details/50554412 一.LinkedHashMa ...
- programming review (c++): (3)graph, binary search
I.graph #include <iostream> #include <vector> using namespace std; vector<vector<, ...
- python 基础 5.1 python 构造器
一. 类的构造器 __init__ 构造函数,在生成对象时调用.由于类可以起到模板的作用,因此,可以在创建实例的时候,把一些我们认为必须绑定的属性强制填写进去.通过定义一个特殊的__init__方法, ...
- VI带行号查看
:set nu 带行号查看,并不改变文件内容 :set nonu 取消带行号查看 在每个用户的主目录下,都有一个 vi 的配置文件".vimrc"或 ...
- 九度OJ 1178:复数集合 (插入排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8393 解决:1551 题目描述: 一个复数(x+iy)集合,两种操作作用在该集合上: 1.Pop 表示读出集合中复数模值最大的那个复数,如 ...
- JavaScript 四种显示数据方式
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 我的Android进阶之旅------>Android如何通过自定义SeekBar来实现视频播放进度条
首先来看一下效果图,如下所示: 其中进度条如下: 接下来说一说我的思路,上面的进度拖动条有自定义的Thumb,在Thumb正上方有一个PopupWindow窗口,窗口里面显示当前的播放时间.在Seek ...
- matlab实战中一些重要的函数总结
这段时间看了一些大型的matlabproject文件(如:faster r-cnn),对于project中常常要用到的一些函数进行一个总结. 1.路径问题. 这主要涵括文件路径的包括和组合. curd ...
- BCH硬分叉在即,Bitcoin ABC和NChain两大阵营PK
混迹币圈,我们都知道,BTC分叉有了BCH,而近期BCH也将面临分叉,这次分叉将是Bitcoin ABC和NChain两大阵营的较量,最后谁能成为主导,我们拭目以待. 比特币现金(BCH)的价格自上周 ...