Any method for taking a list of inputs and determining the corresponding output.
Important words:
Examples:
$$f(x)=x^2-5$$$$g(x,y,z)=\frac{x^2-y^2}{z}$$def square ( x ):
return x**2
def is_a_long_word ( word ):
return len(word) > 8
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.)
See the course notes for three exercises to try now.
Inputs and outputs to use when testing your implementations are shown below.
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] |
Difficulty | Test inputs | Correct output |
---|---|---|
Easy | last_closing_price('GOOG') |
(varies by day, roughly \$1500) |
Hard | last_closing_price('FOO') |
None |
Difficulty | Test inputs | Correct output |
---|---|---|
Easy | country_capitol('LESOTHO') |
'MASERU' |
Medium | country_capitol('malta') |
'VALLETTA |
Hard | country_capitol('nowhere') |
None |
Let's discuss the meaning of each term below, covered in the course notes.
A function whose output type is
bool
In this sense, a relation is a particular type of function.
Examples:
$$x^2+y^2\ge z^2$$$$x<0$$def R ( a, b ):
return a in b[1:]
def is_a_primary_color ( c ):
return c in ['red','green','blue']
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?
The following code builds a simple pandas DataFrame we will manipulate in the subsequent slides.
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 ]
} )
df
ID | age | height | |
---|---|---|---|
0 | 257 | 12 | 60 |
1 | 876 | 73 | 65 |
2 | 329 | 60 | 73 |
3 | 392 | 28 | 75 |
4 | 780 | 5 | 41 |
is_a_minor = df['age'] < 18 # in years
is_tall = df['height'] >= 72 # in inches
df[is_a_minor]
ID | age | height | |
---|---|---|---|
0 | 257 | 12 | 60 |
4 | 780 | 5 | 41 |
df
ID | age | height | |
---|---|---|---|
0 | 257 | 12 | 60 |
1 | 876 | 73 | 65 |
2 | 329 | 60 | 73 |
3 | 392 | 28 | 75 |
4 | 780 | 5 | 41 |
df[~is_a_minor]
ID | age | height | |
---|---|---|---|
1 | 876 | 73 | 65 |
2 | 329 | 60 | 73 |
3 | 392 | 28 | 75 |
df
ID | age | height | |
---|---|---|---|
0 | 257 | 12 | 60 |
1 | 876 | 73 | 65 |
2 | 329 | 60 | 73 |
3 | 392 | 28 | 75 |
4 | 780 | 5 | 41 |
df[is_a_minor & is_tall]
ID | age | height |
---|
df
ID | age | height | |
---|---|---|---|
0 | 257 | 12 | 60 |
1 | 876 | 73 | 65 |
2 | 329 | 60 | 73 |
3 | 392 | 28 | 75 |
4 | 780 | 5 | 41 |
df[~is_a_minor & ~is_tall]
ID | age | height | |
---|---|---|---|
1 | 876 | 73 | 65 |
See the course notes for three exercises to try now.
We will discuss in class the answers to these questions:
We will discuss in class the same four questions for this exercise.