Wednesday 25 March 2015

Introducing Array in numpy

One of the interesting stuff in Python is 'numpy'. It is one of the very useful packages available in Python. Among other uses numpy is basically helpful in dealing with multi-dimensional arrays. Here I am trying to explain some of the basic operations and uses of numpy.

Let me start with defining a simple array and move onto explaining more options available in numpy.

We can create a simple one dimensional array as below, which can (only) be done in the standard Python library as well.





This creates a one dimensional array with these integer values and assigned to Arr1.

If I want to know about the dimension of this array, I would use Arr1.ndim. Similarly we can use .shape, .size, .dtype.name to know the attributes of the array.

A quick thing to note, In numpy dimensions are called axes and the number of axes is rank.

I have continued to write a simple code on defining an array and get details about the array as below.


You should have noticed the way the two dimensional array is defined.

You can find in the code I have used .ndim to print the dimension of the array, .dtype.name to print the data type of the values, similarly shape and size. These are some basic stuff to be known in numpy, but it's vast usage in python is extensive and I will try to cover them in further blogs.

So, finally, when excecuted this code will give an output like this.





No comments:

Post a Comment