Thursday, February 28, 2019

NumPy library - 6 (Array Manipulation)

Array manipulation as the name suggests deals with creation of an array using already created arrays usually by joining or splitting of already defined arrays. Let's see how array manipulation is done using:

1. Joining Arrays

A new array can be formed by merging multiple arrays, this new array then contains all of the arrays. This is accomplished in NumPy by using stacking which provides methods to merge arrays.

a. vstack() function

In order to perform vertical stacking we use this function which combines the second array as new rows of the first array. In this case, the array grows in a vertical direction hence it is known as vertical stacking. The following program shows vertical stacking:

import numpy as np

arr1 = np.random.random(9).reshape(3,3)
arr2 = np.zeros((3, 3))
print('The first array is \n')
print(arr1)
print('\nThe second array is\n')
print(arr2)
print('\nThe  vertical stack array is\n')
print(np.vstack((arr1, arr2)))


The output of the program is shown below:

The first array is

[[0.14500418 0.61535875 0.92246045]
 [0.79186282 0.22879715 0.30451435]
 [0.30571872 0.60974898 0.93925356]]

The second array is

[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

The  vertical stack array is

[[0.14500418 0.61535875 0.92246045]
 [0.79186282 0.22879715 0.30451435]
 [0.30571872 0.60974898 0.93925356]
 [0.         0.         0.        ]
 [0.         0.         0.        ]
 [0.         0.         0.        ]]


------------------
(program exited with code: 0)

Press any key to continue . . .


b. hstack() function

In order to perform horizontal stacking we use this function which combines the second array as new columns of the first array. In this case, the array grows in a horizontal direction hence it is known as horizontal stacking. The following program shows horizontal stacking:

 import numpy as np

arr1 = np.random.random(9).reshape(3,3)
arr2 = np.zeros((3, 3))
print('The first array is \n')
print(arr1)
print('\nThe second array is\n')
print(arr2)
print('\nThe 
horizontal stack array is\n')
print(np.hstack((arr1, arr2)))


The output of the program is shown below:

The first array is

[[0.34118011 0.46964606 0.92296117]
 [0.3612867  0.49396367 0.93493638]
 [0.6506944  0.02496858 0.88195055]]

The second array is

[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

The  horizontal stack array is

[[0.34118011 0.46964606 0.92296117 0.         0.         0.        ]
 [0.3612867  0.49396367 0.93493638 0.         0.         0.        ]
 [0.6506944  0.02496858 0.88195055 0.         0.         0.        ]]


------------------
(program exited with code: 0)

Press any key to continue . . . 


c. column_stack()

This function is used with one-dimensional arrays, which are stacked as columns in order to form a new two-dimensional array. The following program shows column stacking:

import numpy as np

arr1 = np.arange(3)
arr2 = np.arange(3)
arr3 = np.arange(3)
print('The first array is \n')
print(arr1)
print('\nThe second array is\n')
print(arr2)
print('\nThe third array is\n')
print(arr3)
print('\nThe  column stack array is\n')
print(np.column_stack((arr1,arr2,arr3)))


The output of the program is shown below:

The first array is

[0 1 2]

The second array is

[0 1 2]

The third array is

[0 1 2]

The  column stack array is

[[0 0 0]
 [1 1 1]
 [2 2 2]]


------------------
(program exited with code: 0)

Press any key to continue . . .


d. column_stack()

This function is used with one-dimensional arrays, which are stacked as columns in order to form a new two-dimensional array. The following program shows column stacking: 

import numpy as np

arr1 = np.arange(3)
arr2 = np.arange(3)
arr3 = np.arange(3)
print('The first array is \n')
print(arr1)
print('\nThe second array is\n')
print(arr2)
print('\nThe third array is\n')
print(arr3)
print('\nThe  row stack array is\n')
print(np.row_stack((arr1,arr2,arr3))) 


The output of the program is shown below:

The first array is

[0 1 2]

The second array is

[0 1 2]

The third array is

[0 1 2]

The  row stack array is

[[0 1 2]
 [0 1 2]
 [0 1 2]]
------------------
(program exited with code: 0)

Press any key to continue . . . 


2. Splitting Arrays

A new array can be formed by dividingan array, this new array into several parts. This is accomplished in NumPy by using splitting which provides functions to split arrays.

a.  hsplit() function

In order to split the array horizontally, i.e the width of the array is divided into two parts this function is used. The following program shows horizontal splitting:

import numpy as np

arr1 = np.arange(16).reshape((4, 4))

print('The original array is \n')
print(arr1)

[arr2,arr3] = np.hsplit(arr1,2)

print('\nThe  horizontally split arrays are\n')
print('The first array  \n')
print(arr2)
print('\nThe second array  \n')
print(arr3)


The output of the program is shown below which  shows that the 4x4 matrix arr1 will be split into two 2x4 matrices:

The original array is

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

The  horizontally split arrays are

The first array

[[ 0  1]
 [ 4  5]
 [ 8  9]
 [12 13]]

The second array

[[ 2  3]
 [ 6  7]
 [10 11]
 [14 15]]
------------------
(program exited with code: 0)


Press any key to continue . . .

b. vsplit() function

In order to split the array vertically, i.e the height of the array is divided into two parts this function is used. The following program shows vertical splitting:

import numpy as np

arr1 = np.arange(16).reshape((4, 4))

print('The original array is \n')
print(arr1)

[arr2,arr3] = np.vsplit(arr1,2)

print('\nThe  vertically split arrays are\n')
print('The first array \n')
print(arr2)
print('\nThe second array \n')
print(arr3) 


The output of the program is shown below which  shows that the 4x4 matrix arr1 will be split into two 2x4 matrices:

The original array is

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

The  vertically split arrays are

The first array

[[0 1 2 3]
 [4 5 6 7]]

The second array

[[ 8  9 10 11]
 [12 13 14 15]]


------------------
(program exited with code: 0)

Press any key to continue . . . 


c. split() function

Using this function we can split the array into non-symmetrical parts. We pass the array as an argument and also specify the indexes of the parts to be divided. An additional argument axis is also passed, if the the option axis = 1 is used then the indexes will be columns; if instead the option is axis = 0, then they will be row indexes.

The following program divides the matrix into three parts with axis =1, the first of which will include the first column, the second will include the second and the third column, and the third will include the last column:

import numpy as np

arr1 = np.arange(16).reshape((4, 4))

print('The original array is \n')
print(arr1)

[arr2,arr3,arr4] = np.split(arr1,[1,3],axis=1)

print('\nThe splitted arrays are\n')
print('The first array \n')
print(arr2)
print('\nThe second array \n')
print(arr3)
print('\nThe second array \n')
print(arr4)


The output of the program is shown below:

The original array is

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

The splitted arrays are

The first array

[[ 0]
 [ 4]
 [ 8]
 [12]]

The second array

[[ 1  2]
 [ 5  6]
 [ 9 10]
 [13 14]]

The second array

[[ 3]
 [ 7]
 [11]
 [15]]


------------------
(program exited with code: 0)

Press any key to continue . . .


The following program divides the matrix into three parts with axis =0, the first of which will include the first row , the second will include the second and the third row, and the third will include the last row:

import numpy as np

arr1 = np.arange(16).reshape((4, 4))

print('The original array is \n')
print(arr1)

[arr2,arr3,arr4] = np.split(arr1,[1,3],axis=0)

print('\nThe splitted arrays are\n')
print('The first array \n')
print(arr2)
print('\nThe second array \n')
print(arr3)
print('\nThe second array \n')
print(arr4)


The output of the program is shown below:

The original array is

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

The splitted arrays are

The first array

[[0 1 2 3]]

The second array

[[ 4  5  6  7]
 [ 8  9 10 11]]

The second array

[[12 13 14 15]]


------------------
(program exited with code: 0)

Press any key to continue . . . 


We can also use the hsplit() and vsplit() functions to achieve this kind of splitting. The following programs implement them:

1. hsplit()

import numpy as np

arr1 = np.arange(16).reshape((4, 4))

print('The original array is \n')
print(arr1)

[arr2,arr3,arr4] = np.hsplit(arr1,[1,3])

print('\nThe splitted arrays are\n')
print('The first array \n')
print(arr2)
print('\nThe second array \n')
print(arr3)
print('\nThe second array \n')
print(arr4)


The output of the program is shown below:

The original array is

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

The splitted arrays are

The first array

[[ 0]
 [ 4]
 [ 8]
 [12]]

The second array

[[ 1  2]
 [ 5  6]
 [ 9 10]
 [13 14]]

The second array

[[ 3]
 [ 7]
 [11]
 [15]]


------------------
(program exited with code: 0)

Press any key to continue . . .


2. vsplit()

import numpy as np

arr1 = np.arange(16).reshape((4, 4))

print('The original array is \n')
print(arr1)

[arr2,arr3,arr4] = np.vsplit(arr1,[1,3])

print('\nThe splitted arrays are\n')
print('The first array \n')
print(arr2)
print('\nThe second array \n')
print(arr3)
print('\nThe second array \n')
print(arr4)


The output of the program is shown below:

The original array is

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

The splitted arrays are

The first array

[[0 1 2 3]]

The second array

[[ 4  5  6  7]
 [ 8  9 10 11]]

The second array

[[12 13 14 15]]


------------------
(program exited with code: 0)

Press any key to continue . . .



3.  Copying arrays

In order to generate a complete and distinct array using an existing array, use the copy() function as shown in the following program:

import numpy as np

arr1 = np.arange(3)

print('The original array is \n')
print(arr1)

arr2 = arr1.copy()

print('\nThe copied array is\n')
print(arr2)
arr1[0] = 7
print('\nThe first array \n')
print(arr1)
print('\nThe second array \n')
print(arr2)

The output of the program is shown below:

The original array is

[0 1 2]

The copied array is

[0 1 2]

The first array

[7 1 2]

The second array

[0 1 2]
------------------
(program exited with code: 0)

Press any key to continue . . .

In case if we assign arr1 to arr2 (arr2 = arr1) then we are not copying it and changing any element in arr1 will also change the element's value in arr2. Same holds good when we slice an array. The following program proves this:

import numpy as np

arr1 = np.arange(5)

print('The original array is \n')
print(arr1)

arr2 = arr1

print('\nThe copied array is\n')
print(arr2)

arr1[0] = 7
print('\nThe first array \n')
print(arr1)
print('\nThe second array \n')
print(arr2)

arr3 = arr1[0:1]

print('\nThe slice \n')
print(arr3)

arr1[0] = 8
print('\nThe modified first array \n')
print(arr1)

print('\nThe modified slice \n')
print(arr3)

The output of the program is shown below:

The original array is

[0 1 2 3 4]

The copied array is

[0 1 2 3 4]

The first array

[7 1 2 3 4]

The second array

[7 1 2 3 4]

The slice

[7]

The modified first array

[8 1 2 3 4]

The modified slice

[8]


------------------
(program exited with code: 0)

Press any key to continue . . .

Thus we can see that none of the NumPy assignments produces copies of arrays, nor any element contained in them whereas the copy() creates a distinct array.

Here I am ending today's post. In the next post we shall further explore NumPy library and discuss about Vectorization and broadcasting. Till we meet next keep practicing and learning Python as Python is easy to learn!









Share:

2 comments:

  1. very informative post. i would like to know also about "slicing numpy array". can you please send me the info.

    ReplyDelete
    Replies
    1. Thanks for your appreciation. You may visit NumPy library - 4 (Indexing, Slicing, and Iterating) post to learn slicing. Hope this helps!

      Delete