In [1]:
%load_ext watermark
%watermark -d -a 'Sebastian Raschka' -v


Sebastian Raschka 22/12/2014 

CPython 3.4.2
IPython 2.3.1



Star Wars API

This notebook is all about exploratory data analyses based on the new and awesome Star Wars API by Paul Hallett.



Planets


In [8]:
import requests
import json

In [9]:
d = {'name':[], 'diameter':[], 'population':[]}

url = "http://swapi.co/api/planets/?page=1"

while url:
    r = requests.get(url)
    data = json.loads(r.text)
    for i in data['results']:
        d['name'].append(i['name'])
        d['diameter'].append(i['diameter'],)
        d['population'].append(i['population']),
    url = data['next']

In [10]:
import pandas as pd

df = pd.DataFrame(d)
df = df[(df['diameter']!='unknown') & (df['population']!='unknown')]
df['diameter'] = df['diameter'].astype(int)
df['population'] = df['population'].astype(int)
df.head()


Out[10]:
diameter name population
0 12500 Alderaan 2000000000
1 10200 Yavin IV 1000
4 118000 Bespin 6000000
5 4900 Endor 30000000
6 12120 Naboo 4500000000

In [11]:
%matplotlib inline

In [14]:
import plotly.plotly as py
from random import random, randrange, seed
seed(456)

traces = []

for i in df.index:
    tr = Scatter(
        x=df.ix[i]['diameter'], 
        y=df.ix[i]['population'],
        name=df.ix[i]['name'],
        marker=Marker(
        color='rgb(%s, %s, %s)' % (randrange(0,256), randrange(0,256), randrange(0,256)),
        size=0.001*df.ix[i]['diameter']))
    traces.append(tr)

layout = Layout(
        title='Star Wars API - Planets',
        xaxis=XAxis(
            showgrid=False,
            zeroline=False,
            title='Diameter [km]',
            tick0=-1,
            type='log'),
        yaxis=YAxis(
            showgrid=False,
            zeroline=False,
            title='Population',
            type='log'))

fig = Figure(data=traces, layout=layout)
py.iplot(fig, filename='SWAPI-Planets')


Out[14]:



People


In [2]:
import requests
import json

In [3]:
d = {'name':[], 'mass':[], 'height':[]}

url = "http://swapi.co/api/people/?page=1"

while url:
    r = requests.get(url)
    data = json.loads(r.text)
    for i in data['results']:
        d['name'].append(i['name'])
        d['mass'].append(i['mass'],)
        d['height'].append(i['height']),
    url = data['next']

In [4]:
import pandas as pd

df = pd.DataFrame(d)
df = df[(df['mass']!='unknown') & (df['height']!='unknown')]
df['mass'] = df['mass'].apply(lambda x: x.replace(',','.'))
df['mass'] = df['mass'].astype(float)
df['height'] = df['height'].astype(int)
df.head()


Out[4]:
height mass name
0 172 77 Luke Skywalker
1 167 75 C-3PO
2 96 32 R2-D2
3 202 136 Darth Vader
4 150 49 Leia Organa

In [6]:
import plotly.plotly as py
from plotly.graph_objs import *
import plotly.tools as tls
from random import random, randrange, seed
seed(123)

traces = []

for i in df.index:
    tr = Scatter(
        x=df.ix[i]['height'], 
        y=df.ix[i]['mass'],
        name=df.ix[i]['name'],
        marker=Marker(
        color='rgb(%s, %s, %s)' % (randrange(0,256), randrange(0,256), randrange(0,256)),
        size=df.ix[i]['mass']/df.ix[i]['height']*20))
    traces.append(tr)

layout = Layout(
        title='Star Wars API - People',
        showlegend=False,
        xaxis=XAxis(
            showgrid=False,
            zeroline=False,
            tick0=-1,
            title='Mass [kg]',),
        yaxis=YAxis(
            showgrid=False,
            zeroline=False,
            title='Height [cm]',))

fig = Figure(data=traces, layout=layout)
py.iplot(fig, filename='SWAPI-People')


Out[6]:



Spaceships


In [12]:
import requests
import json

In [13]:
d = {'name':[], 'max_atmosphering_speed':[], 'hyperdrive_rating':[]}

url = "http://swapi.co/api/starships/?page=1"

while url:
    r = requests.get(url)
    data = json.loads(r.text)
    for i in data['results']:
        d['name'].append(i['name'])
        d['max_atmosphering_speed'].append(i['max_atmosphering_speed'],)
        d['hyperdrive_rating'].append(i['hyperdrive_rating']),
    url = data['next']

In [14]:
import pandas as pd

df = pd.DataFrame(d)
df['max_atmosphering_speed'] = df['max_atmosphering_speed'].apply(lambda x: x.strip('km'))
df = df[(df['max_atmosphering_speed']!='n/a') & (df['hyperdrive_rating']!='n/a')]
df = df[(df['max_atmosphering_speed']!='unknown') & (df['hyperdrive_rating']!='unknown')]
df['max_atmosphering_speed'] = df['max_atmosphering_speed'].astype(float)
df['hyperdrive_rating'] = df['hyperdrive_rating'].astype(float)
df.head()


Out[14]:
hyperdrive_rating max_atmosphering_speed name
0 2.0 950 CR90 corvette
1 2.0 975 Star Destroyer
2 1.0 1000 Sentinel-class landing craft
4 0.5 1050 Millennium Falcon
5 1.0 1000 Y-wing

In [15]:
import plotly.plotly as py
from plotly.graph_objs import *
import plotly.tools as tls
from random import random, randrange, seed
seed(123)

traces = []

for i in df.index:
    tr = Scatter(
        x=df.ix[i]['max_atmosphering_speed'], 
        y=df.ix[i]['hyperdrive_rating'],
        name=df.ix[i]['name'],
        marker=Marker(
        color='rgb(%s, %s, %s)' % (randrange(0,256), randrange(0,256), randrange(0,256)),
        size=0.001 * df.ix[i]['max_atmosphering_speed']/df.ix[i]['hyperdrive_rating']*20))
    traces.append(tr)

layout = Layout(
        title='Star Wars API - Spaceships',
        showlegend=False,
        xaxis=XAxis(
            showgrid=False,
            zeroline=False,
            title='Max. atmosphering speed [km/h]',
            tick0=-1,
            type='log'),
        yaxis=YAxis(
            showgrid=False,
            zeroline=False,
            title='Hyperdrive rating',),)

fig = Figure(data=traces, layout=layout)
py.iplot(fig, filename='SWAPI-Spaceships')


Out[15]:

In [ ]: