from datascience import *
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')
#sleep = On average, how many hours of sleep do you get each night?
#tv = During a typical week, how many hours do you spend watching television?
#number = Pick a number between 0 and 9.
#study = During a typical week, how many hours do you spend studying?
#snow_white = Which of the Seven Dwarfs from Snow White are you most like?
survey = Table.read_table('survey09.csv')
survey.show(5)
survey.hist('number', bins=np.arange(0, 9))
study = survey.column("studying")
[min(study), max(study)]
survey.hist('studying', bins=np.arange(0, 81, 10))
survey.hist('studying', bins=np.arange(0, 51, 5))
survey.hist('studying', bins=np.arange(0, 51, 2))
survey.hist('tv', bins=15)
survey.hist('sleep', bins=5)
seven = survey.group('snow_white')
seven.barh('snow_white')
galton = Table.read_table('galton.csv')
#Each row corresponds to one adult child
#family = family indicator
#father height (inches)
#mother height (inches)
#"midparent height"= weighted average of parents' heights
#children= # of children in the family
#childNum = child's birth rank (1 = oldest)
#gender
#height (inches)
galton
heights = galton.select(3, 7).relabeled(0, 'MidParent').relabeled(1, 'Child')
heights
my_bins = np.arange(55, 80, 2)
#Histogram of child heights
heights.hist('Child', bins = my_bins, unit='Inch')
#Percentage of heights between 65 and 67
heights.where('Child', are.between(65, 67)).num_rows / heights.num_rows
#Histogram of parent heights
heights.hist('MidParent', bins=my_bins, unit='inch')
#Combined histogram
heights.hist(bins=my_bins, unit='inch')
def double(x):
return x * 2
double(7)
double(15/3)
my_number = 12
double(my_number)
double(my_number / 8)
double(make_array(3, 4, 5))
double('data')
#"local scope"
x
x = 17
double(2)
x
double(x)
x
#What does this function do?
def percents(values):
return np.round(values / sum(values) * 100, 2)
percents(make_array(1, 2, 3, 4))
percents(make_array(1, 4, 30))
#Can have multiple inputs
def percents(values, places):
return np.round(values / sum(values) * 100, places)
percents(make_array(1, 4, 30), 1)
ages = Table().with_columns(
'Person', make_array('A', 'B', 'C', 'D'),
'Age', make_array(63, 110, 99, 102)
)
ages
def cut_off_at_100(z):
return min(z, 100)
cut_off_at_100(3)
cut_off_at_100(107)
cut_age_array = ages.apply(cut_off_at_100, 'Age')
cut_age_array
ages.with_column('Cut off ages', cut_age_array)
type(cut_off_at_100)
heights
heights.scatter('MidParent', 'Child')
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);
nearby = heights.where('MidParent', are.between(67.5, 68.5))
nearby.column('Child').mean()
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=75);
def predict_child(h):
nearby = heights.where('MidParent', are.between(h-0.5, h+0.5))
return nearby.column('Child').mean()
predict_child(68)
predict_child(65)
predictions = heights.apply(predict_child, 'MidParent')
heights = heights.with_column('Child Prediction', predictions)
heights
heights.scatter('MidParent')