Sunday, 9 August 2015

Play Python - Charts- Assignment 1

Let's learn how to use graphs and charts in Python. The first question is: Why do we need graphs and charts?? The answer- to visualize the data. Humans can understand the patterns and trends much more easily when presented with a graph or chart rather than dealing with the raw numbers.



Let's start with the graphs and charts in python.

code

Here is the simple code used to plot a graph of 7 numbers.
output







The output of the above code.












Let's try  with a different problem. The one I used in my previous blog. The problem is on radish survey from open tech school.

Here we have to plot a bar graph of the different variety of radishes present in the dataset.


code

I have used my previous code to load the data and count the votes for each variety of radishes.


Merged the code for the graph with it.

I have used two modules - 

One is "pyplot". It is used wit Matplotlib to plot the graph.
The other one is NumPy which  lots of numeric functions for Python.
Then I have used loop to process the dictionary into a format which is readable for matplotlib.

The next step is indexing of the X values in the graph. It is done with the help of the  dictionary "count", where each entry is read. It helps to spread the graph evenly across the X-axis.


The last step is to plot the graph. plot.xtics()  is used to to add labels to the X-axis like, the names of the varities of the radishes e.g. "White Icicle".
bar chart




So, from the chart, we can summarize all our findings easily without even looking at the raw numbers of the output. 












This is all what I have learnt about charts in python till now and will continue to explore and keep on learning.















Play Python - Assignment 1

Let's take a look at a versatile and high-level programming language, Python. Python is designed to be a highly readable language. It is designed to have an uncluttered visual layout, frequently using English keywords. Python has a smaller number of syntactic exceptions and special cases than most of the other languages. The beauty of Python acts as it's disadvantage too - The indentation. If we do not indent our code properly, it will throw errors. Let's jump into the basic programs of Python and check how it works.

For basic python tutorials, please visit open tech school. Even in this blog I have used problems from the same source.

The Problem Statement: Suppose you're a greengrocer, and you run a survey to see what radish varieties your customers prefer the most. You have your assistant type up the survey results into a text file on your computer, so you have 300 lines of survey data in the file radishsurvey.txt. Each line consists of a name, a hyphen, then a radish variety.


Dataset

We have to find out - 
  • Which radish variety got the most votes (most popular)?
  • What are the least popular radish varieties (got the least votes)?
  • Did anyone vote twice?






Solution:
Code
 At first we created a dictionary to know the varieties of all the radishes and count their vote in a single pass.

Next , read the data from external file( .txt in this case).

Then we split each lines in two parts- names (the persons to voted) and vote(the name of the radishes) by splitting them across the "-".

To calculate the count of the radishes, we had to further process the data to remove any extra spaces and to avoid the repetition of a single variety of radish to to different cases. So, we converted the entries into upper case and marched ahead.

The next step is pretty simple, where we had to count the votes for each variety of radishes. If the variety is present in the dictionary, then +1 is added to the same, if not, a new entry is made and it's count starts from 1.


Output
The last step is to print the vote count of  the different varieties of radishes. I used pprint() instead of print() because it increases the readability.

Note: The output is in alphabetical order - One of the benefits of pprint().

The "Champion" got the highest votes: 76.

We can also get the solution of the second problem from the output.

The "Red King" variety has got the least votes.






Now we need to find if anyone voted more than once or not.


Code


This one is much simpler than the above problem.


Here, we processed the data to eliminate any extra spaces.




Then, used an if statement to check if the name of the voter is already in the "voted" dictionary, the name will be printed as already voted.










This is a small program to give you an idea how Python works, it's syntax and indentation. The next blog will be again on python and I will try to demonstrate how to use charts (at basic level).