As we discussed at the beginning of the last chapter, there are very few array methods for good reasons, and these all depend on the implementation details. They're worth knowing, though.
axis=-1) |
>>> array([1,2,4,3]).argmax() 2 >>> arange(100, shape=(10,10)).argmax() array([9, 9, 9, 9, 9, 9, 9, 9, 9, 9])
axis=-1) |
axis=-1) |
>>> array([1,2,4,3]).argsort() array([0, 1, 3, 2]) >>> take([1,2,4,3], argsort([1,2,4,3])) array([1, 2, 3, 4])
type) |
>>> arange(5).astype('Float64') array([ 0., 1., 2., 3., 4.])
) |
>>> print a [1 2 3] >>> a.byteswap() >>> print a [16777216 33554432 50331648]
) |
>>> array([1,2,3]).byteswapped() array([16777216, 33554432, 50331648])
) |
>>> (arange(3) + 1j).conjugate() array([ 0.-1.j, 1.-1.j, 2.-1.j])
) |
>>> c = a[3:8:2].copy() >>> print c.iscontiguous() 1
) |
>>> arange(25,shape=(5,5)).diagonal() array([ 0, 6, 12, 18, 24])
) |
>>> arange(10).info() class: <class 'numarray.numarraycore.NumArray'> shape: (10,) strides: (4,) byteoffset: 0 bytestride: 4 itemsize: 4 aligned: 1 contiguous: 1 data: <memory at 0x08931d18 with size:0x00000028 held by object 0x3ff91bd8 aliasing object 0x00000000> byteorder: little byteswap: 0 type: Int32
) |
) |
) |
>>> a=arange(25, shape=(5,5)) >>> a array([[ 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]]) >>> a.iscontiguous() 1
) |
>>> a=arange(25, shape=(5,5)) >>> a.is_c_array() 1 >>> a.is_f_array() 0
) |
>>> a=arange(25, shape=(5,5)) >>> a.transpose() >>> 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.iscontiguous() 0 >>> a.is_fortran_contiguous() 1
) |
>>> a=arange(25, shape=(5,5)) >>> a.transpose() >>> a.is_f_array() 1 >>> a.is_c_array() 0
) |
>>> a = arange(10) >>> a.itemsize() 4 >>> a = array([1.0]) >>> a.itemsize() 8 >>> a = array([1], type=Complex64) >>> a.itemsize() 16
) |
>>> arange(100, shape=(10,10)).max() 99
) |
>>> arange(100).nelements() 100
type=None) |
axis=-1) |
>>> arange(5).nonzero() (array([1, 2, 3, 4]),) >>> b = arange(9, shape=(3,3)) % 2; b array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) >>>b.nonzero() (array([0, 1, 1, 2]), array([1, 0, 2, 1]))
r, axis=0) |
>>> a=arange(25, shape=(5,5)) >>> a array([[ 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]]) >>> a.repeat(arange(5)%2*2) array([[ 5, 6, 7, 8, 9], [ 5, 6, 7, 8, 9], [15, 16, 17, 18, 19], [15, 16, 17, 18, 19]])
shape) |
>>> a = array([0, 1, 2, 3]) >>> a.resize(10) array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1])
) |
>>> arange(100).size() 100
) |
>>> a = array([1,2,3]) >>> a.type() Int32 >>> a = array([1], type=Complex64) >>> a.type() Complex64
) |
>>> a = array([1,2,3]) >>> a.typecode() 'l' >>> a = array([1], type=Complex64) >>> a.typecode() 'D'
file) |
>>> a = arange(65,100) >>> a.tofile('test.dat') # writes a's binary data to file 'test.dat'. >>> f = open('test2.dat', 'w') >>> a.tofile(f) # writes a's binary data to file 'test2.dat'
) |
>>> print a [[65 66 67 68 69 70 71] [72 73 74 75 76 77 78] [79 80 81 82 83 84 85] [86 87 88 89 90 91 92] [93 94 95 96 97 98 99]] >>> print a.tolist() [[65, 66, 67, 68, 69, 70, 71], [72, 73, 74, 75, 76, 77, 78], [79, 80, 81, 82, 83, 84, 85], [86, 87, 88, 89, 90, 91, 92], [93, 94, 95, 96, 97, 98, 99]]
) |
>>> a = arange(65,70) >>> a.tostring() 'A\x00\x00\x00B\x00\x00\x00C\x00\x00\x00D\x00\x00\x00E\x00\x00\x00'
axis=-1) |
>>> a = arange(9, shape=(3,3)) >>> a.transpose() >>> a array([[0, 3, 6], [1, 4, 7], [2, 5, 8]])
) |
>>> arange(10).stddev() 3.0276503540974917
axis1, axis2) |
>>> a = arange(25, shape=(5,5)) >>> a.swapaxes(0,1) >>> 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 = arange(4) >>> a.togglebyteorder() >>> a array([ 0, 16777216, 33554432, 50331648])
) |
>>> a = arange(25, shape=(5,5)) >>> a.trace() 60
) |
>>> a = arange(4) >>> b = a.view() >>> b.shape = (2,2) >>> a array([0, 1, 2, 3]) >>> b array([[0, 1], [2, 3]]) >>> a is b False >>> a._data is b._data True
When using Python 2.2 or later, there are four public attributes which correspond to those of Numeric type objects. These are shape, flat, real, and imag (or imaginary). The following methods are used to implement and provide an alternative to using these attributes.
) |
) |
>>> a = arange(12) >>> a.setshape((3,4)) >>> print a.getshape() (3, 4) >>> print a [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]
) |
>>> print a [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] >>> print a.getflat() [ 0 1 2 3 4 5 6 7 8 9 10 11]
) |
) |
) |
) |
) |
) |
Send comments to the NumArray community.