In [ ]:
from datascience import *
import numpy as np

%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')

Lecture 10

Prediction

In [ ]:
galton = Table.read_table('galton.csv')
heights = galton.select(3, 7).relabeled(0, 'MidParent').relabeled(1, 'Child')
In [ ]:
heights
In [ ]:
heights.scatter('MidParent', 'Child')
In [ ]:
heights.scatter('MidParent', 'Child')
plots.plot([67.5, 67.5], [50, 85], color='red', lw=2)
plots.plot([68.5, 68.5], [50, 85], color='red', lw=2);
In [ ]:
nearby = heights.where('MidParent', are.between(67.5, 68.5))
nearby.column('Child').mean()
In [ ]:
heights.scatter('MidParent', 'Child')
plots.plot([67.5, 67.5], [50, 85], color='red', lw=2)
plots.plot([68.5, 68.5], [50, 85], color='red', lw=2)
plots.scatter(68, 66.24, color='gold', s=50);
In [ ]:
def predict_child(h):
    nearby = heights.where('MidParent', are.between(h - 0.5, h + 0.5))
    return nearby.column('Child').mean()

predict_child(68)
In [ ]:
predict_child(70)
In [ ]:
predict_child(72)
In [ ]:
predicted = heights.apply(predict_child, 'MidParent')
predicted
In [ ]:
heights = heights.with_column('Predicted child', predicted)
heights
In [ ]:
heights.scatter('MidParent')

Apply with Multiple Columns

In [ ]:
temperatures = Table.read_table('temperatures.csv')
temperatures
In [ ]:
temperatures.plot('Day')
In [ ]:
temperatures.select('Low', 'High').hist(bins=np.arange(30, 105, 5))
In [ ]:
temperatures.scatter('Low', 'High')
In [ ]:
# Difference between high temp and low temp
def difference(x, y):
    return x-y

difference(65, 54)
In [ ]:
daily_spread = temperatures.apply(difference, 'High', 'Low')
temperatures = temperatures.with_column('Spread', daily_spread)
temperatures
In [ ]:
temperatures.hist('Spread', bins=np.arange(0, 40, 4))
In [ ]:
temperatures.where('Spread', are.above(20)).num_rows / temperatures.num_rows

Function with Optional Arguments

In [ ]:
def percents(s, places):
    return np.round(s/sum(s) * 100, places)
In [ ]:
x = make_array(2, 5, 16)
percents(x, 4)
In [ ]:
def percents(s, places=2):
    return np.round(s/sum(s) * 100, places)
In [ ]:
percents(x)

Grouping by Category

In [ ]:
all_cones = Table.read_table('cones.csv')
all_cones
In [ ]:
cones = all_cones.drop('Color').exclude(5)
cones
In [ ]:
cones.group('Flavor')
In [ ]:
cones.group('Flavor', min)
In [ ]:
cones.group('Flavor', list)
In [ ]:
cones.group('Flavor', np.average)
In [ ]:
#Can get min price using .group
cones.group('Flavor', min)
In [ ]:
#Or by computing using other table methods:
min(cones.where('Flavor', 'chocolate').column('Price'))
In [ ]:
#User-defined function
def spread(arr):
    return max(arr) - min(arr)

spread(make_array(7, 10, 2))
In [ ]:
#Use your own function in .group
cones.group('Flavor', spread)

Grouping by multiple columns

In [ ]:
cones
In [ ]:
all_cones
In [ ]:
all_cones.group(['Flavor', 'Color'])
In [ ]:
all_cones.group(['Flavor', 'Color'], np.average)

Examples

In [ ]:
nba = Table.read_table('nba_salaries.csv').relabeled(3, 'SALARY')
nba
In [ ]:
# total salary paid by each team, highest first

nba.select('TEAM', 'SALARY').group('TEAM', sum).sort('SALARY sum', descending=True)
In [ ]:
nba.group('TEAM', sum)
In [ ]:
# average salary paid for each position

nba.select('POSITION', 'SALARY').group('POSITION', np.average)
In [ ]:
# for each team, average salary paid for each position

nba.drop('PLAYER').group(['TEAM', 'POSITION'], np.average)

Pivot Tables

In [ ]:
all_cones
In [ ]:
all_cones.group(['Flavor', 'Color'])
In [ ]:
all_cones.pivot('Flavor', 'Color')
In [ ]:
all_cones.pivot('Flavor', 'Color', values='Price', collect=np.average)
In [ ]:
all_cones.pivot('Flavor', 'Color', values='Price', collect=list)

Examples

In [ ]:
survey = Table.read_table('survey10.csv')
In [ ]:
survey.show(3)
In [ ]:
survey.pivot('love_at_first_sight', 'year')
In [ ]:
survey.pivot('love_at_first_sight', 'super_power',values="number",collect=np.mean)

Challenge Question

Which NBA teams spent the most on their “starters” in 2015-2016?

In [ ]:
nba
In [ ]:
# Let's first look at average salary per team per position
nba.pivot('POSITION', 'TEAM', values = 'SALARY', collect = np.average)
In [ ]:
#If we assume the "starter" is the player paid the most in each position, 
#we can use "collect = max"
nba_starters_salaries = nba.pivot('POSITION', 'TEAM', values = 'SALARY', collect = max)
In [ ]:
#Now we need to find the total paid for the starters
totals = nba_starters_salaries.drop("TEAM").apply(sum)
In [ ]:
#Add "totals" to our "nba_starters_salaries" table and sort by total
nba_starters_salaries.with_column("TOTAL", totals).sort("TOTAL",descending = True)
In [ ]:
 
In [ ]: