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.





Thursday 19 March 2015

A simple plot using input from a text file

I am continuing from my last blog on using matplotlib for creating charts. Here is another simple example, but this time reading data from a text file.

I have created a text file as seen below in the same directory where my python scripts are saved. Since I am using anaconda they are usually saved in the documents folder.


I have not included any heading to the columns in my input file, my intention is to just create a list and plot them. I have just included X values in first column and Y values in the second. I am just going to transpose them and assign them to a single variable and plot them.

I think this can be better put in a scatter plot, so I am using same matplotlib library as in my previous blog and numpy for loading text and using transpose options.

The code can be written as seen below.





This would give and output like this when executed.
































Again this can be extended to create more sensible and useful visualizations.

Wednesday 18 March 2015

Creating a simple chart in Python

Python is an excellent tool for data analysis and visualization. To start with I have written a simple (and a very common to start with code) to plot a very basic chart.

I am using Anaconda Python, as this is more easy and comes with all the modules already. In case you want to install some module in Anaconda Python, simply go the the Anaconda command prompt and type in the following command.

conda install 'module name', for example to install 'bokeh' you can write a command conda install bokeh. This will install the all the required stuff in 'bokeh'. ('bokeh' is an interactive visualization library in python and an effective one!). But I am using matplotlib library here.


This gives an output like this

This can be extended to format the chart and draw more advanced charts.

Tuesday 17 March 2015

Python Program for Permutations and Combinations

Permutations and Combinations in Python:

While choosing 'r' number of items from 'n' objects there are two scenarios that are of concern. 

Firstly, we may want to choose 'r' items from 'n' objects and we do not care about the order in which they choose. 

This is called Permutation and there are two more things associated with permutation that it can be done with repetition or without repetition.

Secondly, we may want to choose 'r' items in certain order from 'n' objects.

This is called Combination and again there are two things associated with combination that it can be done with repetition and without repetition.

Here in this simple program below, I have written functions for all four scenarios above. This just takes the 'n' and 'r' as inputs from the user and prints off the results of all the four scenarios.



And, you will be able see an output like this when this code is executed.



*I am not sure, if there is a module that already deals with these functions. But atleast I hope this could be a bit useful for beginners.

Friday 13 March 2015

How to install a package in Python in Windows 7?

Installing Python packages in windows 7

I spent a considerable time while trying to install packages in python. I got various answers for installing them but most of them did not help. So I thought I could share how to install a python package in windows 7 environment.

This is a very basic step but I would like to start from here. Go to the command prompt and select the path where your Python program files are saved. In my case they are saved in here as below.


Then just enter this,
                          python -m pip install "the package you want to install"

so for example I wanted to install django-excel package, so I did as below to install that. You can see the command in the 4th line that installs this package.


But I would suggest using Anaconda python where most of the modules comes with that and would save time installing the modules required.