Completion of Machine Learning Course

I feel extremely happy that I’m nearing the end of this course that introduced me to the amazing world of Machine Learning and the countkess possibilities it has to offer.
Since my last update on the Machine Learning course, I have explored Unsupervised learning algorithms in the course. Previously, I had written about the various Supervised Learning algorithms and techniques that had been taught to us. In this post, I wish to make a note of the Unsupervised Learning and specialized learning techniques I implemented, namely Clustering (K-Means), PCA, Anomaly Detection and Recommender systems : –

Continue reading

Using Counters in Python

Python never ceases to amaze me! I came across yet another amazing trick in Python which is not very well known but can be extremely useful for programmers.
I dunno how many of you are familiar with using Counter class, but I found it really interesting.

A Counter is basically a { Key : Value} pair type of a map and when a string is passed onto it, it denotes frequencies of all the letters occuring in a string. I wrote a simple python script to implement the Counter class, which I encourage you try running on your machine as well.

Counters1.py  Counters1_Output

First we have to import the module Counter from collections and then we pass any string to it.
Counter(Some_string) returns to us the frequency of each occuring letter in the string, as you can see in the output. This is extremely useful for a number of applications, one of the most spontaneous use of this trick could be in coding text compression and cryptographic algorithms like Huffman, Run Length Encoding where we have to otherwise run for loops and use arrays to find out this frequency of each letter.
This simple magic of Python saves us a lot of hassles.

One other application I would like to mention is that of checking if two strings are anagrams (a word with same number of letters, but in a re-arranged manner).
Check out the following code:-

Counters2.py   Counters2_OP

The first string, string1 is my name ‘Divyansh’ and in the second string, string2 I rearranged the letters in my name. When I check for the equality of the two strings, I get a false (obviously!), but when I compare their Counter, I’m basically comparing the frequency of each letter in the two strings, which is what the check for an anagam is. Hence, my script prints True in case of an anagram.

Do play with Counter by using your own name, or any other string. I’ll edit this post when I find more uses of the Counter class.

– jigsaw

Use of if __name__ == “__main__”: in Python

When I first came across the code if __name__ == “__main_” in a python script I was reading, I was utterly confused!
I had thought that python didnt have any kind of a main function, hence the confusion. But after hacking around a bit, when I found the need for this statement, it felt like a genius implementation.

I will try and explain in this blog post, the need to include the statement if  __name__ ==  “__main__”: is some modules.

The main keyword included in Python is not like the main method implemented in other programming languages like C and C++. The execution of a python script implicitly starts from the top of the script, and the interpreter reads the script line by line in a top-down fashion.
While reading a Python file, the interpreter also defines some special variables. It sets the variable __name__ to equal to __main__ while the file (or module) is being executed independently, and sets it to the name of the module, if the module has been imported in some other script and that script is being executed.

To furnish an example which will dispell all doubts, try running the follwing code in your machine:-
I built two separate python scripts, by the name of one.py and two.py.

one       two.py

Continue reading

Brainstorming implementation details for Machine Learning project

I spent the last night brainstorming with a friend, to come up with the best approach for my project on Machine Learning.

Our Objective : We have decided to apply machine learning techniques to an opportunistic network routing protocol, so that the nodes in the network (which is void of any previously defined architecture), learns the best next hop to foward the message to, based on the previously selected neighbour nodes.

The project idea seems really innovative and we didn’t find any significant learning algortithms previously applied in the field of networking to train a network. So we were really excited about this venture of ours.
But we have run into a few roadbloacks!

First of all, its getting extremely difficult to find out time for everything with the fear of placements looming over our heads, so it’s been almost 3 weeks after mid-sems that we were able to start work on the ML project.

Initially, we had thought of applying a Supervided Learning algorithm for this problem, but then we quickly realised that it is almost impossible to get, or even generate the kind of data which we would need t go ahead with a supervised learning approach.

Continue reading

Progress on Learning Python

As of this moment I have completed 38 exercises of Learn Python the Hard Way and there’s only a little more to go.
I hope I’m able to complete it in the next 2 days.

Now I have learnt and implemented most basics in Python and have successfully hacked around in Python for a lot of hours. I can start building small programs and self-projects in python.

