In the last tutorial we covered how to create different kinds of array in Numpy. In this Numpy tutorial we show different methods or functions that exists in Numpy to manipulate elements and change the shape and size of array. These are basic to any computational and mathematical work on array/ matrices. Since there are so many methods in Numpy only the most fundamental and mostly used methods have been covered here.

Here are some of the methods or functions that are frequently required and used in matrix or array manipulation.
  • reshape( )
  • ravel( )
  • flatten( )
  • shape( )
  • indexing and slicing
  • transpose( )
  • resize( )
reshape( )

Consider we create one dimensional array using the arange( ) function.

# a = arange(6)

The elements are as shown-

# a
> array([0, 1, 2, 3, 4, 5])

The reshape( ) function of Numpy changes the shape of the given array. If we want for example to change the one dimentional array a to an array of 2 rows and 3 columns we do the following.

# a.reshape(2,3)
>
array([[0, 1, 2],
       [3, 4, 5]])


That is we apply the reshape method to the ndarray object a.

Similarly.

# a.reshape(3,2)
>
array([[0, 1],
       [2, 3],
       [4, 5]])

ravel( )

The ravel( ) function is to restore or un-entangle an array. It is like opposite operation to reshape( ) For example in the previous example we used reshape( ) to change one dimensional array a to 2 by 3 and 3 by 2 dimensional array b. We can use the ravel( ) to deduce back the one dimensional array from the array b. This is illustrated below.

We have the matrix/array b as follows:

# b
>
array([[0, 1, 2],
       [3, 4, 5]])

Now we apply ravel( ) method to array b and assign to another array object c.

# c = b.ravel

# c
-----------------------> c()
> array([0, 1, 2, 3, 4, 5])

Now array c is one dimensional like array a.

flatten( )

flatten( ) is same as ravel( ) which reduces multi-dimensional array to one dimensional form, but the difference is flatten( ) allocates new memory location to the resulting array whereas ravel( ) may keep the memory location reversed.

# b
>
array([[0, 1, 2],
       [3, 4, 5]])

# d = b.flatten()

# d
> array([0, 1, 2, 3, 4, 5])

shape( )

shape( ) is used to find out the size of an array and for changing the shape of an array. It is not the opposite of reshape( ) in that it does not restore the array changed by reshape( ). Consider we have 3 by identity matrix then we can use the shape to find its size which is (3,3) in tuple form.

# I = eye(3, dtype = int32)

# I
>
array([[1, 0, 0],
       [0, 1, 0],
       [0, 0, 1]])

# I.shape
> (3, 3)

shape( ) is also used change the shape of a given array. Consider the array b.

# b
>
array([[0, 1, 2],
       [3, 4, 5]])

By applying the shape method to the ndarray object b and assigning to the required shape we wish to transform to we can change the existing shape of b.

# b.shape = (3,2)


# b
>
array([[0, 1],
       [2, 3],
       [4, 5]])


indexing and slicing

Indexing and slicing works in the same way as with list in regular python programming. We use the square bracket [ ] to access any array element or access part of the array. The later is called slicing.

Consider the array a:

# a
> array([0, 1, 2, 3, 4, 5])

We can access each element using the square bracket and specifying which element we wish to know about.

# a[0]
> 0

# a[1]
> 1

# a[4]
> 4

We can extract more than one element of an array using the colon within the square bracket. This operation is called slicing.

# a[0:3]
> array([0, 1, 2])

# a[3:4]
> array([3])

Multidimentional array indexing

We can get or extract elements of multi-dimensional using the indexing operation of the form array([row,col]).

Consider we have an array a

# a
> array([0, 1, 2, 3, 4, 5])

Let's create another matrix b by reshaping this to a 2x3 matrix:

# b = a.reshape(2,3)

# b
>
array([[0, 1, 2],
       [3, 4, 5]])

Now we can access the elements of array b in Numpy as follows:

# b[1,2]
> 5

# b[0,2]
> 2

# b[1,2]
> 5

Now we show how to access the row and column of array b in Numpy:

The row and column have the syntax like the following:

b[row, column] and colon : means all

Consider the same b array/matrix:

# b
>
array([[0, 1, 2],
       [3, 4, 5]])

So in order to extract the first row do the following

# b[0,:]
> array([0, 1, 2])



Similarly for extracting the second row,

# b[1,:]
> array([3, 4, 5])

Extracting the first column follow,

# b[:,0]
> array([0, 3])

Similarly for the second column and third column:

# b[:,1]
> array([1, 4])

# b[:,2]
> array([2, 5])


transpose( )

transpose( ) takes the mathematical transpose of given array/matrix. Consider the array b and applying the transpose method to this b object:

# b
>
array([[0, 1],
       [2, 3],
       [4, 5]])

# b.transpose
----------------------------------> b.transpose()
>
array([[0, 2, 4],
       [1, 3, 5]])


resize( )

resize( ) works like reshape( ) which creates new array from the specified array. The size of the new array to be constructured must be specified.


# b
>
array([[0, 1],
       [2, 3],
       [4, 5]])

# b.resize(2,3)

# b
>
array([[0, 1, 2],
       [3, 4, 5]])

But resize( ) has other feature which is not available in reshape( ). resize( ) can change the shape of given array to any size with the elements either filled by zero or filled by repeating the original elements.

# resize(b,(6,6))
>
array([[0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5]])



In this basic Numpy tutorial we showed you how to work with different ndarray object methods to modify and manipulate arrays or matrices in Numpy.

Next we will cover additional array operation techniques called stacking and splitting which are often required in working with arrays/ matrices.

0 comments:

Post a Comment

 
Top