# Lesson 3 - Shape Manipulation¶

The Original Tutorial may be found here.

In :
import numpy as np


## Changing the shape of an array¶

The Original Tutorial may be found here

An array has a shape given by the number of elements along each axis:

In :
a = np.floor(10*np.random.random((3,4)))

In :
a

Out:
array([[ 3.,  6.,  5.,  5.],
[ 1.,  6.,  1.,  6.],
[ 0.,  5.,  2.,  2.]])
In :
a.shape

Out:
(3L, 4L)

The shape of an array can be changed with various commands:

In :
a.ravel() # flatten the array

Out:
array([ 3.,  6.,  5.,  5.,  1.,  6.,  1.,  6.,  0.,  5.,  2.,  2.])
In :
a.shape = (6, 2)

In :
a

Out:
array([[ 3.,  6.],
[ 5.,  5.],
[ 1.,  6.],
[ 1.,  6.],
[ 0.,  5.],
[ 2.,  2.]])
In :
a.transpose()

Out:
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:

In :
a

Out:
array([[ 3.,  6.],
[ 5.,  5.],
[ 1.,  6.],
[ 1.,  6.],
[ 0.,  5.],
[ 2.,  2.]])
In :
b = a.resize((2,6))

In :
a

Out:
array([[ 3.,  6.,  5.,  5.,  1.,  6.],
[ 1.,  6.,  0.,  5.,  2.,  2.]])
In :
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:

In :
b = a.reshape(3,-1)

In :
a

Out:
array([[ 3.,  6.,  5.,  5.,  1.,  6.],
[ 1.,  6.,  0.,  5.,  2.,  2.]])
In :
b

Out:
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.

## Stacking together different arrays¶

The Original Tutorial may be found here

In :
a = np.floor(10*np.random.random((2,2)))

In :
a

Out:
array([[ 1.,  9.],
[ 7.,  7.]])
In :
b = np.floor(10*np.random.random((2,2)))

In :
b

Out:
array([[ 9.,  5.],
[ 4.,  1.]])
In :
np.vstack((a, b))

Out:
array([[ 1.,  9.],
[ 7.,  7.],
[ 9.,  5.],
[ 4.,  1.]])
In :
np.hstack((a, b))

Out:
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:

In :
np.column_stack((a,b))   # With 2D arrays

Out:
array([[ 1.,  9.,  9.,  5.],
[ 7.,  7.,  4.,  1.]])

Let's create two 1D arrays.

In :
a = np.array((4., 2.))

In :
b = np.array((2., 8.))


This is to quickly illustrate using the newaxis argument to increase a dimension for the array.

In :
a[:,np.newaxis]

Out:
array([[ 4.],
[ 2.]])
In :
b[:,np.newaxis]

Out:
array([[ 2.],
[ 8.]])

Let's column_stack them!

In :
np.column_stack((a[:, np.newaxis], b[:, np.newaxis]))

Out:
array([[ 4.,  2.],
[ 2.,  8.]])
In :
np.vstack((a[:, np.newaxis], b[:, np.newaxis]))   # The behavior of vstack is different

Out:
array([[ 4.],
[ 2.],
[ 2.],
[ 8.]])

let's try concatenate:

In :
a

Out:
array([ 4.,  2.])
In :
b

Out:
array([ 2.,  8.])
In :
np.concatenate((a, b))

Out:
array([ 4.,  2.,  2.,  8.])

Sort of similar to column stack.

let's try to create array using the r_[] and c_[]

In :
np.r_[1:4, 0, 4]

Out:
array([1, 2, 3, 0, 4])
In :
np.c_[1:4, 2:5]

Out:
array([[1, 2],
[2, 3],
[3, 4]])

# Splitting one array into serveral smaller ones¶

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:

In :
a = np.floor(10*np.random.random((2,12)))

In :
a

Out:
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.]])
In :
a_hsplit = np.hsplit(a, 3)    # Split a into 3

In :
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.]]


In :
a_vsplit = np.vsplit(a,2)   # Split a into 2

In :
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).

In :
a_as0 = np.array_split(a, 3, axis=1)

In :
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).

In :
a_as1 = np.array_split(a, 2, axis=0)

In :
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.]]



# Conclusion¶

We have now completed Lesson 3 - Shape Manipulation.