Monday, December 30, 2013

How to compute the most frequently occuring element in a list in python

In python there is a module named "collections" which has some specialized functions that can be used as an alternative to dict, list, etc.

There is a tool called Counter which performs operations like tallying the number of occurrences of objects in a list, etc. Here is how you can make use of this to compute the mode or most frequently occurring element in a list.

%First import the Counter tool from the collections module
>>> from collections import Counter

%Built a counter for a list. This list can be of numbers or strings. For example, if I consider a list of numbers
>>> info = Counter([1, 2, 1, 1, 1, 2])

%To know the frequencies of each element, you can call the most_common function with no arguments. This will return a list of elements attached with their frequencies in descending order.
>>>  info.most_common()
[(1, 4), (2, 2)]

%If you want to know what is the top element with highest frequency, you can send in the argument '1'
>>> info.most_common(1)
[(1, 4)]             %Here 1 is the element and 4 is the frequency of '1'

You can access these elements like this:
>>> info.most_common(1)[0][0]
1
>>> info.most_common(1)[0][1]
4

%If you want to know the top 2 elements with their frequency, you can send argument '2'
>>> info.most_common(2)
[(1, 4), (2, 2)]

To compute the size of the unique elements, you can say:
>>> len(info)
2

Similar operations on a list of strings:

>>> info = Counter(["a","b","c","a","b","c","a","a"])

>>> info.most_common()
[('a', 4), ('c', 2), ('b', 2)]
>>> info.most_common(1)
[('a', 4)]
>>> info.most_common(2)
[('a', 4), ('c', 2)]
>>> info.most_common(3)
[('a', 4), ('c', 2), ('b', 2)]

%The interesting thing is, even if you pass the argument which is greater than the length of the counter, it won't give any segmentation fault. So, it is suggested to put a check on the length of the counter.

>>> info.most_common(4)
[('a', 4), ('c', 2), ('b', 2)]

>>> len(info)
3

Hope this is useful.

Monday, December 16, 2013

Inspiring email by Prof. Alexander Coward

One of my colleagues shared this link with me which has the full transcript of Prof. Alexander Coward's email. Personally, it really encouraged me to share this with others who want to personally get more motivated as I was after reading through this email.

Dear All,

As some of you may have heard, there is some strike activity taking place on campus tomorrow.

I want to let you know that I will not be striking, which means that I will be, so-to-speak, crossing a picket line. Moreover, I know that two of your GSIs have decided to strike, but because I happen to be free in the afternoon when they teach, and because I enjoy teaching smaller classes from time to time and I haven’t had a chance to in a while, I’ll be covering those sections. If you were planning to see me at office hours tomorrow afternoon, then feel free to come to one of the sections I’ll be covering. I will be in Stephens 230c from 2:10 to 4pm, Cory Hall 285 from 4:10pm to 5pm, and Evans Hall 6 from 5:10pm-6pm.
The reason for me taking this decision is extremely simple: We have 7 class days left until the end of the course. Despite the fact that we’ve made good time and are likely to finish the syllabus with a few lectures in hand for review, class hours are valuable and your education is too important to just cancel a class if we don’t have to. Whatever the alleged injustices are that are being protested about tomorrow, it is clear that you are not responsible for those things, whatever they are, and I do not think you should be denied an education because of someone else’s fight that you are not responsible for. I say this with no disrespect whatsoever to the two GSIs who have decided to strike. Societies where people stand up for what they believe in are generally better than societies where people do not, sometimes dramatically so. Further, I cannot discount the possibility that I may be in the wrong on this and they may be right. I have certainly been on the wrong side of political judgements before and I’m sure I will be again. However from a practical point of view I’ve made my decision and you should all turn up to class and discussion tomorrow as normal.
Beyond practical matters, I think it’s also worth reflecting a little on the broader relationship between politics and your education, and I think I have some important things to share on this topic that may be helpful to you.

I do this with some trepidation. Normally I try to avoid talking about politics with my students and also my professional colleagues because people have a wide variety of views, sometimes held with great conviction and feeling. If I was to get into a political disagreement with one of you or one of my colleagues, it might get in the way of or distract us from the central mission we have of working together to give you a great education.

However sometimes political events reach into our lives without our invitation or control, and we have no choice but to engage with each other about politics. Many times in history it has done so with far more violence and disruption than a strike, and it is wise to be psychologically prepared for this fact.
If I’ve learned one thing about politics since I was your age, it is this: Politics, like most things in life worth thinking about, including mathematics, is very big, very complicated, and very interconnected. I’ve lived and worked in four countries on four continents, all with societies set up differently both politically and socially. I’ve discovered that there is no unique or obviously best way of setting up society. For every decision and judgement you reach, there are people who benefit and people who lose out. It’s the same with the way I teach my classes. I know that for every decision I make about how to teach you there are some of you who benefit and there are others who would do better if I did things differently. There is no way of getting around that. Every judgement you make in life is a question of balancing different interests and ideals. Reasonable good people can disagree on political questions like whether to strike or not, and they can disagree about far more contentious topics also.

All this may sound like speaking in platitudes. However it is a point worth making to all of you because you are so young. One of the nice things about being young is that your thinking can be very clear and your mind not so cluttered up with memories and experiences. This clarity can give you a lot of conviction, but it can also lead you astray because you might not yet appreciate just how complicated the world is. As you get older you tend to accumulate life experiences to learn from, and this is the source of wisdom, but the trouble is that the lessons we glean from life do not all point in the same direction. Sometimes it is hard to tease the correct learning from the experiences life throws at us.

So what are we to do with the fact that when we are young we lack a lot of the perspective we need to make definitive judgements about what is right, but that as we get older our judgements tend to be informed by our experiences, and these experiences guide us in contradictory ways, both between different people and within the same person?

I don’t know.

However one thing I do know is that you are not going to be able to avoid making these kinds of judgements, just as I cannot avoid making a judgment about whether to strike or not. Like it or not, I have to make a political choice, and I have to talk to you about it. For me, the choice not to strike is quite easy, but for you the kinds of judgements and choices you are going to face in your lives are going to be far from easy; they are going to be of a complexity and importance that will rival that faced by any previous generation. To an extent that you may not yet appreciate, the world is changing incredibly quickly. In just a decade, since I was your age, the internet and telecommunications has truly transformed the way we live, not just in rich countries but around the world. When I was an undergraduate, if I wanted to check my email I went to a little room in the basement to use a computer, and if I wanted to learn something I went to a library. The kinds of breakthroughs we are seeing in biotechnology remind me of the way people were talking about electricity in 1900. Of course I don’t know - nobody knows - but my guess is that biotechnology in the 21st century could be similarly transformative to the way the full power of electricity only hit prime-time in the 20th century. The recent controversy about the NSA has shown that the role of information technology on society can be, or at least might become, double edged. There is climate change, another controversial and difficult topic, the exact impact of which we do not yet know. These are just a few of the challenges we can see, and we should remember that history has a habit of throwing curve balls at each generation that nobody saw coming. And among all this tumult, our search for common human peace and happiness on some level becomes more difficult, though no less important. A previous generation dodged the bullet of nuclear armageddon when things looked bleak, but for your generation the bullets are coming thicker and faster than ever before. The potential all of you in your generation are going to have for both good and harm is tremendous.

