Author: Anirudh

  • Statistical Learning – 2016

    Statistical Learning – 2016

    On January 12, 2016, Stanford University professors Trevor Hastie and Rob Tibshirani will offer the 3rd iteration of Statistical Learning, a MOOC which first began in January 2014, and has become quite a popular course among data scientists. It is a great place to learn statistical learning (machine learning) methods using the R programming language. For a quick course on R, check this out – Introduction to R Programming

    Slides and videos for Statistical Learning MOOC by Hastie and Tibshirani available separately here. Slides and video tutorials related to this book by Abass Al Sharif can be downloaded here.

    The course covers the following book which is available for free as a PDF copy.

    Logistics and Effort:

    statLearnEffort

    Rough Outline of Schedule (based on last year’s course offering):

    Week 1: Introduction and Overview of Statistical Learning (Chapters 1-2)
    Week 2: Linear Regression (Chapter 3)
    Week 3: Classification (Chapter 4)
    Week 4: Resampling Methods (Chapter 5)
    Week 5: Linear Model Selection and Regularization (Chapter 6)
    Week 6: Moving Beyond Linearity (Chapter 7)
    Week 7: Tree-based Methods (Chapter 8)
    Week 8: Support Vector Machines (Chapter 9)
    Week 9: Unsupervised Learning (Chapter 10)

    Prerequisites: First courses in statistics, linear algebra, and computing.

     

  • Sherlock and the Beast – HackerRank

    Sherlock and the Beast – HackerRank

    I found myself stuck on this problem recently. I must confess, I lost a couple of hours trying to get to figure the logic for this one. Here’s the problem:

    sherlockAndTheBeast

    I’ve written 2 functions to solve this problem. The first one I used for smaller N, say N < 30 and the second one for N > 30. The second function is elegant, and it relies on the mathematical property that if a number N is not divisible by 3, it could either leave a remainder 1 or 2.

    If it leaves a remainder 2, then subtracting 5 once would make the number divisible by 3. If it leaves a remainder 1, then subtracting 5 twice would make the number divisible by 3.

    We subtract 5 from N iteratively and attempt to divide N into 2 parts, one divisible by 3 and the other divisible by 5. We want the part that is divisible by 3 to be the larger part, so that the associated Decent Number is the largest possible. This explanation might seem obtuse, but if you get pen down on paper, you’ll understand what I mean.

    Solution

    sherlockAndTheBeastSol

  • Supplementary Material to Andrew Ng’s Machine Learning MOOC

    Supplementary Material to Andrew Ng’s Machine Learning MOOC

    Although the lecture videos and lecture notes from Andrew Ng‘s Coursera MOOC are sufficient for the online version of the course, if you’re interested in more mathematical stuff or want to be challenged further, you can go through the following notes and problem sets from CS 229, a 10-week course that he teaches at Stanford (which also happens to be the most enrolled course on campus). It’s not hard to end up with a 100% score on his MOOC which is obviously a (much) watered down version of the course he teaches at Stanford, at least in terms of difficulty. If you don’t believe me, just have a go at the problem sets from the links below.

    Lecture Notes

    Section Notes

    Handouts and Problem Sets

  • Solutions to Machine Learning Programming Assignments

    Solutions to Machine Learning Programming Assignments

    This post contains links to a bunch of code that I have written to complete Andrew Ng’s famous machine learning course which includes several interesting machine learning problems that needed to be solved using the Octave / Matlab programming language. I’m not sure I’d ever be programming in Octave after this course, but learning Octave just so that I could complete this course seemed worth the time and effort. I would usually work on the programming assignments on Sundays and spend several hours coding in Octave, telling myself that I would later replicate the exercises in Python.

    If you’ve taken this course and found some of the assignments hard to complete, I think it might not hurt to go check online on how a particular function was implemented. If you end up copying the entire code, it’s probably your loss in the long run. But then John Maynard Keynes once said, ‘In the long run we are all dead‘. Yeah, and we wonder why people call Economics the dismal science!

    Most people disregard Coursera’s feeble attempt at reigning in plagiarism by creating an Honor Code, precisely because this so-called code-of-conduct can be easily circumvented. I don’t mind posting solutions to a course’s programming assignments because GitHub is full to the brim with such content. Plus, it’s always good to read others’ code even if you implemented a function correctly. It helps understand the different ways of tackling a given programming problem.

    ex1
    ex2
    ex3
    ex4
    ex5
    ex6
    ex7
    ex8

    Enjoy!

     

  • Troubleshooting ‘Rattle’ (R library) Installation on Ubuntu

    Troubleshooting ‘Rattle’ (R library) Installation on Ubuntu

    This post pertains to Ubuntu / Debian users only.

    rattle is a free graphical interface for data mining with R. I wanted to visualize decision trees and had to install this library.
    > install.packages('rattle')
    got me the following error message:

    configure: error: GTK version 2.8.0 required
    ERROR: configuration failed for package ‘RGtk2’

    rattle_installationNonZeroExit

    This error occurs when attempting to install the RGtk2 package. The install is looking for the header files for GTK. Possibly they are not yet. Luckily the problem can be solved quite easily. Open Terminal (Ctrl + Alt + T) and type in the following commands:


    sudo apt-get update
    wajig install libgtk2.0-dev

    Go back and try installing rattle now with the same command as earlier. It should work. It did for me! As you can see below, decision trees are visualized lot better with rattle than if you used just rpart.

    rattle

  • Spot the Difference — It’s NumPy!

    Spot the Difference — It’s NumPy!

    My first brush with NumPy happened over writing a block of code to make a plot using pylab. ⇣


    pylab is part of matplotlib (in matplotlib.pylab) and tries to give you a MatLab like environment. matplotlib has a number of dependencies, among them numpy which it imports under the common alias np. scipy is not a dependency of matplotlib.


    I had a tuple (of lows and highs of temperature) of lengh 2 with 31 entries in each (the number of days in the month of July), parsed from this text file:

    Boston July Temperatures
    ————————-
    Day High Low
    ————
    1 91 70
    2 84 69
    3 86 68
    4 84 68
    5 83 70
    6 80 68
    7 86 73
    8 89 71
    9 84 67
    10 83 65
    11 80 66
    12 86 63
    13 90 69
    14 91 72
    15 91 72
    16 88 72
    17 97 76
    18 89 70
    19 74 66
    20 71 64
    21 74 61
    22 84 61
    23 86 66
    24 91 68
    25 83 65
    26 84 66
    27 79 64
    28 72 63
    29 73 64
    30 81 63
    31 73 63
    view raw julyTemps.txt hosted with ❤ by GitHub

    Given below, are 2 sets of code that do the same thing; one without NumPy and the other with NumPy. They output the following graph using PyLab:

    differenceTemp

    Code without NumPy

    import pylab
    def loadfile():
    inFile = open('julyTemps.txt', 'r')
    high =[]; low = []
    for line in inFile:
    fields = line.split()
    if len(fields) < 3 or not fields[0].isdigit():
    pass
    else:
    high.append(int(fields[1]))
    low.append(int(fields[2]))
    return low, high
    def producePlot(lowTemps, highTemps):
    diffTemps = [highTemps[i] – lowTemps[i] for i in range(len(lowTemps))]
    pylab.title('Day by Day Ranges in Temperature in Boston in July 2012')
    pylab.xlabel('Days')
    pylab.ylabel('Temperature Ranges')
    return pylab.plot(range(1,32),diffTemps)
    producePlot(loadfile()[1], loadfile()[0])
    view raw withoutNumPy.py hosted with ❤ by GitHub

    Code with NumPy
    import pylab
    import numpy as np
    def loadFile():
    inFile = open('julyTemps.txt')
    high = [];vlow = []
    for line in inFile:
    fields = line.split()
    if len(fields) != 3 or 'Boston' == fields[0] or 'Day' == fields[0]:
    continue
    else:
    high.append(int(fields[1]))
    low.append(int(fields[2]))
    return (low, high)
    def producePlot(lowTemps, highTemps):
    diffTemps = list(np.array(highTemps) – np.array(lowTemps))
    pylab.plot(range(1,32), diffTemps)
    pylab.title('Day by Day Ranges in Temperature in Boston in July 2012')
    pylab.xlabel('Days')
    pylab.ylabel('Temperature Ranges')
    pylab.show()
    (low, high) = loadFile()
    producePlot(low, high)
    view raw withNumPy.py hosted with ❤ by GitHub

    The difference in code lies in how the variable diffTemps is calculated.

    diffTemps = list(np.array(highTemps) - np.array(lowTemps))
    

    seems more readable than

    diffTemps = [highTemps[i] - lowTemps[i] for i in range(len(lowTemps))]
    

    Notice how straight forward it is with NumPy. At the core of the NumPy package, is the ndarray object. This encapsulates n-dimensional arrays of homogeneous data types, with many operations being performed in compiled code for performance. element-by-element operations are the “default mode” when an ndarray is involved, but the element-by-element operation is speedily executed by pre-compiled C code.

  • MITx 6.00.2x Introduction to Computational Thinking and Data Science (Fall 2015)

    MITx 6.00.2x Introduction to Computational Thinking and Data Science (Fall 2015)

    MIT’s Fall 2015 iteration of 6.00.2x starts today. After an enriching learning experience with 6.00.1x, I have great expectations from this course. As the course website mildly puts it, 6.00.2x is an introduction to using computation to understand real-world phenomena. MIT OpenCourseware (OCW) mirroring the material covered in 6.00.1x and 6.00.2x can be found here.

    The course follows this book by John Guttag (who happens to be one of the instructors for this course). However, purchasing the book isn’t a necessity for this course.

    Introduction to Computation and Programming Using Python

    One thing I loved about 6.00.1x was its dedicated Facebook group, which gave a community / classroom-peergroup feel to the course. 6.00.2x also has a Facebook group. Here’s a sneak peak:

    descriptionUpdate

    The syllabus and schedule for this course is shown below. The course is spread out over 2 months which includes 7 weeks of lectures.

    MITx 6.00.2x Fall 2015 Course Calendar
    MITx 6.00.2x Fall 2015 Course Calendar

    The prerequisites for this course are pretty much covered in this set of tutorial videos that have been created by one of the TAs for 6.00.1x. If you’ve not taken 6.00.1x in the past, you can go through these videos (running time < 1hr) to judge whether or not to go ahead with 6.00.2x.

    So much for the update. Got work to do! 🙂

  • Funny Python

    Funny Python

    If a programming language is named after a sketch comedy troupe, one knows what to expect. Python IS a funny language with its own bag of surprises.

    pythonMonty
    Monty Python’s Flying Circus

    For instance, If you’ve just moved from a language such as C to Python and you’re missing curly braces (how can one not want whitespaces!!), and you try this:

    >>> from __future__ import braces

    from __future__ import braces
    Click Image for Larger View

    Or say, if you try importing this.

    >>> import this

    import this
    A sense of humour is required for proper interpretation

    Or if you ever wanted to know why XKCD’s Cueball left Perl for Python, you should know, that it was for gravity defying stunts that he couldn’t perform anywhere else. Just import antigravity!

    >>> import antigravity

    You’re led to this webcomic on your browser.

    import antigravity

    So the upshot is that you can get tickled and trolled by Python every now and then, keeping in line with its rich tradition of doing so (check out video below).

    Comedians!

  • Python to the Rescue

    Python to the Rescue

    Another journal-like entry

    Programming as a profession is only moderately interesting. It can be a good job, but you could make about the same money and be happier running a fast food joint. You’re much better off using code as your secret weapon in another profession.

    People who can code in the world of technology companies are a dime a dozen and get no respect. People who can code in biology, medicine, government, sociology, physics, history, and mathematics are respected and can do amazing things to advance those disciplines.

    – Advice from an Old Programmer

    I was reading a paper today, written by MIT’s Esther Duflo, part of a homework assignment on a MOOC on development policy (Foundations of Development Policy: Advanced Development Economics) offered by Duflo and Abhijit Banerjee. So I opened the paper and started copying important lines from the PDF to a text editor to make notes. I could copy the text, but when I pasted it onto a text editor, it turned out to be gibberish (you can try it too!).

    For instance, instead of pasting

    Between 1973 and 1978 the Indonesian Government constructed over 61,000 primary schools throughout the county

    I got:

    Ehwzhhq 4<:6 dqg 4<:;/ wkh Lqgrqhvldq Jryhuqphqw frqv wuxfwhg ryhu 94/333 sulpdu| vfkrrov wkurxjkrxw wkh frxqwu|

    It was a good thing the cipher used for this text wasn’t too complicated. After some perusal, I found that ‘B’ became ‘E’, ‘e’ became ‘h’, ‘t’ became ‘w’ and so on. So I copied the entire content of the PDF to a text file and named the encrypted file estherDuflo.txt. I noticed that the encryption had been implemented only on the first 1475 lines. The remaining was plain English.

    So I wrote a Python script to decrypt the gibberish, rather than simply typing out my notes. It took 20 minutes writing the code and 8 ms to execute (of course!). I didn’t want to spend a lot of time ensuring a thorough decryption, so the result wasn’t perfect, but then I’m going to make do. I named the decrypted file estherDufloDecrypted.txt.

    Sample from the Encrypted File

    5U LL*?} @?_ w@MLh @h!i| L?ti^ i?Uit Lu 5U LL*
    L?t|h U|L? ? W?_L?it@G ,_i?Ui uhL4 @? N? t @* L*U)
    , Tih4i?|
    ,t| ih # L
    W
    Devwudfw
    Ehwzhhq 4<:6 dqg 4<:;/ wkh Lqgrqhvldq Jryhuqphqw frqvwuxfwhg ryhu 94/333 sulpdu|
    vfkrrov wkurxjkrxw wkh frxqwu|1 Wklv lv rqh ri wkh odujhvw vfkrro frqvwuxfwlrq surjudpv rq
    uhfrug1 L hydoxdwh wkh hhfw ri wklv surjudp rq hgxfdwlrq dqg zdjhv e| frpelqlqj glhuhqfhv
    dfurvv uhjlrqv lq wkh qxpehu ri vfkrrov frqvwuxfwhg zlwk glhuhqfhv dfurvv frkruwv lqgxfhg
    e| wkh wlplqj ri wkh surjudp1 Wkh hvwlpdwhv vxjjhvw wkdw wkh frqvwuxfwlrq ri sulpdu| vfkrrov
    ohg wr dq lqfuhdvh lq hgxfdwlrq dqg hduqlqjv1 Fkloguhq djhg 5 wr 9 lq 4<:7 uhfhlyhg 3145 wr
    314< pruh |hduv ri hgxfdwlrq iru hdfk vfkrro frqvwuxfwhg shu 4/333 fkloguhq lq wkhlu uhjlrq
    ri eluwk1 Xvlqj wkh yduldwlrqv lq vfkrrolqj jhqhudwhg e| wklv srolf| dv lqvwuxphqwdo yduldeohv
    iru wkh lpsdfw ri hgxfdwlrq rq zdjhv jhqhudwhv hvwlpdwhv ri hfrqrplf uhwxuqv wr hgxfdwlrq
    udqjlqj iurp 91; shufhqw wr 4319 shufhqw1 +MHO L5/ M64/ R48/ R55,
    Wkh txhvwlrq ri zkhwkhu lqyhvwphqw lq lqiudvwuxfwxuh lqfuhdvhv kxpdq fdslwdo dqg uhgxfhv
    sryhuw| kdv orqj ehhq d frqfhuq wr ghyhorsphqw hfrqrplvwv dqg srolf|pdnhuv1 Iru h{dpsoh/
    dydlodelolw| ri vfkrrolqj lqiudvwuxfwxuh kdv ehhq vkrzq wr eh srvlwlyho| fruuhodwhg zlwk frpsohwhg
    vfkrrolqj ru hquroophqw e| Qdqf| Elugvdoo +4<;8, lq xuedq Eud}lo/ Ghqqlv GhWud| +4<;;, dqg Ohh
    view raw estherDuflo.txt hosted with ❤ by GitHub

    My Code
    from string import *
    # create decipher dictionary
    l = letters[:26]
    decipher = "".join([l[(i+3)%26] for i in range(len(l))])
    decipher = dict(zip(decipher,l))
    # open and read encrypted text
    filename = 'estherDuflo.txt'
    f = open(filename, 'rw')
    lines = f.readlines()
    lines = [l[:-1] for l in lines]
    # use first 1475 lines only
    newlines = lines[:1475]
    # apply decryption on those 1475 lines
    decipheredLines = []
    for line in newlines:
    x = line.lower()
    s = []
    for letter in x:
    if letter in letters:
    s.append(decipher[letter])
    else:
    s.append(letter)
    s.append('\n')
    decipheredLines.append(''.join(s))
    # write deciphered text to new text file
    decipheredFile = 'estherDufloDeciphered.txt'
    df = open(decipheredFile, 'w')
    for line in decipheredLines:
    df.write("%s" % line)
    # close both text files
    f.close()
    df.close()
    view raw estherDuflo.py hosted with ❤ by GitHub

    Sample from the Decrypted File
    5r ii*?} @?_ t@jie @e!f| i?qf^ f?rfq ir 5r ii*
    i?q|e r|i? ? t?_i?fq@d ,_f?rf rei4 @? k? q @* i*r)
    , qfe4f?|
    ,q| fe # i
    t
    abstract
    between 4<:6 and 4<:;/ the indonesian government constructed over 94/333 primar|
    schools throughout the countr|1 this is one of the largest school construction programs on
    record1 i evaluate the eect of this program on education and wages b| combining dierences
    across regions in the number of schools constructed with dierences across cohorts induced
    b| the timing of the program1 the estimates suggest that the construction of primar| schools
    led to an increase in education and earnings1 children aged 5 to 9 in 4<:7 received 3145 to
    314< more |ears of education for each school constructed per 4/333 children in their region
    of birth1 using the variations in schooling generated b| this polic| as instrumental variables
    for the impact of education on wages generates estimates of economic returns to education
    ranging from 91; percent to 4319 percent1 +jel i5/ j64/ o48/ o55,
    the question of whether investment in infrastructure increases human capital and reduces
    povert| has long been a concern to development economists and polic|makers1 for e{ample/
    availabilit| of schooling infrastructure has been shown to be positivel| correlated with completed
    schooling or enrollment b| nanc| birdsall +4<;8, in urban bra}il/ dennis detra| +4<;;, and lee

  • Karatsuba Multiplication Algorithm – Python Code

    Karatsuba Multiplication Algorithm – Python Code

    Motivation for this blog post

    I’ve enrolled in Stanford Professor Tim Roughgarden’s Coursera MOOC on the design and analysis of algorithms, and while he covers the theory and intuition behind the algorithms in a surprising amount of detail, we’re left to implement them in a programming language of our choice.

    And I’m ging to post Python code for all the algorithms covered during the course!

    The Karatsuba Multiplication Algorithm

    Karatsuba’s algorithm reduces the multiplication of two n-digit numbers to at most  n^{\log_23}\approx n^{1.585} single-digit multiplications in general (and exactly n^{\log_23} when n is a power of 2). Although the familiar grade school algorithm for multiplying numbers is how we work through multiplication in our day-to-day lives, it’s slower (\Theta(n^2)\,\!) in comparison, but only on a computer, of course!

    Here’s how the grade school algorithm looks:
    (The following slides have been taken from Tim Roughgarden’s notes. They serve as a good illustration. I hope he doesn’t mind my sharing them.)

    gradeSchoolAlgorithm

    …and this is how Karatsuba Multiplication works on the same problem:

    exampleKaratsuba

    recursiveKaratsuba

    A More General Treatment

    Let x and y be represented as n-digit strings in some base B. For any positive integer m less than n, one can write the two given numbers as

    x = x_1B^m + x_0
    y = y_1B^m + y_0,

    where x_0 and y_0 are less than B^m. The product is then

    xy = (x_1B^m + x_0)(y_1B^m + y_0)
    xy = z_2B^{2m} + z_1B^m + z_0

    where

    z_2 = x_1y_1
    z_1 = x_1y_0 + x_0y_1
    z_0 = x_0y_0

    These formulae require four multiplications, and were known to Charles Babbage. Karatsuba observed that xy can be computed in only three multiplications, at the cost of a few extra additions. With z_0 and z_2 as before we can calculate

    z_1 = (x_1 + x_0)(y_1 + y_0) - z_2 - z_0

    which holds since

    z_1 = x_1y_0 + x_0y_1
    z_1 = (x_1 + x_0)(y_1 + y_0) - x_1y_1 - x_0y_0

    A more efficient implementation of Karatsuba multiplication can be set as xy = (b^2 + b)x_1y_1 - b(x_1 - x_0)(y_1 - y_0) + (b + 1)x_0y_0, where b = B^m.

    Example

    To compute the product of 12345 and 6789, choose B = 10 and m = 3. Then we decompose the input operands using the resulting base (Bm = 1000), as:

    12345 = 12 · 1000 + 345
    6789 = 6 · 1000 + 789

    Only three multiplications, which operate on smaller integers, are used to compute three partial results:

    z2 = 12 × 6 = 72
    z0 = 345 × 789 = 272205
    z1 = (12 + 345) × (6 + 789) − z2 − z0 = 357 × 795 − 72 − 272205 = 283815 − 72 − 272205 = 11538

    We get the result by just adding these three partial results, shifted accordingly (and then taking carries into account by decomposing these three inputs in base 1000 like for the input operands):

    result = z2 · B2m + z1 · Bm + z0, i.e.
    result = 72 · 10002 + 11538 · 1000 + 272205 = 83810205.

    Pseudocode and Python code

    procedure karatsuba(num1, num2)
    if (num1 < 10) or (num2 < 10)
    return num1*num2
    /* calculates the size of the numbers */
    m = max(size_base10(num1), size_base10(num2))
    m2 = m/2
    /* split the digit sequences about the middle */
    high1, low1 = split_at(num1, m2)
    high2, low2 = split_at(num2, m2)
    /* 3 calls made to numbers approximately half the size */
    z0 = karatsuba(low1,low2)
    z1 = karatsuba((low1+high1),(low2+high2))
    z2 = karatsuba(high1,high2)
    return (z2*10^(2*m2))+((z1-z2-z0)*10^(m2))+(z0)

    def karatsuba(x,y):
    """Function to multiply 2 numbers in a more efficient manner than the grade school algorithm"""
    if len(str(x)) == 1 or len(str(y)) == 1:
    return x*y
    else:
    n = max(len(str(x)),len(str(y)))
    nby2 = n / 2
    a = x / 10**(nby2)
    b = x % 10**(nby2)
    c = y / 10**(nby2)
    d = y % 10**(nby2)
    ac = karatsuba(a,c)
    bd = karatsuba(b,d)
    ad_plus_bc = karatsuba(a+b,c+d) – ac – bd
    # this little trick, writing n as 2*nby2 takes care of both even and odd n
    prod = ac * 10**(2*nby2) + (ad_plus_bc * 10**nby2) + bd
    return prod
    view raw karatsuba.py hosted with ❤ by GitHub