Since the last time I learnt  the implementation of if-elif-else and looping through if and while loops. I also used more and more of functions in my code, and also learnt a lot of new functions which are incredibly easy to implement. This is what makes Python a beautiful language! And the more I learn things about Python, the more comfortable I feel with it.

Next I have to learn about python dictionaries and object oriented programming in python. Since I have a fair bit of background in programming, it wouldn’t be much of a problem for me to grasp the implementation in python.

Also, I pushed all my updates, modifications and new files made onto github. I also found a site called pythonchallenge.com  which introduced a really interesting way for us to learn python. You should try it.

Hope to form a firm grip on python programming concepts by the next week.

– jigsaw

Difference between sort() and sorted() function in Python

Initially i was confused as to what is the difference between sort() and sorted(), but with a little bit of hacking and playing around with code, I was able to remove my confusion. I’m also attaching the code I used as a part of my operations on lists. In this blog post, i’ll deconstruct sort() and sorted() functions.
The most basic difference is, list_name.sort() function works on the list in place and it physically sorts the elements of the list, while sorted(list_name) function simply returns the sorted form of the list without actually physically sorting the list.
(Excuse me for not using any technical jargons here).

Try running this fun script with Python.
Try running this fun script with Python.

Continue reading

Difference between add . | add * | add -u | add -A/add–all

In this post I wish to document the correct and concise difference between these 4 types of git command to add files to stage them for a commit to your online repository.

1. git add . v/s git add * 

add * adds all the files in the current directory, and stages them for a commit, except the ones that begin with a dot. Files beginning with a . are hidden for add *n.
On the other hand, git add . will add all the files, folders, sub-folders to the staging area along with the ones that begin with a ‘.’ .
I also read that * is not a part of git. It is a wildcard, interpreted by the shell. * expands to all the files in the current directory. However, add . refers to the current directory itself and git add . will add all the files in that directory, including the files beginning with a dot.
http://stackoverflow.com/questions/16969768/what-does-git-add-git-add-single-dot-command-do
http://stackoverflow.com/questions/26042390/git-add-asterisk-vs-git-add-period

NOTE: Both add * and add . don’t add the removed files for commiting.

2. git add *  v/s  git add -A/–all  v/s  git add -u

YfLUZ

This table depicts graphically the difference between these 3 implements of git add.

add . or add * indexes all the new and modified files and stages them for a commit, except for the files that have been removed.

add -u stages all modified and removed files for a commit, except for the new files created. It does not add any new files, it only stages changes to the files that have been tracked before.

add -A or add –all is a combination of both add . & add -u. It will add all new files, modified files and removed files, for staging.

http://stackoverflow.com/questions/572549/difference-between-git-add-a-and-git-add
Hope I have been able to make the difference between these 4 kinds of add commands clear.

– jigsaw

Documenting the Computer Networking Research Project

I started working on this research project on Computer Networking in my 5th semester. I was highly motivated to do something technical, and at that moment Computer Networks piqued my curiousity. So I delved into a research project on Opportunistic and Delay Tolerant networks along with a friend of mine. The idea was to devise a new network routing protocol.
We initially started with our preliminary readings, to get idea about the subject. We,

1. Read the following books- Computer Networking by Tanenbaum and Data Communications & Networking by Forouzan.

2. Watched all the NPTEL lectures on Networking.

3. Studied and analysed popular research papers published in the field of Computer Networking like Epidemic, Prophet, Spray and wait, and other research papers published in the field of Computer Networking.

We quickly hit upon an idea to implement Machine Learning on Opportunistic Networks, so that we could make our networking protocol learn on the basis of features, that which node is the next best forwarder for the message.

In December, keeping this on the side, we branched off to devise a simpler protocol so that we can an idea of the ONE (Opportunistic Environment Simulator) before we get down to implement our Machine Learning based Networking protocol.

The concise summary of our protocols is as follows:-

Continue reading

Making progress on Machine Learning

I had started a course on Machine Learning on Coursera by Stanford University in February. As of now I have completed more than half of the course (up till Week 6) and I have to start with SVM’s next. I wanted to document my progress up till now and mention in brief, the things that I have learnt in ML since.

First of all, I’d like to state that I have signed up for this course two times before this and ended up leaving it in between due to my semester exams and lack of determination. I had never made it past week 3. So I guess the fact that I have reached up till week 6 this time guarantees the fact that I have strengthened my will power in this regard, since (there were mid semester exams in between this time too, but I managed doing my programming exercises well before the deadlines).

