Lecture 9
Functions
DATA 8
Fall 2017
Slides created by John DeNero (denero@berkeley.edu) and Ani Adhikari (adhikari@berkeley.edu)
Announcements
Attendance
Comparing Histograms
Overlaid Graphs
For visually comparing two populations
(Demo)
Discussion Question
This histogram describes a year of daily temperatures
Try to answer these questions:
Defining Functions
Def Statements
User-defined functions give names to blocks of code
def spread(values):
return max(values) - min(values)
(Demo)
Name
Argument names (parameters)
Body
Return expression
Discussion Question
What does this function do? What kind of input does it take? What output will it give? What's a reasonable name?
def f(s):
return np.round(s / sum(s) * 100, 2)
(Demo)
Apply
Apply
The apply method creates an array by calling a function on every element in input column(s)
table_name.apply(function_name, 'column_label')
(Demo)
Example: Prediction
Sir Francis Galton
(Demo)