sequence=None, typecode=None, copy=1, savespace=0, type=None, shape=None) |
>>> a = array([1.2, 3.5, -1])
>>> print a [ 1.2 3.5 -1. ]
type
is omitted, numarray tries to
find the best data type which can represent all the elements.
Since the elements we gave our example were two floats and one integer, it
chose Float64 as the type of the resulting array. One can specify
unequivocally the type
of the elements -- this is especially
useful when, for example, one wants an array contains floats even
though all of its input elements are integers:
>>> x,y,z = 1,2,3 >>> a = array([x,y,z]) # integers are enough for 1, 2 and 3 >>> print a [1 2 3] >>> a = array([x,y,z], type=Float32) # not the default type >>> print a [ 1. 2. 3.]
shape
to use for the array. When
passed a NumArray instance, by default array will make
an independent, aligned, contiguous, non-byteswapped copy. If also passed
a shape or different type, the resulting ``copy'' will be reshaped or
cast as the new type.
seq, type=None, typecode=None) |
seq, type=None, typecode=None) |
>>> mystery = array([1, 2.0, -3j])
A very common mistake is to call array with a set of numbers as
arguments, as in array(1, 2, 3, 4, 5)
. This doesn't produce the expected
result if at least two numbers are used, because the first argument to
array must be the entire data for the array -- thus, in most cases,
a sequence of numbers. The correct way to write the preceding invocation is
most likely array([1, 2, 3, 4, 5])
.
Possible values for the type argument to the array creator function (and indeed to any function which accepts a so-called type for arrays) are:
To specify a type, e.g. UInt8, etc, the easiest method is just to specify it as a string:
a = array([10], type = 'UInt8')
The various means for specifying types are defined in table 4.1, with each item in a row being equivalent. The preferred methods are in the first 3 columns: numarray type object, type string, or type code. The last two columns were added for backwards compatabililty with Numeric and are not recommended for new code. Numarray type object and string names denote the size of the type in bits. The numarray type code names denote the size of the type in bytes. The type objects must be imported from or referenced via the numerictypes module. All type strings and type codes are specified using ordinary Python strings, and hence don't require an import. Complex type names denote the size of one component, real or imaginary, in bits/bytes, but the letter code is the total size of the whole number ('c8' and 'c16').
Numarray Type | Numarray String | Numarray Code | Numeric String | Numeric Code |
Int8 | 'Int8' | 'i1' | 'Byte' | '1' |
UInt8 | 'UInt8' | 'u1' | 'UByte' | |
Int16 | 'Int16' | 'i2' | 'Short' | 's' |
UInt16 | 'UInt16' | 'u2' | 'UShort' | |
Int32 | 'Int32' | 'i4' | 'Int' | 'i' |
UInt32 | 'UInt32' | 'u4' | 'UInt' | 'u' |
Int64 | 'Int64' | 'i8' | ||
UInt644.1 | 'UInt64' | 'u8' | ||
Float32 | 'Float32' | 'f4' | 'Float' | 'f' |
Float64 | 'Float64' | 'f8' | 'Double' | 'd' |
Complex32 | 'Complex32' | 'c8' | 'F' | |
Complex64 | 'Complex64' | 'c16' | 'Complex' | 'D' |
Bool | 'Bool' |
The following example shows one way of creating multidimensional arrays:
>>> ma = array([[1,2,3],[4,5,6]]) >>> print ma [[1 2 3] [4 5 6]]
>>> ma_floats = array([[1,2,3],[4,5,6]], type=Float32) >>> print ma_floats [[ 1. 2. 3.] [ 4. 5. 6.]]
>>> print ma.shape # works only with Python 2.2 or later >>> print ma.getshape() # works with all Python versions (2, 3)
A
is always equal to len(A.getshape())
. Note that shape is an
attribute and getshape is a method of array objects. They are the
first of several that we will see throughout this tutorial. If you're not used
to object-oriented programming, you can think of attributes as ``features'' or
``qualities'' of individual arrays, and methods are functions that operate on
individual arrays. The relation between an array and its shape is similar to
the relation between a person and their hair color. In Python, it's called an
object/attribute relation.
a, shape) |
>>> flattened_ma = reshape(ma, (6,)) >>> print flattened_ma [1 2 3 4 5 6]
>>> a = array([1,2,3,4,5,6,7,8]) >>> print a [1 2 3 4 5 6 7 8] >>> b = reshape(a, (2,4)) # 2*4 == 8 >>> print b [[1 2 3 4] [5 6 7 8]] >>> c = reshape(b, (4,2)) # 4*2 == 8 >>> print c [[1 2] [3 4] [5 6] [7 8]]
One nice feature of shape tuples is that one entry in the shape tuple is allowed to be -1. The -1 will be automatically replaced by whatever number is needed to build a shape which does not change the size of the array. Thus:
>>> a = reshape(array(range(25)), (5,-1)) >>> print a, a.getshape() [[ 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]] (5, 5)
>>> a = array([1,2,3,4,5,6,7,8,9,10]) >>> a.getshape() (10,) >>> a.setshape((2,5)) >>> a.shape = (2,5) # for Python 2.2 and later >>> print a [[ 1 2 3 4 5] [ 6 7 8 9 10]] >>> a.setshape((10,1)) # second axis has length 1 >>> print a [[ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7] [ 8] [ 9] [10]] >>> a.setshape((5,-1)) # note the -1 trick described above >>> print a [[ 1 2] [ 3 4] [ 5 6] [ 7 8] [ 9 10]]
>>> a.setshape((6,-1)) Traceback (innermost last): File "<stdin>", line 1, in ? ValueError: New shape is not consistent with the old shape
Sections denoted ``For Advanced Users'' indicates function aspects which may not be needed for a first introduction of numarray, but is mentioned for the sake of completeness.
The default printing routine provided by the numarray module prints arrays as follows:
This explains why rank-1 arrays are printed from left to right, rank-2 arrays have the first dimension going down the screen and the second dimension going from left to right, etc.
If you want to change the shape of an array so that it has more elements than it started with (i.e. grow it), then you have several options: One solution is to use the concatenate function discussed later.
>>> print a [0 1 2 3 4 5 6 6 7] >>> print concatenate([[a],[a]]) >>> print b [[0 1 2 3 4 5 6 7] [0 1 2 3 4 5 6 7]] >>> print b.getshape() (2, 8)
array, shape) |
>>> base = array([0,1])
>>> big = resize(base, (9,9)) >>> print big [[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 1 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 1 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 1 0 1 0]]