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() can't work. 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. The use of array methods is limited to functionality which depends critically on the implementation details of array objects. Array methods are discussed in the next chapter. We've already covered two functions which operate on arrays, reshape and resize.
a, indices, axis=0) |
>>> print a [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19]] >>> print take(a, (0,)) # first row [ [0 1 2 3 4]] >>> print take(a, (0,1)) # first and second row [[0 1 2 3 4] [5 6 7 8 9]] >>> print take(a, (0,-1)) # out-of-range index [[0 1 2 3 4] [0 1 2 3 4]]
>>> print take(a, (0,), axis=1) # first column [[ 0] [ 5] [10] [15]] >>> print take(a, (0,1), axis=1) # first and second column [[ 0 1] [ 5 6] [10 11] [15 16]] >>> print take(a, (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). I mention it here because later in this tutorial, we will see functions which have 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]]
>>> table = (255 - arange(256)**2 / 256).astype(UInt8)
>>> m51b = take(table, m51)
a, indices, values) |
>>> 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 len(ind): a.flat[i] = v[i]
a, 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]
.
a, axes=None) |
>>> print a [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19]] >>> print transpose(a) [[ 0 5 10 15] [ 1 6 11 16] [ 2 7 12 17] [ 3 8 13 18] [ 4 9 14 19]]
a, repeats, axis=0) |
>>> print a [[0 1 2] [3 4 5]] >>> print repeat(a, 2*ones(a.shape[0])) [[0 1 2] [0 1 2] [3 4 5] [3 4 5]] >>> print repeat(a, 2*ones(a.shape[1]), 1) [[0 0 1 1 2 2] [3 3 4 4 5 5]]
a, (b0, ..., bn)) |
b0 ... bn
as indicated by the value of the corresponding
element in a. Assume a 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 (ie. 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))
a) |
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]),)
condition, x, y) |
>>> 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 nonzero 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])
condition, a, axis=0) |
>>> print x [0 1 2 3] >>> print greater(x, 2) [0 0 0 1] >>> print compress(greater(x, 2), x) [3]
a, offset=0, axis1=0, axis2 = 1) |
>>> 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) |
>>> 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
bins, 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.3029573 0.79585496 0.82714031 0.77993884 0.55069605 0.76043182 0.28511823 0.29987358 0.40286206 0.68617903] >>> print searchsorted(bin_boundaries, data) [4 8 9 8 6 8 3 3 5 7]
>>> def histogram(a, bins): ... 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]
a, axis=-1) |
sort(a, 3)
will be an array of the same shape as
a, where the elements of a 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]]
a, axis=-1) |
sort(a)
. In other words, for a rank-1 array, take(a,
argsort(a)) == sort(a)
.
>>> 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]
a, axis=-1) |
a, 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 2 0] >>> print argmax(data, 0) [0 4 1 1 4] >>> print argmin(data) [4 0 4 4 2] >>> print argmin(data, 0) [1 1 0 0 0]
string, type, shape=None) |
file, type, shape=None) |
m1, m2) |
m1, m2) |
>>> 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) |
(3,4)
, then the shape of the array returned will be
(2,3,4)
since the length of (3,4)
is 2. 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.
a, 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) >>> print y.getshape() (1, 2, 5) >>> print y [[[0 2 4 6 8] [1 3 5 7 9]]]
(a0, a1, ... , an), 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)) [[[ 0 1 2 3] [ 5 6 7 8] [10 11 12 13]] [[ 0 1 2 3] [ 5 6 7 8] [10 11 12 13]]]
a, b) |
matrixmultiply(a, transpose(b))
.
a,b) |
result[i, j] = a[i] * b[j]
.
) |
) |
a, new_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]]
a, counts, axis=0) |
len(a.getshape()[axis])
. In
one dimension this is straightforward:
>>> y array([0, 1, 2, 3, 4, 5]) >>> repeat(y, (1,2,0,2,2,3)) array([0, 1, 1, 3, 3, 4, 4, 5, 5, 5])
>>> x array([[0, 1, 2], [3, 4, 5]]) >>> repeat(x, (2,6)) array([[0, 1, 2], [0, 1, 2], [3, 4, 5], [3, 4, 5], [3, 4, 5], [3, 4, 5], [3, 4, 5], [3, 4, 5]]) >>> repeat(x, (6,3,2), 1) array([[0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2], [3, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5]])
n) |
>>> 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) |
x, y, rtol = 1.e-5, atol = 1.e-8) |
![]() |
(7.1) |
See Also:
Send comments to the NumArray community.