Definition of a function

Any method for taking a list of inputs and determining the corresponding output.

 

Important words:

  • method
  • input
  • output

Mathematical functions

Examples:

$$f(x)=x^2-5$$$$g(x,y,z)=\frac{x^2-y^2}{z}$$

English functions

Examples:

  • the capitol of        
  • the difference in ages between         and        

Python functions

In [1]:
def square ( x ):
    return x**2

def is_a_long_word ( word ):
    return len(word) > 8

Functions in tables

Bentley Email ID Real Name
aaltidor Alina Altidor
mbhaduri Moinak Bhaduri
wbuckley Winston Buckley
ncarter Nathan Carter
lcherveny Luke Cherveny

(Imagine the table continuing, containing the User ID and Name of each memeber of Bentley's Mathematical Sciences Department.)

Practice writing Python functions

See the course notes for three exercises to try now.

Inputs and outputs to use when testing your implementations are shown below.

Exercise 1 - from mathematics

Difficulty Test inputs Correct output
Easy solve_quadratic(1,0,-4) [-2,2]
Medium solve_quadratic(1,-2,100) [10]
Medium solve_quadratic(3,0,3) []
Hard solve_quadratic(0,1,2) [-2]

Exercise 2 - from English

Difficulty Test inputs Correct output
Easy last_closing_price('GOOG') (varies by day, roughly \$1500)
Hard last_closing_price('FOO') None

Exercise 3 - from a table

Difficulty Test inputs Correct output
Easy country_capitol('LESOTHO') 'MASERU'
Medium country_capitol('malta') 'VALLETTA
Hard country_capitol('nowhere') None

Terminology review

Let's discuss the meaning of each term below, covered in the course notes.

  • data type
  • input type(s), and output type
  • map
  • arity
  • unary, binary, and ternary

Definition of a relation

A function whose output type is bool

 

In this sense, a relation is a particular type of function.

Mathematical relations

Examples:

$$x^2+y^2\ge z^2$$$$x<0$$

English relations

Examples:

  •         is the capitol of        
  •         is a fruit

Python relations

In [2]:
def R ( a, b ):
    return a in b[1:]

def is_a_primary_color ( c ):
    return c in ['red','green','blue']

Relations in tables

This is an extremely common way to encounter relations in data science.

Bentley Email ID Real Name
aaltidor Alina Altidor
mbhaduri Moinak Bhaduri
wbuckley Winston Buckley
ncarter Nathan Carter
lcherveny Luke Cherveny

Here we use the same table as earlier (still imagining it contains the whole department), but now it is interpreted as a relation instead of a function.

How would you express the relation in English?

Example uses of relations

The following code builds a simple pandas DataFrame we will manipulate in the subsequent slides.

In [3]:
import pandas as pd
df = pd.DataFrame( {
    'ID' : [ 257, 876, 329, 392, 780 ],
    'age' : [ 12, 73, 60, 28, 5 ],
    'height' : [ 60, 65, 73, 75, 41 ]
} )
In [4]:
df
Out[4]:
ID age height
0 257 12 60
1 876 73 65
2 329 60 73
3 392 28 75
4 780 5 41
In [5]:
is_a_minor = df['age'] < 18   # in years
is_tall = df['height'] >= 72  # in inches

df[is_a_minor]
Out[5]:
ID age height
0 257 12 60
4 780 5 41
In [6]:
df
Out[6]:
ID age height
0 257 12 60
1 876 73 65
2 329 60 73
3 392 28 75
4 780 5 41
In [7]:
df[~is_a_minor]
Out[7]:
ID age height
1 876 73 65
2 329 60 73
3 392 28 75
In [8]:
df
Out[8]:
ID age height
0 257 12 60
1 876 73 65
2 329 60 73
3 392 28 75
4 780 5 41
In [9]:
df[is_a_minor & is_tall]
Out[9]:
ID age height
In [10]:
df
Out[10]:
ID age height
0 257 12 60
1 876 73 65
2 329 60 73
3 392 28 75
4 780 5 41
In [11]:
df[~is_a_minor & ~is_tall]
Out[11]:
ID age height
1 876 73 65

Practice writing Python relations

See the course notes for three exercises to try now.

Exercise 4 - food inspections

We will discuss in class the answers to these questions:

  1. Name at least two relations expressed by the contents of this table. (You need not use all the columns.)
  2. What are the input types, output type, and arity of each of your relations?
  3. Does the table contain any sets of columns that define a function?
  4. If so, what are the input types, output type, and arity of the function(s)?

Exercise 5 - tech companies

We will discuss in class the same four questions for this exercise.

Questions from the notes

  • In what way is a relation a special kind of function?
  • In what way is a function a special kind of relation?
  • What does it mean to do "lookup" in a table representing a function?
  • What does it mean to do "lookup" in a table representing a relation?
  • How can you tell, from a table, whether a function has an inverse?
  • How is a Python list like a function?
  • How is a Python dictionary like a function?
  • How can we convert any two-column table into a Python dictionary?