from datascience import *
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')
Let's examine visualizations of numerical data by looking at how old the top grossing movies are.
top = Table.read_table('top_movies_2017.csv')
top
# add the movie age to the top Table
ages = 2022 - top.column('Year')
top = top.with_column('Age', ages)
top
We can bin numerical data by creating a set of bins end points, and then calculating how many data points fall within each bin.
[min(ages), max(ages)]
# create the bin end points
my_bins = np.arange(0, 121, 20)
my_bins
# Bin the ages of movies into bins of [ ). The last row just gives the end of the last bin and is always 0.
top.bin('Age', bins = my_bins)
# It is possible to bin with intervals of different sizes
uneven_bins = make_array(0, 5, 10, 15, 25, 40, 65, 101)
uneven_bins
# Bin the ages of movies into bins of [ ). The last row just gives the end of the last bin and is always 0.
top.bin('Age', bins = uneven_bins)
sum(top.bin('Age', bins = uneven_bins).column(1))
Histograms are a useful way to visual numerical data. To create a histogram we binned the data, and then treated the bins as categories and create a bar plot of the resulting data.
# histogram with even bin sizes
top.hist('Age', bins = np.arange(0, 110, 10), unit = 'Years')
# We can also specify the number of evenly sized bins we want.
top.hist('Age', bins = 20, unit = 'Years')
# We can create histograms of uneven bin sizes.
# The *area* of the bar should be proportional to the number of items in a bin range.
top.hist('Age', bins = uneven_bins, unit = 'Years')
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(100 * values / sum(values), 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)
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
# Side note: overlapping histogram
heights.hist(bins=my_bins, unit='inches')
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')