In: a Out: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) In: hsplit(a, 3) Out: [array([[0], [3], [6]]), array([[1], [4], [7]]), array([[2], [5], [8]])]
Compare it with a call of the split
function, with extra parameter axis=1
:
In: split(a, 3, axis=1) Out: [array([[0], [3], [6]]), array([[1], [4], [7]]), array([[2], [5], [8]])]
vsplit
function splits along the vertical axis:In: vsplit(a, 3) Out: [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7,8]])]
The split
function, with axis=0
, also splits along the vertical axis:
In: split(a, 3, axis=0) Out: [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7,8]])]
dsplit
function, unsurprisingly, splits depth-wise. We will need an array of rank three first:In: c = arange(27).reshape(3, 3, 3) In: c Out: 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, 25, 26]]]) In: dsplit(c, 3) Out: [array([[[ 0], [ 3], [ 6]], [[ 9], [12], [15]], [[18], [21], [24]]]), array([[[ 1], [ 4], [ 7]], [[10], [13], [16]], [[19], [22], [25]]]), array([[[ 2], [ 5], [ 8]], [[11], [14], [17]], [[20], [23], [26]]])]
Besides the
shape
and dtype
attributes, ndarray
has a number of other attributes, as shown in the following list:
ndim
attribute gives the number of dimensions:In: b Out: 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]]) In: b.ndim Out: 2
size
attribute contains the number of elements. This is shown a follows:In: b.size Out: 24
itemsize
attribute gives the number of bytes for each element in the array:In: b.itemsize Out: 8
nbytes
. This is just a product of the itemsize
and size
attributes:In: b.nbytes Out: 192 In: b.size * b.itemsize Out: 192
T
attribute has the same effect as the transpose
function, which is shown as follows:In: b.resize(6,4) In: b Out: 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]]) In: b.T Out: array([[ 0, 4, 8, 12, 16, 20], [ 1, 5, 9, 13, 17, 21], [ 2, 6, 10, 14, 18, 22], [ 3, 7, 11, 15, 19, 23]])
In: b.ndim Out: 1 In: b.T Out: array([0, 1, 2, 3, 4])
j
. For example, we can create an array with complex numbers:In: b = array([1.j + 1, 2.j + 3]) In: b Out: array([ 1.+1.j, 3.+2.j])
real
attribute gives us the real part of the array, or the array itself if it only contains real numbers:In: b.real Out: array([ 1., 3.])
imag
attribute contains the imaginary part of the array:In: b.imag Out: array([ 1., 2.])
In: b.dtype Out: dtype('complex128') In: b.dtype.str Out: '<c16'
flat
attribute returns a numpy.flatiter
object. This is the only way to acquire a flatiter
—we do not have access to a flatiter
constructor. The flat iterator enables us to loop through an array as if it is a flat array, as shown next:In: b = arange(4).reshape(2,2) In: b Out: array([[0, 1], [2, 3]]) In: f = b.flat In: f Out: <numpy.flatiter object at 0x103013e00> In: for item in f: print item .....: 0 1 2 3
It is possible to directly get an element with the flatiter
object:
In: b.flat[2] Out: 2
Or multiple elements:
In: b.flat[[1,3]] Out: array([1, 3])
The flat
attribute is settable. Setting the value of the flat
attribute leads to overwriting the values of the whole array:
In: b.flat = 7 In: b Out: array([[7, 7], [7, 7]]) or selected elements In: b.flat[[1,3]] = 1 In: b Out: array([[7, 1], [7, 1]])