Rotate

You don’t learn cool things everyday. And today is not everyday, learnt this neat trick to rotate a matrix with a one liner in #python!!!

Rotate_A_matrix_in_python

b = np.array(zip(*a[::-1]))

Simple and powerful. Waiting for a chance to use it somewhere in actual code…

Want to understand what’s happening visit this page by Peter Norvig, director of research Google!!

Guided by Theory

Theory vs PracticalIn one of the talks Charlie Munger says this…..

I could see that I was not going to cope as well as I wished with life unless I could acquire a better theory-structure on which to hang my observations and experiences. By then, my craving for more theory had a long history. Partly, I had always loved theory as an aid in puzzle solving and as a means of satisfying my monkey-like-curiosity. And, partly, I had found that theory-structure was a superpower in helping one get what one wanted. As I had early discovered in school wherein I had excelled without labor, guided by theory, while many others, without mastery of theory failed despite monstrous effort. Better theory I thought had always worked for me and, if now available could make me acquire capital and independence faster and better assist everything I loved.

I have been a fan of action, but now gravitating towards theory!!

Why Choose Python now revealed?

WhyChoosePythonI am playing with reveal.js for a week now. Reveal is html presentation framework. Its awesome. Love the html presentations!!

Moreover Powerpoint!. Ipython notebook and reveal.js are my new presentation tool.

During this week, this particular post “Why Choose Python?” is getting lot of attention, so decided to try reveal on this post…

so here’s the result.

Click on the title below to watch the presentation. Created entirely with ipython notebook!!

Why Choose Python – the presentation

The awesomeness of reveal.js only available on modern browsers (Chrome, Firefox)… IE users, please upgrade.. :)

Fun Experiments with Markov Chain

independancedayA stochastic process is a mathematical model that evolves over time in a probabilistic manner. Markov chain is a special kind of stochastic process, where the outcome of the experiment depends only on the outcome of the previous experiment. That is the next state of the system depends only on the present state, not on preceding states. Markov chains are named after the Russian mathematician A. A. Markov who stated the theory of stochastic processes.

Now what if we mix markov chain and PM Modi’s independence day speech, find the hilarious results in this ipython notebook.

Markov Chain and Modi’s Speech

After this experiment, I know how some of the comment spam is being generated and is fooling the comment filters on wordpress.

Exploring AirWire with Pandas – A Dashboard from Session Histroy

airwire internet usage analyticsLast week received my internet bill and my wife wondered how much data we are using and if someone was snooping at our wifi?

Assured her, this isn’t the case but her doubt lingered.

Went to the ISP website, downloaded the session data and showed her. Still not convinced, so imported the data into pandas.

And that exploration.. produced the above PDF. A dashboard of information mined from the csv file. My wife loved it.

Here’s the script that will produce this pdf. Will be directly useful to anyone using airwire services and for other’s it’s a good starting point.

Continue reading

Little Experiments with BeautifulSoup

cricketwithball
Look at any conference related to python, web scraping is one of the topic that is always there.

This prompted me to BeautifulSoup. I am toying with it for few months now, but yesterday got a real chance to use it.

Here’s a small function that I used yesterday to scrape out all the test match and one day match results from espn website. Someone needed the data.


import urllib2
from bs4 import BeautifulSoup
import csv
def cricketScrap(url,f):
    page = urllib2.urlopen(url).read()
    soup = BeautifulSoup(page)
    table = soup.find('table', attrs={'class': 'engineTable'})
    x = (len(table.findAll('tr')) )
    if x==1:
        return
    
    for row in table.findAll('tr')[1:x]:
        col = row.findAll('td')
        team1 = col[0].getText()
        team2 = col[1].getText()
        winner = col[2].getText()
        margin = col[3].getText()
        ground = col[4].getText()
        match_date = col[5].getText()
        match = (team1, team2, winner, margin, ground, match_date )
        f.writerow(match)

Here’s how to use it.

'''
For one day match

'''

lis=["Team 1","Team 2","Winner","Margin","Ground","Match Date"]
f = csv.writer(open("oneindiancricketscrape.csv", "w"))
f.writerow(lis)

onedayurl=r"http://stats.espncricinfo.com/indian-premier-league-2014/engine/records/team/match_results.html?class=2;team=6;type=year;id="
for year in range(1974,2015):
    cricketScrap(onedayurl+str(year),f)


And here’s the interesting one, scape all results of the matches played between 1772 to 2014


allmatchurl=r"http://stats.espncricinfo.com/indian-premier-league-2014/engine/records/team/match_results.html?class=0;team=0;type=year;id="

lis=["Team 1","Team 2","Winner","Margin","Ground","Match Date"]
f = csv.writer(open("allCricketMatchResults1900.csv", "w"))
f.writerow(lis)


for year in range(1772,2015):
    cricketScrap(allmatchurl+str(year),f)


And if you are just interested in one day and test matches results played by india till now, then click the following links to download the csv files.

Quotes – When thought and feelings compete………

quotes

Here are some quotes and text collected over last few months from books, blogs and online article…

  • When thought and feelings compete, feelings almost always win….
  • One subscriber is worth 1,000 surfers. Newspapers learned this a century ago –Seth Godin
  • “Would you rather be the world’s greatest lover, but have everyone think you’re the world’s worst lover? Or would you rather be the world’s worst lover but have everyone think you’re the world’s greatest lover?’ Now, that’s an interesting question. – Warren Buffet
  • “I learned that it pays to hang around with people better than you are, because you will float upward a little bit. And if you hang around with people that behave worse than you , pretty soon you’ll start sliding down the pole. It just works that way.” – Warren Buffet
  • “You are neither right nor wrong because the crowd disagrees with you. You are right because your data and reasoning are right. “ Ben Graham advise to his students
  • “What’s easy, won’t last. What lasts, won’t be easy.”
  • If we make mistakes let them be because we are too fast rather than too slow -Lou Gerstner’s
  • Everything worthwhile has an origin, but those origins aren’t the reason that they are worthwhile – Seth Godin
  • To be prepared against surprise is to be trained. To be prepared for surprise is to be educated. -James Carse
  • When you’re playing catch with your six-year-old, you’re not trying to win. You throw the ball in a way she can catch it. You encourage her. The game is to play, not to win.-Seth Godin
  • “Life loves the liver of it. You must live and life will be good to you, give you experiences.” — Mary Angelou
  • Sharing a book is (almost) as good as writing one. – Seth Godin
  • “By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third, by experience, which is the bitterest.”—Confucius
  • “If you are immune to boredom, there is literally nothing you cannot accomplish.” — David Foster Wallace

If you liked this, you will enjoy this, this, this and this.

Follow

Get every new post delivered to your Inbox.

Join 973 other followers

%d bloggers like this: