Horizontal bar chart with 3 encodings using matplotlib


The chart explains the gender difference in school performance based on different states of india. Full project report

View python source code inside packages


Often we want to know how a function is written in an imported package. This post explains how to examine the source code of a function/class.

To know where the package is installed:


For the package pandas:

import pandas

To examine the source code of a given function or class, import the package inspect.

import inspect as insp
print insp.getsourcefile(pandas.DataFrame) # prints the path to source file

print insp.getsourcelines(pandas.DataFrame) # prints the source code

Documentation of inspect package can be found here.

Viewing the source code from IPython Notebook

Append ? to the function name inside the ipython-notebook cell to view code description and ?? for the entire source code.

import pandas

pandas.DataFrame? # shows the docstring

pandas.DataFrame?? # shows the source code and docstring

Return top “N” elements from an array

top_n returns a mask = [True, False, True, False, False ...] with “True” for top n values. The mask is passed into an array as index to get “True” values.

import numpy as np
from scipy.stats import rankdata

def top_n(list_array, n = 1):

  Returns a boolean mask with "True" for greatest "n" number of values
  np_array = np.array(list_array)
  # creating a mask
  mask = np.zeros(len(np_array.flatten()), dtype=bool)
  r =rankdata(np_array, method ="dense")
  # rank matrix with highest value =1
  for index, val in enumerate(r):
    if  val <= (n):
	mask[index] = True
  return mask.reshape(np_array.shape)

boolean_filter will return a list where boolean is true.

def boolean_filter(b_list, boolean):
  This function returns values in b_list where the boolean is true
  return [item for i, item in enumerate(b_list) if boolean[i]==True]