numpy.ma.fromfunction
numpy.ma.fromfunction(function, shape, **kwargs) = <numpy.ma.core._convert2ma instance at 0x7f63b516d4d0>
返回数组的坐标值经过function运算之后的数组:
例如:
>>> b = np.fromfunction(lambda x,y,z:x*100+y*10+z,(3,3,3),dtype=int)
>>> b
array([[[ 0, 1, 2],
[ 10, 11, 12],
[ 20, 21, 22]],
[[100, 101, 102],
[110, 111, 112],
[120, 121, 122]],
[[200, 201, 202],
[210, 211, 212],
[220, 221, 222]]])
b中的数组是通过它的坐标值运算得到的,即*100+y*10+z
Rounding(数据的凑整,诸如四舍五入)
around(a[, decimals, out]) Evenly round to the given number of decimals.
round_(a[, decimals, out]) Round an array to the given number of decimals.
rint(x[, out]) Round elements of the array to the nearest integer.
fix(x[, y]) Round to nearest integer towards zero.
floor(x[, out]) Return the floor of the input, element-wise.
ceil(x[, out]) Return the ceiling of the input, element-wise.
trunc(x[, out]) Return the truncated value of the input, element-wise.
具体细节参考文档 3.20.3 Rounding
形状操作:
一个数组的形状由它每个轴上的元素个数给出:
>>> a = floor(10*random.random((3,4)))
>>> a
array([[ 7., 5., 9., 3.],
[ 7., 2., 7., 8.],
[ 6., 8., 3., 2.]])
>>> a.shape
(3, 4)
一个数组的形状可以被多种命令修改:
>>> a.ravel() # flatten the array
array([ 7., 5., 9., 3., 7., 2., 7., 8., 6., 8., 3., 2.])
>>> a.shape = (6, 2)
>>> a.transpose()
array([[ 7., 9., 7., 7., 6., 3.],
[ 5., 3., 2., 8., 8., 2.]])
由
ravel()
展平的数组元素的顺序通常是“C风格”的,就是说,最右边的索引变化得最快,所以元素a[0,0]之后是a[0,1]。如果数组被改变形状(reshape)成其它形状,数组仍然是“C风格”的。NumPy通常创建一个以这个顺序保存数据的数组,所以
ravel()
将总是不需要复制它的参数
3
。但是如果数组是通过切片其它数组或有不同寻常的选项时,它可能需要被复制。函数
reshape()
和
ravel()
还可以被同过一些可选参数构建成FORTRAN风格的数组,即最左边的索引变化最快。
reshape
函数改变参数形状并返回它,而 resize
函数改变数组自身。
>>> a
array([[ 7., 5.],
[ 9., 3.],
[ 7., 2.],
[ 7., 8.],
[ 6., 8.],
[ 3., 2.]])
>>> a.resize((2,6))
>>> a
array([[ 7., 5., 9., 3., 7., 2.],
[ 7., 8., 6., 8., 3., 2.]])
数组堆叠操作:
>>> a = floor(10*random.random((2,2)))
>>> a
array([[ 1., 1.],
[ 5., 8.]])
>>> b = floor(10*random.random((2,2)))
>>> b
array([[ 3., 3.],
[ 6., 0.]])
>>> vstack((a,b))
array([[ 1., 1.],
[ 5., 8.],
[ 3., 3.],
[ 6., 0.]])
>>> hstack((a,b))
array([[ 1., 1., 3., 3.],
[ 5., 8., 6., 0.]])
其中vstack将其纵向堆叠,hstack将其横向堆叠。
函数
column_stack
以列将一维数组合成二维数组,它等同与
vstack
对一维数组。
>>> column_stack((a,b)) # With 2D arrays
array([[ 1., 1., 3., 3.],
[ 5., 8., 6., 0.]])
>>> a=array([4.,2.])
>>> b=array([2.,8.])
>>> a[:,newaxis] # This allows to have a 2D columns vector
array([[ 4.],
[ 2.]])
>>> column_stack((a[:,newaxis],b[:,newaxis]))
array([[ 4., 2.],
[ 2., 8.]])
>>> vstack((a[:,newaxis],b[:,newaxis])) # The behavior of vstack is different
array([[ 4.],
[ 2.],
[ 2.],
[ 8.]])
复制和视图
完全不拷贝
简单的赋值不拷贝数组对象或它们的数据。
>>> a = arange(12)
>>> b = a # no new object is created
>>> b is a # a and b are two names for the same ndarray object
True
>>> b.shape = 3,4 # changes the shape of a
>>> a.shape
(3, 4)
视图(view)和浅复制
不同的数组对象分享同一个数据。视图方法创造一个新的数组对象指向同一数据。>>> c = a.view()
>>> c is a
False
>>> c.base is a # c is a view of the data owned by a
True
>>> c.flags.owndata
False
>>>
>>> c.shape = 2,6 # a's shape doesn't change
>>> a.shape
(3, 4)
>>> c[0,4] = 1234 # a's data changes
>>> a
array([[ 0, 1, 2, 3],
[1234, 5, 6, 7],
[ 8, 9, 10, 11]])
从特征上看a和c是不同形态的数组,但是a和c是同属一块数据区域,类似于opencv中Mat的浅拷贝
深复制
这个复制方法完全复制数组和它的数据。
>>> d = a.copy() # a new array object with new data is created
>>> d is a
False
>>> d.base is a # d doesn't share anything with a
False
>>> d[0,0] = 9999
>>> a
array([[ 0, 10, 10, 3],
[1234, 10, 10, 7],
[ 8, 10, 10, 11]])
函数和方法(method)总览(详细参考文档)
创建数组
arange, array, copy, empty, empty_like, eye, fromfile, fromfunction, identity, linspace, logspace, mgrid, ogrid, ones, ones_like, r , zeros, zeros_like
转化
astype, atleast 1d, atleast 2d, atleast 3d, mat
操作
array split, column stack, concatenate, diagonal, dsplit, dstack, hsplit, hstack, item, newaxis, ravel, repeat, reshape, resize, squeeze, swapaxes, take, transpose, vsplit, vstack
询问
all, any, nonzero, where
排序
argmax, argmin, argsort, max, min, ptp, searchsorted, sort
运算
choose, compress, cumprod, cumsum, inner, fill, imag, prod, put, putmask, real, sum
基本统计
cov, mean, std, var
基本线性代数
cross, dot, outer, svd, vdot