iPython notebooks are a great way to learn and experiment with code

The fact that they document your work so well is just a benefit!

So let's work on a little project together :)

I have a really up-lifting topic we can look at, School Shooting Incidents in the United States

In [1]:
import os
import sys

os.listdir('.')
Out[1]:
['us_population.csv', 'Example.ipynb', 'school_shootings.csv']
In [3]:
import gale.fileIO.csvHandlers as csvh
data, headers = csvh.read_as_list('school_shootings.csv', header=True)
print data[0]
['1850', '5']
In [2]:
import pandas as pd
df = pd.read_csv('school_shootings.csv')
print df.head()
   Decade  Count
0    1850      5
1    1860      6
2    1870     12
3    1880     16
4    1890     11
In [4]:
df.plot(x='Decade', y='Count')
Out[4]:
<matplotlib.axes.AxesSubplot at 0x9ecfc6c>

Gross! Let's make that look nicer easily

In [ ]:
from mpltools import style
style.use('ggplot')

df.plot(x='Decade', y='Count')
plt.ylabel('Number of School Shooting Incidents')
In [ ]:
os.listdir('.')
In [ ]:
uspop = pd.read_csv('us_population.csv')
uspop.head()
In [ ]:
def pull_year(date_string):
    return int(date_string.split(' ')[-1])

uspop['year'] = uspop.Date.apply(pull_year)
uspop.head()
In [ ]:
uspop.plot(x='year', y='National Population')
plt.ylabel('US Population')
plt.xlabel('Year')

So iPython notebook is great and it documents my work, but sometimes it's easier to use bash commands than just Python

iPython already recognizes some of the basic bash commands and other shell commands can be run with a '!' before it.

In [5]:
ls
Example.ipynb  school_shootings.csv  us_population.csv
In [6]:
cd ~/Projects/Fragmented-Care/
/home/visitors/adampah/Projects/Fragmented-Care
In [7]:
ls -la
total 40
drwxrwxr-x  6 adampah adampah 4096 May 20 11:38 ./
drwxr-xr-x 27 adampah adampah 4096 May 29 11:21 ../
drwxr-xr-x  5 adampah adampah 4096 May 28 08:24 Code/
drwxr-xr-x 16 adampah adampah 4096 Jun  9 21:23 Data/
-rw-r--r--  1 adampah adampah 4096 May 19 15:36 ._.DS_Store
-rw-r--r--  1 adampah adampah 6148 May 20 08:53 .DS_Store
drwxr-xr-x  4 adampah adampah 4096 May 29 12:00 .hg/
-rw-r--r--  1 adampah adampah   31 May 19 15:52 .hgignore
drwxrwxr-x  3 adampah adampah 4096 Jun  9 16:34 Results/
In [8]:
!ping www.google.com
PING www.google.com (74.125.225.116) 56(84) bytes of data.
64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_req=1 ttl=56 time=1.61 ms
64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_req=2 ttl=56 time=1.57 ms
64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_req=3 ttl=56 time=1.57 ms
64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_req=4 ttl=56 time=1.62 ms
64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_req=5 ttl=56 time=1.63 ms
^C
--- www.google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4003ms
rtt min/avg/max/mdev = 1.571/1.604/1.635/0.044 ms

But sometimes you may want to actually just execute a big block of bash code and we can do that too!

In [9]:
%%bash
cd $gale
ls -la
total 16
drwxrwxr-x  4 adampah adampah 4096 Jun  6 15:42 .
drwxr-xr-x 23 adampah adampah 4096 Jun  2 18:52 ..
drwxrwxr-x 14 adampah adampah 4096 Jun  3 19:43 gale
drwxrwxr-x  4 adampah adampah 4096 Jun  6 15:42 .hg

That '%%' symbol is the invocation of a magic command (specifically a cell magic, you can do a line magic with just a single '%').

iPython notebook can run code from a number of other programming languages that way.

One thing to note though, is executing bash commands in a cell magic doesn't change the state of the actual notebook.

In [10]:
ls -la
total 40
drwxrwxr-x  6 adampah adampah 4096 May 20 11:38 ./
drwxr-xr-x 27 adampah adampah 4096 May 29 11:21 ../
drwxr-xr-x  5 adampah adampah 4096 May 28 08:24 Code/
drwxr-xr-x 16 adampah adampah 4096 Jun  9 21:23 Data/
-rw-r--r--  1 adampah adampah 4096 May 19 15:36 ._.DS_Store
-rw-r--r--  1 adampah adampah 6148 May 20 08:53 .DS_Store
drwxr-xr-x  4 adampah adampah 4096 May 29 12:00 .hg/
-rw-r--r--  1 adampah adampah   31 May 19 15:52 .hgignore
drwxrwxr-x  3 adampah adampah 4096 Jun  9 16:34 Results/

Even better we can actually transfer the results from a bash command to python!

In [11]:
files = !ls
for individual_file in files:
    print individual_file
Code
Data
Results

Even better iPython can handle this with more languages than just bash too! At the current time it works with:

  • Ruby
  • Javascript
  • HTML
  • LaTeX
  • Perl
  • R
  • Julia

    and more are always coming. Running them is also simple

In [12]:
%%ruby
puts "Oh snap. This is Ruby Code!"
Oh snap. This is Ruby Code!
In [13]:
%%javascript
console.log("Double damn! It's javascript!")

But wait, more!

Magics aren't just used for running other languages, there are also other built-in, useful magics

In [14]:
%timeit map(float, [1, 2, 3, 4, 5] * 1000)
1000 loops, best of 3: 594 ┬Ás per loop
In [ ]:
%timeit [float(x) for x in [1, 2, 3, 4, 5] * 1000]