The Original Tutorial may be found here.
import numpy as np
The Original Tutorial may be found here
An array has a shape given by the number of elements along each axis:
a = np.floor(10*np.random.random((3,4)))
a
array([[ 3., 6., 5., 5.], [ 1., 6., 1., 6.], [ 0., 5., 2., 2.]])
a.shape
(3L, 4L)
The shape of an array can be changed with various commands:
a.ravel() # flatten the array
array([ 3., 6., 5., 5., 1., 6., 1., 6., 0., 5., 2., 2.])
a.shape = (6, 2)
a
array([[ 3., 6.], [ 5., 5.], [ 1., 6.], [ 1., 6.], [ 0., 5.], [ 2., 2.]])
a.transpose()
array([[ 3., 5., 1., 1., 0., 2.], [ 6., 5., 6., 6., 5., 2.]])
The reshape
(non-mutative) method returns its argument with a modified shape, whereas the resize
(mutative) method modifies the array itself:
a
array([[ 3., 6.], [ 5., 5.], [ 1., 6.], [ 1., 6.], [ 0., 5.], [ 2., 2.]])
b = a.resize((2,6))
a
array([[ 3., 6., 5., 5., 1., 6.], [ 1., 6., 0., 5., 2., 2.]])
b
Note that the resize
method has mutated a
. It does not return anything. It just mutate (a side effect).
If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:
b = a.reshape(3,-1)
a
array([[ 3., 6., 5., 5., 1., 6.], [ 1., 6., 0., 5., 2., 2.]])
b
array([[ 3., 6., 5., 5.], [ 1., 6., 1., 6.], [ 0., 5., 2., 2.]])
Note that the reshape
method does not mutate a
. It merely return the reshaped array.
The Original Tutorial may be found here
a = np.floor(10*np.random.random((2,2)))
a
array([[ 1., 9.], [ 7., 7.]])
b = np.floor(10*np.random.random((2,2)))
b
array([[ 9., 5.], [ 4., 1.]])
np.vstack((a, b))
array([[ 1., 9.], [ 7., 7.], [ 9., 5.], [ 4., 1.]])
np.hstack((a, b))
array([[ 1., 9., 9., 5.], [ 7., 7., 4., 1.]])
The function column_stack
stacks 1D arrays as columns into a 2D array. It is equivalent to vstack
only for 1D arrays:
np.column_stack((a,b)) # With 2D arrays
array([[ 1., 9., 9., 5.], [ 7., 7., 4., 1.]])
Let's create two 1D arrays.
a = np.array((4., 2.))
b = np.array((2., 8.))
This is to quickly illustrate using the newaxis
argument to increase a dimension for the array.
a[:,np.newaxis]
array([[ 4.], [ 2.]])
b[:,np.newaxis]
array([[ 2.], [ 8.]])
Let's column_stack
them!
np.column_stack((a[:, np.newaxis], b[:, np.newaxis]))
array([[ 4., 2.], [ 2., 8.]])
np.vstack((a[:, np.newaxis], b[:, np.newaxis])) # The behavior of vstack is different
array([[ 4.], [ 2.], [ 2.], [ 8.]])
let's try concatenate
:
a
array([ 4., 2.])
b
array([ 2., 8.])
np.concatenate((a, b))
array([ 4., 2., 2., 8.])
Sort of similar to column stack.
let's try to create array using the r_[]
and c_[]
np.r_[1:4, 0, 4]
array([1, 2, 3, 0, 4])
np.c_[1:4, 2:5]
array([[1, 2], [2, 3], [3, 4]])
Using hsplit
, you can split an array along its horizontal axis, either by specifying the number of equally shaped arrays to return, or by specifying the columns after which the division should occur:
a = np.floor(10*np.random.random((2,12)))
a
array([[ 9., 9., 4., 3., 4., 5., 3., 8., 0., 6., 6., 8.], [ 7., 8., 9., 7., 1., 6., 8., 6., 5., 2., 6., 7.]])
a_hsplit = np.hsplit(a, 3) # Split a into 3
for i in a_hsplit:
print i
print ""
[[ 9. 9. 4. 3.] [ 7. 8. 9. 7.]] [[ 4. 5. 3. 8.] [ 1. 6. 8. 6.]] [[ 0. 6. 6. 8.] [ 5. 2. 6. 7.]]
a_vsplit = np.vsplit(a,2) # Split a into 2
for i in a_vsplit:
print i
print
[[ 9. 9. 4. 3. 4. 5. 3. 8. 0. 6. 6. 8.]] [[ 7. 8. 9. 7. 1. 6. 8. 6. 5. 2. 6. 7.]]
(Recall axis 0 is out-most). Split along axis 1 (column).
a_as0 = np.array_split(a, 3, axis=1)
for i in a_as0:
print i
print ""
[[ 9. 9. 4. 3.] [ 7. 8. 9. 7.]] [[ 4. 5. 3. 8.] [ 1. 6. 8. 6.]] [[ 0. 6. 6. 8.] [ 5. 2. 6. 7.]]
(Recall axis 0 is out-most). Split along axis 0 (row).
a_as1 = np.array_split(a, 2, axis=0)
for i in a_as1:
print i
print
[[ 9. 9. 4. 3. 4. 5. 3. 8. 0. 6. 6. 8.]] [[ 7. 8. 9. 7. 1. 6. 8. 6. 5. 2. 6. 7.]]
We have now completed Lesson 3 - Shape Manipulation.