I suspect many of you have heard sentiments along these lines before. However I also suspect that many of you will think something in response along the lines of `I know all that, but these things are for someone else to figure out, not me.’
That is a mistake.

One of the things you can lose track of when you attend a top tier university like Berkeley is just how exceptional and amazing you really are. I’m blown away every time I talk to you. The way you ask penetrating questions, the way you improved so much between midterm 1 and 2, the way you challenge me to be a better teacher, it just knocks my socks off. You really are amazing. I’ve taught students all over the world, and I’ve never seen a group of students so talented. I’m not just talking about some of you. I’m talking about all of you. It’s a privilege to be your professor. Sadly, however, I know many of you don’t feel that way. The difficulty you all face is that as you look around at all your fellow students, it’s easy to have your eye drawn by people doing better than you. Or rather, I should say people who look like they’re doing better than you. In reality the true extent of how much people are learning can be difficult to measure. Sometimes failures and adversity are better preparations for long term success than effortless progress.

Why am I telling you all this?

I’m telling you this because you all need to know that there is not some great pool of amazing people in some other place who are going to shape the way our species navigates the coming decades. The simple fact is that, like it or not, technology is going to change the way we live in the future, and you’re going to have to solve some very hard problems, as well as figure out how best to use new technology for good, while at the same time facing human dangers that have haunted humanity throughout history.

Part of the work of your generation is going to be technological, using scientific ideas to serve the interests of society, and part of the work is going to be fundamentally human, tied inexorably with qualities of the human condition - human emotion - that dominate the whole of history. These things are not separate, but are inexorably linked, and you are in a better place to understand that connection than me.

I can’t tell you what your particular role should be in the new realities of the 21st century. It’s up to you to decide if you want to make the focus of your life technological, focused on new innovations to drive society forward, or essentially human, focused on the age-old struggles of trying to get along, work together, and find happiness, or some combination of the two.

However I can tell you this:

Whatever you decide to do with your life, it’s going to be really, really complicated.

Science and technology is complicated. History and politics is complicated. People are complicated. Figuring out how to be happy, and do simple things like take care of our kids and maintain friendships and relationships, is complicated.
In order for you to navigate the increasing complexity of the 21st century you need a world-class education, and thankfully you have an opportunity to get one. I don’t just mean the education you get in class, but I mean the education you get in everything you do, every book you read, every conversation you have, every thought you think.

You need to optimize your life for learning.

You need to live and breath your education.

You need to be *obsessed* with your education.

Do not fall into the trap of thinking that because you are surrounded by so many dazzlingly smart fellow students that means you’re no good. Nothing could be further from the truth.

And do not fall into the trap of thinking that you focusing on your education is a selfish thing. It’s not a selfish thing. It’s the most noble thing you could do.
Society is investing in you so that you can help solve the many challenges we are going to face in the coming decades, from profound technological challenges to helping people with the age old search for human happiness and meaning. 
That is why I am not canceling class tomorrow. Your education is really really important, not just to you, but in a far broader and wider reaching way than I think any of you have yet to fully appreciate. 

See you tomorrow,

 Alexander

Taken from: http://alumni.berkeley.edu/california-magazine/just-in/2013-11-21/cal-lecturers-email-students-goes-viral-why-i-am-not

Tuesday, November 12, 2013

How to import functions in another file to your current file in python

If you are working on a code (sample.py) that uses functions of another file (simpleops.py) in the same directory, you can simply call "import simpleops" and then use the functions in that file as "simpleops.()" as shown in the below example.

Example: 

Your current program (sample.py) :

#!/usr/bin/env python
import simpleops

print simpleops.sum(5,3)
print simpleops.mult(5,3)



If the another file is in different directory compared to your current file, then you should first import its directory path and then import the file like: "sys.path.insert(0, FILEPATH) import simpleops" as shown in the below example. Note that the value '0' in sys.path.insert means it first searches the modules in the current directory first from the program has been called. 

Example: 

Your current program (sample.py):

#!/usr/bin/env python
import sys
sys.path.insert(0, FILEPATH)
import simpleops

print simpleops.sum(5,3)
print simpleops.mult(5,3)



For example lets say, your sample code (simpleops.py) looks like this:

#!/usr/bin/env python

def mult(a,b):
        return(a*b)

def sum(a,b):
        return(a+b)


Monday, November 11, 2013

How to install maven on linux?

Step-1: Download the maven binary package from http://maven.apache.org/download.cgi

Step-2: Extract the tar file: tar -xvzf apache-maven-3.1.1-bin.tar.gz

Step-3: Create a folder in /usr/local with the name apache-maven and extract the above tar file in this directory which gives the file structure as: /usr/local/apache-maven/apache-maven-3.1.1

Step-4: Set your path variables as below:
export M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1
export M2=$M2_HOME/bin
export PATH=$M2:$PATH

Step-5: To check if maven is successfully installed: mvn --version


Monday, October 7, 2013

How to create new user on linux for mysql

First login as 'root' on your linux machine.

Go to mysql terminal and use these commands to create a new user.

mysql> create user ''@'localhost' identified by '';

mysql> grant all privileges on  * . * to ''@'localhost';

mysql> flush privileges;

Now go back to your console and login using the created username and password using this command:

$mysql -u -p

Thats all!! And you are good to use this username and password and work on databases.

Friday, September 20, 2013

How to install updates from console on linux

There are two options to install updates.

1. Using yum
2. GUI

It is very simple if you have already yum installed on your machine.

$ yum update

It should automatically install all the updates of different software on your machine. 

Using GUI, it is very simple process. Just follow the instructions on GUI.

Wednesday, September 18, 2013

Problem with tagger and chunkers of NLTK on linux

I installed NLTK using the commands which were found here: http://nltk.org/install.html

Everything got installed successfully. So when I started using this nltk package like this:

python
>>> import nltk
>>> text = nltk.word_tokenize("And now for something completely different")
>>> text
['And', 'now', 'for', 'something', 'completely', 'different']

But when I try to tag these tokens, there are some errors as shown below.

>>>  nltk.pos_tag(text)
  File "", line 1
    nltk.pos_tag(text)
    ^
IndentationError: unexpected indent
>>> nltk.pos_tag(text)
Traceback (most recent call last):
  File "", line 1, in
  File "/usr/lib/python2.6/site-packages/nltk/tag/__init__.py", line 99, in pos_tag
    tagger = load(_POS_TAGGER)
  File "/usr/lib/python2.6/site-packages/nltk/data.py", line 605, in load
    resource_val = pickle.load(_open(resource_url))
  File "/usr/lib/python2.6/site-packages/nltk/data.py", line 686, in _open
    return find(path).open()
  File "/usr/lib/python2.6/site-packages/nltk/data.py", line 467, in find
    raise LookupError(resource_not_found)
LookupError:
**********************************************************************
  Resource 'taggers/maxent_treebank_pos_tagger/english.pickle' not
  found.  Please use the NLTK Downloader to obtain the resource:
  >>> nltk.download()
  Searched in:
    - '/root/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
**********************************************************************


The way you can fix it is: Run this command which downloads the tagger:
>>> nltk.download('maxent_treebank_pos_tagger')

It downloads this package to some directory on your machine. For me it downloaded here: /root/nltk_data...

And all the system related files with regard to NLTK are in this directory: /usr/lib/python2.6/site-packages/nltk
So, I created a new directory called 'taggers' and copy this 'maxent_treebank_pos_tagger' to this new directory named: /usr/lib/python2.6//site-packages/nltk/taggers/

Now when you run this command:
>>> tagged = nltk.pos_tag(tokens)
[('At', 'IN'), ('eight', 'CD'), ("o'clock", 'JJ'), ('on', 'IN'), ('Thursday', 'NNP'), ('morning', 'NN')]

Similarly when you try to do this:
>>> entities = nltk.chunk.ne_chunk(tagged)
You will see similar results that you have to download chunker and corpora, follow the similar procedure and execute these two commands:

>>> nltk.download('maxent_ne_chunker')
>>> nltk.download('words')

This can help you install the chunkers package and when you execute this command, you can get the parse tree
>>> entities = nltk.chunk.ne_chunk(tagged)
>>> entities
Tree('S', [('At', 'IN'), ('eight', 'CD'), ("o'clock", 'JJ'), ('on', 'IN'), ('Thursday', 'NNP'), ('morning', 'NN'), Tree('PERSON', [('Arthur', 'NNP')]), ('did', 'VBD'), ("n't", 'RB'), ('feel', 'VB'), ('very', 'RB'), ('good', 'JJ'), ('.', '.')])

Hope this helps!


Thursday, September 5, 2013

Install/add network printer to your system list in linux through command line

Initially check if CUPS (Common Unix Printing System) daemon is running or not using this command:

$$ /etc/init.d/cups status

You will see something like this on your terminal:

$$ cupsd (pid ****) is running...

If the daemon is not running, then start the daemon using these commands:

$$ /etc/init.d/cups start
$$ chkconfig cups on

To add the network printer, use this command:

$$ lpadmin -p Printer_name -E -v socket://


To set a default printer:

$$ lpadmin -d  Printer_name

To check the printer status and see if printer is in ready mode:

$$ lpq

 You see something like this when your printer is ready and there are no entries:

$$ Printer_name  is ready
$$ no entries

To check the printers installed on your machine, you can use this command:

$$ lpstat -p -d

If you want to delete a printer on your machine:

$$ lpadmin -x Printer_name




**part of this has been taken from http://sumitgoel.me

Wednesday, June 26, 2013

Applying for F-1 US visa from India

This post is intended to share my experience with those who are applying for F-1 visa to pursue education in the United States of America. Please note that this post is based on my OWN personal experience ONLY. Please follow the US-traveldocs (http://www.ustraveldocs.com/in/) website for the correct information.

There are many good websites online which gives a very detailed information about applying to F-1 visa. Here is how I applied for the visa.

Step-1: Receive i-20 from the university of your choice

Step-2: Pay SEVIS fee online (save this payment confirmation slip)

Step-3: Fill up DS-160 form online (My visa interview is in Hyderabad. So, no need for me to upload my photograph online. I went to OFC for a separate photo and fingerprint collection.)

Step-4: Take appointment for OFC and visa interview on ustraveldocs website

Step-5: At OFC: Take these documents along with you: passport, appointment confirmation, DS-160 confirmation. (Good to take i-20 as well but not required. I was not asked to show i-20)

You will be given a token number and asked to be seated. When your token number shows up, they will take your photograph and collect your finger prints (all fingers). They may ask you why you are going to the States.

Step-6: At the US consulate for visa interview: Take all the required documents which may include: passport, appointment confirmation, DS-160, SEVIS receipt, i-20, admission letter, GRE scorecard, TOEFL scorecard, funding letters (if no funding then bank loan documents that support you financially), transcripts, degree certificates, etc. And proceed for the interview.

Good luck for all the aspirants who want to pursue education in the US!

Tuesday, May 28, 2013

How to plot multiple graphs (scatter plots or histograms) on a same window in R?

Recently I have been working with R and bumped into different interesting facts of R. Here is how you can plot multiple graphs in the same window.

Your variable is x is a dependent variable on independent variables a, b, c and d. For example,

 a <- rnorm(100)
 b <- rnorm(100)
 c <- rnorm(100)
 d <- rnorm(100)


x <- a+b+c+d

If you want to understand how x is dependent upon each of these variables, you can make a scatter plot. If you want to see all these four dependency graphs on a same plot, you can use layout function in R.

Execute these commands..

M <- matrix(c(1:4), byrow=TRUE, nrow=2)
> layout(M)
> plot(x, a)
> plot(x, b)
> plot(x, c)
> plot(x, d)

Its that easy!

If you want to plot two histograms on a same plot, heres how you can do!


> hist1 <- hist(rnorm(500,3))
> hist2 <- hist(rnorm(500,5)) #500 random numbers centered around 5
> plot( hist1, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
> plot( hist2, col=rgb(1,0,0,1/4), xlim=c(0,10), add=T)  # second histogram