<rant> Implementing algorithms in Octave really irks me out! Its like, so many times that I have understood the concepts crystal clear but when i end up implementing it in Octave, it is so shitty that it saps all the fun out of it. I have actually spent hours racking my brains to figure out the right code, when the issue has only been a damn parenthesis or some stupid bullshit. This had led me to doubt my interest in the course a few times. I don’t want to let that happen further. </rant>

Continue reading

Creating git repository for the Python exercises

I feel so happy after creating my first repository on github and successfully adding all the files to it from my local machine!
It took me a lot of time and effort, and there were actually a few times when I thought that I wouldn’t be able to do it. It took me hours, and that is not how much it should have taken to just create a new repository.

I had earlier tried to create a repository using SSH clone and I wasn’t successful. Even this repository I have created is a HTTPS clone. I am yet to master a SSH clone repository. I get lost in the process after adding the SSH keys.

So here I’d like to document the whole procedure I used to create the repository (using HTTPS clone) and the problems I faced mid-way :-

First I went on my github profile and simply created a new repository with the name- Learn Python the Hard Way. This part is too easy and requires no real explanation.
Then I opened my terminal and followed the following procedure:-

1. Initialized the local repository (the directory where I hold all the python exercises) as a git repository.
jigsaw:Python$ git init

Initialized empty Git repository in /home/jigsaw/Python/.git/

2. Added the files from in the new local repository. This stages them for a commit to my git repository.
jigsaw:Python$ git add .

Note: At the end, I’ll also mention the difference between add . and add -A/–all

3. Checked  the status of the files staged for a commit.
jigsaw:Python$  git status

On branch master

Initial commit

Changes to be committed:
  (use “git rm –cached …” to unstage)

new file:   Python File Modes
new file:   Python Files I_O.pdf
new file:   Untitled Document 1.py~
new file:   ex1.py
new file:   ex1.py~
new file:   ex10.py
new file:   ex10.py~
and so on..

4. Committed the files that I have staged to my git repository.
jigsaw:Python$ git commit -m “Required message”

[master (root-commit) e0954d9] First commit; Half way through Learn Python the Hard Way

 51 files changed, 3488 insertions(+)
and so on..

5. Next I copied the HTTPS clone URL from the lower right hand corner of my repository on github and added it to the command as follows.

jigsaw:Python$ git remote add origin https://github.com/jigsaw2212/Learn-Python-The-Hard-Way.git

From my understanding, here we mean to create a remote (called origin )on our machine that links us to the repo on git through the given URL. I could be wrong.
6. The last step was simple. I just had to push the changes from my local repo ( on my system) to github, using :
jigsaw:Python$ git push origin master 
‘master’ to my git repository.
Just had to type my username and password here and I was done!

Username for ‘https://github.com&#8217;: jigsaw2212

Password for ‘https://jigsaw2212@github.com&#8217;: 

But I encountered an error message, which said :-


To https://github.com/jigsaw2212/Learn-Python-The-Hard-Way.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to ‘https://github.com/jigsaw2212/Learn-Python-The-Hard-Way.git&#8217;
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.

I was confused! I looked around, banged my head on the wall a few times and finally understood that what this error message was trying to tell me is that my local repo was not up-to-date with my master on github. This happened because I had added a README.md file upon creation of my repository on github, which made it out of sync with my local repo.

http://stackoverflow.com/questions/20939648/issue-pushing-new-code-in-github

Hence, I first had to pull changes from the git repo to my local machine before pushing my whole project on to github.

jigsaw:Python$ git pull
jigsaw:Python$ git push origin master

And through this I was finally able to push all the scripts I had written in the exercises on to a new repository on github.
Link to repo- https://github.com/jigsaw2212/Learn-Python-The-Hard-Way

After that, I also created a branch for my project ( so that the master remains clean), using :-
jigsaw:Python$ git checkout -b LPTHW_Next_part
Means, create a new branch and move to it.

I can view the existing branches and the current branch I’m on using:-
jigsaw:Python$ git branch

* LPTHW_Next_part

  master

Next time, I’ll try create using the SSH clone, so that I don’t have to log in again and again when I’m pushing files into it.
-jigsaw

Continue reading