1 of 13

Lecture 9

Functions

DATA 8

Fall 2017

Slides created by John DeNero (denero@berkeley.edu) and Ani Adhikari (adhikari@berkeley.edu)

2 of 13

Announcements

3 of 13

Attendance

4 of 13

Comparing Histograms

5 of 13

Overlaid Graphs

For visually comparing two populations

(Demo)

6 of 13

Discussion Question

This histogram describes a year of daily temperatures

Try to answer these questions:

  • What proportion of days had a high temp in the range 60-69?
  • What proportion had �a low of 45 or more?
  • How many days had�a difference of more�than 20 degrees�between their high &�low temperatures?

7 of 13

Defining Functions

8 of 13

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

9 of 13

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)

10 of 13

Apply

11 of 13

Apply

The apply method creates an array by calling a function on every element in input column(s)

  • First argument: Function to apply
  • Other arguments: The input column(s)

table_name.apply(function_name, 'column_label')

(Demo)

12 of 13

Example: Prediction

13 of 13

Sir Francis Galton

  • 1822 - 1911 (knighted in 1909)
  • A pioneer in making predictions
  • Particular interest in heredity
  • Charles Darwin's half-cousin

(Demo)