Most of the useful manipulations on arrays are done with functions. This might
be surprising given Python's object-oriented framework, and that many of these
functions could have been implemented using methods instead. Choosing functions
means that the same procedures can be applied to arbitrary python sequences,
not just to arrays. For example, while transpose([[1,2],[3,4]])
works
just fine, [[1,2],[3,4]].transpose()
does not. This approach also allows
uniformity in interface between functions defined in the numarray Python
system, whether implemented in C or in Python, and functions defined in
extension modules. We've already covered two functions which operate on arrays:
reshape
and resize
.
array, indices, axis=0, clipmode=CLIP) |
take
is a generalized indexing/slicing of the array. In
its simplest form, it is equivalent to indexing:
>>> a1 = array([10,20,30,40]) >>> print a1[[1,3]] [20 40] >>> print take(a1,[1,3]) [20 40]
take
and put
. take
selects the elements of the array (the first argument) based on the
indices (the second argument). Unlike slicing, however, the array
returned by take
has the same rank as the input array.
Some 2-D examples:
>>> print a2 [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19]] >>> print take(a2, (0,)) # first row [[0 1 2 3 4]] >>> print take(a2, (0,1)) # first and second row [[0 1 2 3 4] [5 6 7 8 9]] >>> print take (a2, (0, -1)) # index relative to dimension end [[ 0 1 2 3 4] [15 16 17 18 19]]
>>> print take(a2, (0,), axis=1) # first column [[ 0] [ 5] [10] [15]] >>> print take(a2, (0,1), axis=1) # first and second column [[ 0 1] [ 5 6] [10 11] [15 16]] >>> print take(a2, (0,4), axis=1) # first and last column [[ 0 4] [ 5 9] [10 14] [15 19]]
This is considered to be a structural operation, because its result does not depend on the content of the arrays or the result of a computation on those contents but uniquely on the structure of the array. Like all such structural operations, the default axis is 0 (the first rank). Later in this tutorial, we will see other functions with a default axis of -1.
take is often used to create multidimensional arrays with the indices from a rank-1 array. As in the earlier examples, the shape of the array returned by take is a combination of the shape of its first argument and the shape of the array that elements are "taken" from - when that array is rank-1, the shape of the returned array has the same shape as the index sequence. This, as with many other facets of numarray, is best understood by experiment.
>>> x = arange(10) * 100 >>> print x [ 0 100 200 300 400 500 600 700 800 900] >>> print take(x, [[2,4],[1,2]]) [[200 400] [100 200]]
m51
is a 2-D array of type UInt8
containing a greyscale image. We can
create a table mapping the integers 0-255 to integers 0-255 using a
"compressive nonlinearity":
>>> table = (255 - arange(256)**2 / 256).astype(UInt8)
>>> m51b = take(table, m51)
>>> print take(a2, [0,1], [0,1]) 1 >>> print take(a2, [0,1], [1,0]) 5
array, indices, values, axis=0, clipmode=CLIP) |
>>> x = arange(6) >>> put(x, [2,4], [20,40]) >>> print x [ 0 1 20 3 40 5]
ind = array(indices, copy=0) v = array(values, copy=0).astype(a.type()) for i in range(len(ind)): a.flat[i] = v[i]
array, mask, values) |
>>> x=arange(5) >>> putmask(x, [1,0,1,0,1], [10,20,30,40,50]) >>> print x [10 1 30 3 50] >>> putmask(x, [1,0,1,0,1], [-1,-2]) >>> print x [-1 1 -1 3 -1]
[-1, -2, -1, -2, -1]
.
array, axes=None) |
axes=arange(a.rank)[::-1]
.
>>> a2=arange(6,shape=(2,3)); print a2 [[0 1 2] [3 4 5]] >>> print transpose(a2) # same as transpose(a2, axes=(1,0)) [[0 3] [1 4] [2 5]] >>> a3=arange(24,shape=(2,3,4)); print a3 [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] >>> print transpose(a3) # same as transpose(a3, axes=(2,1,0)) [[[ 0 12] [ 4 16] [ 8 20]] [[ 1 13] [ 5 17] [ 9 21]] [[ 2 14] [ 6 18] [10 22]] [[ 3 15] [ 7 19] [11 23]]] >>> print transpose(a3, axes=(1,0,2)) [[[ 0 1 2 3] [12 13 14 15]] [[ 4 5 6 7] [16 17 18 19]] [[ 8 9 10 11] [20 21 22 23]]]
array, repeats, axis=0) |
>>> print a [[0 1 2] [3 4 5]] >>> print repeat(a, 2*ones(a.shape[0])) # i.e. repeat(a, (2,2)), broadcast # rules apply, so this is also equivalent to repeat(a, 2) [[0 1 2] [0 1 2] [3 4 5] [3 4 5]] >>> print repeat(a, 2*ones(a.shape[1]), 1) # i.e. repeat(a, (2,2,2), 1), or # repeat(a, 2, 1) [[0 0 1 1 2 2] [3 3 4 4 5 5]] >>> print repeat(a, (1,2)) [[0 1 2] [3 4 5] [3 4 5]]
condition, x, y, out=None) |
>>> where(arange(10) >= 5, 1, 2) array([2, 2, 2, 2, 2, 1, 1, 1, 1, 1])
Starting with numarray-0.6, where supports a one parameter form that is equivalent to the nonzero function but reads better:
>>> where(arange(10) % 2) (array([1, 3, 5, 7, 9]),) # indices where expression is true
Like nonzero, the one parameter form of where can be used to do array indexing:
>>> a = arange(10,20) >>> a[ where( a % 2 ) ] array([11, 13, 15, 17, 19])
Note that for array indices which are boolean arrays, using where is not necessary but is still OK:
>>> a[(a % 2) != 0] array([11, 13, 15, 17, 19]) >>> a[where((a%2) != 0)] array([11, 13, 15, 17, 19])
selector, population, outarr=None, clipmode=RAISE) |
population=(b0, ..., bn)
as indicated by the value of the corresponding element in selector.
Assume selector is an array that you want to "clip" so that no values
are greater than 100.0.
>>> choose(greater(a, 100.0), (a, 100.0))
greater(a, 100.0)
is false (i.e. 0) this
will ``choose'' the corresponding value in a. Everywhere else
it will ``choose'' 100.0. This works as well with arrays.
Try to figure out what the following does:
>>> ret = choose(greater(a,b), (c,d))
array) |
reshape(a, (-1,))
. There is a ravel
method which reshapes the array in-place. Unlike a.ravel()
,
however, the ravel function works with non-contiguous arrays.
>>> a=arange(25) >>> a.setshape(5,5) >>> a.transpose() >>> a.iscontiguous() 0 >>> a array([[ 0, 5, 10, 15, 20], [ 1, 6, 11, 16, 21], [ 2, 7, 12, 17, 22], [ 3, 8, 13, 18, 23], [ 4, 9, 14, 19, 24]]) >>> a.ravel() Traceback (most recent call last): ... TypeError: Can't reshape non-contiguous arrays >>> ravel(a) array([ 0, 5, 10, 15, 20, 1, 6, 11, 16, 21, 2, 7, 12, 17, 22, 3, 8, 13, 18, 23, 4, 9, 14, 19, 24])
a) |
>>> a = array([-1, 0, 1, 2]) >>> nonzero(a) (array([0, 2, 3]),) >>> print a2 [[-1 0 1 2] [ 9 0 4 0]] >>> print nonzero(a2) (array([0, 0, 0, 1, 1]), array([0, 2, 3, 0, 2]))
condition, a, axis=0) |
>>> print x [1 0 6 2 3 4] >>> print greater(x, 2) [0 0 1 0 1 1] >>> print compress(greater(x, 2), x) [6 3 4] >>> print a2 [[-1 0 1 2] [ 9 0 4 0]] >>> print compress(a2>1, a2) [2 9 4] >>> a = array([[1,2],[3,4]]) >>> print compress([1,0], a, axis = 1) [[1] [3]] >>> print compress([[1,0],[0,1]], a) [1, 4]
a, offset=0, axis1=0, axis2=1) |
Warning: in Numeric (and numarray 0.7 or before), there is a bug in the diagonal function which will give erronous result for arrays of 3-D or higher.
>>> print x [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19] [20 21 22 23 24]] >>> print diagonal(x) [ 0 6 12 18 24] >>> print diagonal(x, 1) [ 1 7 13 19] >>> print diagonal(x, -1) [ 5 11 17 23]
a, offset=0, axis1=0, axis2=1) |
Warning: in Numeric (and numarray 0.7 or before), there is a bug in the trace function which will give erronous result for arrays of 3-D or higher.
>>> print x [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19] [20 21 22 23 24]] >>> print trace(x) # 0 + 6 + 12 + 18 + 24 60 >>> print trace(x, -1) # 5 + 11 + 17 + 23 56 >>> print trace(x, 1) # 1 + 7 + 13 + 19 40
bin, values) |
>>> print bin_boundaries [ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ] >>> print data [ 0.31 0.79 0.82 5. -2. -0.1 ] >>> print searchsorted(bin_boundaries, data) [4 8 9 11 0 0]
>>> def histogram(a, bins): ... # Note that the argument names below are reverse of the ... # searchsorted argument names ... n = searchsorted(sort(a), bins) ... n = concatenate([n, [len(a)]]) ... return n[1:]-n[:-1] ... >>> print histogram([0,0,0,0,0,0,0,.33,.33,.33], arange(0,1.0,.1)) [7 0 0 3 0 0 0 0 0 0] >>> print histogram(sin(arange(0,10,.2)), arange(-1.2, 1.2, .1)) [0 0 4 2 2 2 0 2 1 2 1 3 1 3 1 3 2 3 2 3 4 9 0 0]
array, axis=-1) |
sort(a, 3)
will be an array of the same shape as array,
where the elements of array have been sorted along the fourth
axis.
>>> print data [[5 0 1 9 8] [2 5 8 3 2] [8 0 3 7 0] [9 6 9 5 0] [9 0 9 7 7]] >>> print sort(data) # Axis -1 by default [[0 1 5 8 9] [2 2 3 5 8] [0 0 3 7 8] [0 5 6 9 9] [0 7 7 9 9]] >>> print sort(data, 0) [[2 0 1 3 0] [5 0 3 5 0] [8 0 8 7 2] [9 5 9 7 7] [9 6 9 9 8]]
array, axis=-1) |
sort(array)
. In other words, for a 1-D array,
take(a.flat, argsort(a))
is the same as sort(a)
... but slower.
>>> print data [5 0 1 9 8] >>> print sort(data) [0 1 5 8 9] >>> print argsort(data) [1 2 0 4 3] >>> print take(data, argsort(data)) [0 1 5 8 9]
array, axis=-1) |
array, axis=-1) |
>>> print data [[9 6 1 3 0] [0 0 8 9 1] [7 4 5 4 0] [5 2 7 7 1] [9 9 7 9 7]] >>> print argmax(data) [0 3 0 3 1] >>> print argmax(data, 0) [4 4 1 4 4] >>> print argmin(data) [4 1 4 4 4] >>> print argmin(data, 0) [1 1 0 0 2]
datastring, type, shape=None) |
file, type, shape=None) |
a, b) |
>>> print a [1 2] >>> print b [10 11] # kind of like vector inner product with implicit transposition >>> print dot(a,b), dot(b,a) 32 32 >>> print a [[1 2] [5 7]] >>> print b [[ 0 1] [ 10 100]] >>> print dot(a,b) [[ 20 201] [ 70 705]] >>> print dot(b,a) [[ 5 7] [510 720]]
a, b) |
>>> print a [[0 1 2] [3 4 5]] >>> print b [1 2 3] >>> print a*b [[ 0 2 6] [ 3 8 15]] >>> print matrixmultiply(a,b) [ 8 26]
m, m_min, m_max) |
>>> a = arange(9, type=Float32) >>> print clip(a, 1.5, 7.5) [1.5 1.5 2. 3. 4. 5. 6. 7. 7.5]
shape, type=None) |
shape=(3,4)
, then the shape of the array returned will be
(2,3,4)
since the length of (3,4)
is 2 and if
shape=(5,6,7)
, the returned array's shape will be (3,5,6,7)
.
The contents of the returned arrays are such that the ith subarray
(along index 0, the first dimension) contains the indices for that axis
of the elements in the array. An example makes things clearer:
>>> i = indices((4,3)) >>> i.getshape() (2, 4, 3) >>> print i[0] [[0 0 0] [1 1 1] [2 2 2] [3 3 3]] >>> print i[1] [[0 1 2] [0 1 2] [0 1 2] [0 1 2]]
i[0]
has an array of the specified shape, and each element in
that array specifies the index of that position in the subarray for axis 0.
Similarly, each element in the subarray in i[1]
contains the index of
that position in the subarray for axis 1.
array, axis1, axis2) |
>>> x = arange(10) >>> x.setshape((5,2,1)) >>> print x [[[0] [1]] [[2] [3]] [[4] [5]] [[6] [7]] [[8] [9]]] >>> y = swapaxes(x, 0, 2) >>> y.getshape() (1, 2, 5) >>> print y [[[0 2 4 6 8] [1 3 5 7 9]]]
arrs, axis=0) |
array((a0, ..., an))
, as long as all
arrays have the same shape.
>>> print x [[ 0 1 2 3] [ 5 6 7 8] [10 11 12 13]] >>> print concatenate((x,x)) [[ 0 1 2 3] [ 5 6 7 8] [10 11 12 13] [ 0 1 2 3] [ 5 6 7 8] [10 11 12 13]] >>> print concatenate((x,x), 1) [[ 0 1 2 3 0 1 2 3] [ 5 6 7 8 5 6 7 8] [10 11 12 13 10 11 12 13]] >>> print array((x,x)) # Note: one extra dimension [[[ 0 1 2 3] [ 5 6 7 8] [10 11 12 13]] [[ 0 1 2 3] [ 5 6 7 8] [10 11 12 13]]] >>> print a [[1 2]] >>> print b [[3 4 5]] >>> print concatenate((a,b),1) [[1 2 3 4 5]] >>> print concatenate((a,b),0) ValueError: _concat array shapes must match except 1st dimension
a, b) |
matrixmultiply(a, transpose(b))
.
a,b) |
result[i, j] = a[i] * b[j]
.
a, max_line_width=None, precision=None, supress_small=None) |
a, max_line_width=None, precision=None, supress_small=None) |
>>> print a [ 1.00000000e+00 1.10000000e+00 1.11600000e+00 1.11380000e+00 1.20000000e-02 1.34560000e-04] >>> print array_str(a,precision=4,suppress_small=1) [ 1. 1.1 1.116 1.1138 0.012 0.0001] >>> print array_str(a,precision=3,suppress_small=1) [ 1. 1.1 1.116 1.114 0.012 0. ] >>> print array_str(a,precision=3) [ 1.000e+00 1.100e+00 1.116e+00 1.114e+00 1.200e-02 1.346e-04]
array, shape) |
>>> x = arange(10) >>> y = resize(x, (4,2)) # note that 4*2 < 10 >>> print x [0 1 2 3 4 5 6 7 8 9] >>> print y [[0 1] [2 3] [4 5] [6 7]] >>> print resize(array((0,1)), (5,5)) # note that 5*5 > 2 [[0 1 0 1 0] [1 0 1 0 1] [0 1 0 1 0] [1 0 1 0 1] [0 1 0 1 0]]
n, type=None) |
>>> print identity(5) [[1 0 0 0 0] [0 1 0 0 0] [0 0 1 0 0] [0 0 0 1 0] [0 0 0 0 1]]
a, axis=0) |
>>> print x [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15] [16 17 18 19]] >>> print sum(x) [40 45 50 55] # 0+4+8+12+16, 1+5+9+13+17, 2+6+10+14+18, ... >>> print sum(x, 1) [ 6 22 38 54 70] # 0+1+2+3, 4+5+6+7, 8+9+10+11, ...
a, axis=0) |
a, axis=0) |
a, axis=0) |
a, axis=0) |
a, axis=0) |
a) |
a) |
a, b, rtol=1.e-5, atol=1.e-8) |
rtol, atol
. The formula used is:
(8.1) |
See Also:
Send comments to the NumArray community.