Functions Intro 2 CS week 1 1 Functions

  • Slides: 73
Download presentation
Functions Intro 2 CS – week 1 1

Functions Intro 2 CS – week 1 1

Functions / Procedures • Simplify programs • Break them down to smaller building blocks

Functions / Procedures • Simplify programs • Break them down to smaller building blocks • Avoid code duplication • Work in a local setting (local variables) • Abstraction: Hide implementation details 2

Syntax def <function_name>([arguments]): <statement> … • Statements are indented (4 spaces is standard) •

Syntax def <function_name>([arguments]): <statement> … • Statements are indented (4 spaces is standard) • Function names: just like variables. The convention is small characters separated by ‘_’ 3

Example def pretty_print(text): length = len(text) print("*" * (length+4)) Indentation print("*", text, "*") print("*"

Example def pretty_print(text): length = len(text) print("*" * (length+4)) Indentation print("*", text, "*") print("*" * (length+4)) pretty_print(“Hello!”) Output: ***** * Hello! * ***** 4

Return values • Functions can return values – a result of their computation with

Return values • Functions can return values – a result of their computation with the return statement: return <expression> def norm 2(vec): result = 0 for number in vec: result += number**2 return result**(1/2) d = norm 2((1, 0, 3)) 5

 • “return” can be used without a value • Equivalent to “return None”

• “return” can be used without a value • Equivalent to “return None” – None: a special value • When no return statement is given, the function returns None (as if it was the last statement) >>> print("hi")==None hi True >>> type(None) <class 'None. Type'> 6

“return” can be used anywhere in the function and immediately stops its execution. Execution

“return” can be used anywhere in the function and immediately stops its execution. Execution resumes wherever function was called. def find(value, some_list): for item in some_list: if item == value: return True return False 7

How to use functions in our programs? • Bottom up design: take the basic

How to use functions in our programs? • Bottom up design: take the basic operations, build functions from them to use as building blocks to build UP. • Top down design: take the big problem, break it down to sub-tasks. def find_life_on_mars(): build_spaceship() launch_spaceship() land_on_mars() return is_life_on_mars() Now, break down each sub-task into smaller ones. 8

Complex Programs • Programs end up being very complex, with many functions doing various

Complex Programs • Programs end up being very complex, with many functions doing various sub tasks. • Span many files, and may be complex to follow • How does execution proceed? How does the computer keep track? 9

def a(): print("start a()") b() c() print("fin a()") def b(): print("start b()") d() print("fin

def a(): print("start a()") b() c() print("fin a()") def b(): print("start b()") d() print("fin b()") >>> a() start b() start and fin d() fin b() start c() start and fin d() fin c() fin a() The call stack def c(): print("start c()") d() print("fin c()") Function d() Line 1 def d(): print("start and fin d()") Function a() Line 4 Line 2 Line 3 Line 1 Function b() Function c() Line 3 Line 2 Line 1 10

Error messages and the call stack. • When an error occurs within some function,

Error messages and the call stack. • When an error occurs within some function, the error message specifies the state of excution: 11

12

12

Defining Functions • Functions must be defined before they are used. a() #ERROR: a()

Defining Functions • Functions must be defined before they are used. a() #ERROR: a() is not defined yet. def a(): # not executed until a() called: return b() #Okay. a() #ERROR! Because b() isn’t defined. def b(): return 1 a() #This is Okay. 13

Documenting functions def my_add_fun(arg 1, arg 2): """adds two numbers a function that adds

Documenting functions def my_add_fun(arg 1, arg 2): """adds two numbers a function that adds two numbers and returns the result""" return arg 1+arg 2 >>> help(my_add_fun) Help on function my_add_fun in module __main__: my_add_fun(arg 1, arg 2) adds two numbers a function that adds two numbers and returns the result 14

Scope • Variables inside a function are local. They are not recognized or known

Scope • Variables inside a function are local. They are not recognized or known outside of it. – Hides internal structure – Makes function more independent of code outside def a(x): y = x+2 return y a(1) print(x) Error! Print(y) Error! External scope: Function a(): x, y 15

Scope On the other hand, a function knows of variables that are outside. •

Scope On the other hand, a function knows of variables that are outside. • • This is usually very confusing. Try to avoid it. Instead, give needed values as parameters! y=2 External scope: def a(): y print(y) Function a(): y=3 a() Will work, and print the value of y (that is 3) 16

Scope • But, functions do not usually change variables outside. This can be very

Scope • But, functions do not usually change variables outside. This can be very bad for code readability: a=1 b=3 c=4 func_a() Did any of my variables change? Which ones? 17

Scope • Python assumes that if you are assigning a value to a variable

Scope • Python assumes that if you are assigning a value to a variable inside a function, you mean a new local variable (that happens to have the same name): x=1 y=2 def f(x): x=x+1 y=x+1 f(1) print(x) print(y) External scope: x, y Function a(x): x, y External variables unchanged. (Prints 1, 2) 18

Side effects and global variables • If you insist, you can indeed reach out

Side effects and global variables • If you insist, you can indeed reach out and touch an external “global” variable: x=1 def a(): global x x=x+1 a() print(x) This very bad code! Avoid this if you can! Will print 3 19

Scope • Scope can be more complex, and have several nested levels: def f

Scope • Scope can be more complex, and have several nested levels: def f 1(x): def f 2(y): return y**2 return f 2(x)) def f 3(x): print(x) External scope: Function f 1(x): Function f 2(y): Function f 3(x): 20

References, Aliases and Copies 21

References, Aliases and Copies 21

References • It is important to understand how information is represented internally. • Variables

References • It is important to understand how information is represented internally. • Variables do not directly hold a value, but instead hold its address in memory. – a “reference” to the value / object. x = 3 y = “hello” Cell number 22130 “Hello” x: 27191 3 y: 22130 Cell number 27191 22

References How we often think about this: Variables point to the value. x =

References How we often think about this: Variables point to the value. x = 3 y = “hello” (We usually don’t care about the address in memory) x: y: “Hello” 3 23

Assignment copies the address from one variable to the other. x = “hello” y

Assignment copies the address from one variable to the other. x = “hello” y = x print(id(x)) Print(id(y)) x: 22130 Builtin function id will show you the number of the memory cell (in most python implementations) “Hello” y: 22130 24

Lists (and other containers) also work the same way. They reference the values /

Lists (and other containers) also work the same way. They reference the values / objects inside them. x = [1, ”hello”, 2. 0] [2275, 2344, 24551] x: 22130 1 2. 0 “Hello” 25

Lists Implications: x = [1, ”hello”, 2. 0] y = x y[0] = 53

Lists Implications: x = [1, ”hello”, 2. 0] y = x y[0] = 53 print(x[0]) #output will be 53 [2275, 2344, 24551] 2288 x: 22130 y: 22130 1 2. 0 53 “Hello” 26

Lists and Functions can accept lists as parameters: def set_zero(lst, i): lst[i] = 0

Lists and Functions can accept lists as parameters: def set_zero(lst, i): lst[i] = 0 my_list = [1, 2, 3] set_zero(my_list, 1) print(my_list) [2275, 2544, 3551] 2278 my_list: lst: 1 3 0 2 Result: Function alters list, in outer scope, but without having to return it. Must be aware of this when we program! 27

Copying the list In contrast to the previous examples… x = [1, ”hello”, 2.

Copying the list In contrast to the previous examples… x = [1, ”hello”, 2. 0] y = x[: ] Slicing creates a brand new list! y[0] = 53 A “shallow copy” print(x[0]) #output will be 1 [2275, 2344, 24551] x: 22130 1 “Hello” 2. 0 53 y: 42445 [2275, 2344, 24551] [2288 , , ] 28

Mutable and Immutable Types • We can pass immutable types to a function knowing

Mutable and Immutable Types • We can pass immutable types to a function knowing that they will not be changed. def funky_function(tpl): … some code … my_tpl = 1, 2, 3 funky_function(my_tpl) print(my_tpl) 29

Mind benders • What does the following code do? a = [1, 2] b

Mind benders • What does the following code do? a = [1, 2] b = [1, 2] a[0] = b b[0] = a print(a[0]) • Can a list contain itself? 30

Mind benders a is a tuple. It is immutable. a = ([1, 2, 3],

Mind benders a is a tuple. It is immutable. a = ([1, 2, 3], “hi”, 2. 0) a[0][0] = 52 is this going to run? Did we change the contents of the tuple? 31

An Example of a Larger Program • Let’s write a program for the game

An Example of a Larger Program • Let’s write a program for the game “Hangman” • The computer selects a random word from a word list, and the user has to guess it. 32

import random def game_loop(num_guesses_left): word = random. choice(load_word_list("words. txt")) found_list = ['_']*len(word) win =

import random def game_loop(num_guesses_left): word = random. choice(load_word_list("words. txt")) found_list = ['_']*len(word) win = False available_letters_list = get_all_letters() while(not win and num_guesses_left>0): letter = request_guess(word, found_list, available_letters_list, num_guesses_left) if letter in word: print("Great! You found a letter!") found(letter, word, found_list) win = '_' not in found_list else: print(“You guessed wrong!") num_guesses_left -= 1 print_win_loss_message(num_guesses_left, word) 33

def load_word_list(file_name): """ loads the word list from a given text file. The words

def load_word_list(file_name): """ loads the word list from a given text file. The words are assumed to be in a text file, with one word per line. """ file = open(file_name) result = [line. strip() for line in file] return result We did not learn how to work with files just yet, but you can guess what the code does… 34

def request_guess(word, found_list, available_letters_list, num_guesses_left): print_request(word, found_list, num_guesses_left) result = input("please enter a letter

def request_guess(word, found_list, available_letters_list, num_guesses_left): print_request(word, found_list, num_guesses_left) result = input("please enter a letter to guess: ") #insist on getting an unused letter. while result not in available_letters_list: #there is actually a better way to do the next line: print("The available letters are: ", str. join("", available_letters_list)) result = input("please enter a letter: ") #remove the letter from the list of available letters. for i in range(len(available_letters_list)): if(result == available_letters_list[i]): available_letters_list[i] = "_" return result 35

def print_request(word, found_list, num_guesses_left): print("n. Try to guess the word. (“ + str(num_guesses_left) +

def print_request(word, found_list, num_guesses_left): print("n. Try to guess the word. (“ + str(num_guesses_left) + " guesses left)") print(str. join(" ", found_list)+"n") 36

def get_all_letters(): begin. Num = ord("a") end. Num = ord("z") return [chr(number) for number

def get_all_letters(): begin. Num = ord("a") end. Num = ord("z") return [chr(number) for number in range(begin. Num, end. Num+1)] 37

def found(letter, word, found_list): for i in range(len(word)): if(word[i]==letter): found_list[i]=letter 38

def found(letter, word, found_list): for i in range(len(word)): if(word[i]==letter): found_list[i]=letter 38

def print_win_loss_message(num_guesses_left, word): if num_guesses_left>0: print("You win! you had", num_guesses_left, "guesses left. ") else:

def print_win_loss_message(num_guesses_left, word): if num_guesses_left>0: print("You win! you had", num_guesses_left, "guesses left. ") else: print("You lost!") print('The word was "'+ word+'"') 39

Function Extras 40

Function Extras 40

Default values for arguments def p_print(text, times=1, p_char="*" ): print(p_char * (len(text)+4)) for i

Default values for arguments def p_print(text, times=1, p_char="*" ): print(p_char * (len(text)+4)) for i in range(times): print(p_char, text, p_char) print(p_char * (len(text)+4)) p_print("hello", 2) p_print("hello", 2, '@') p_print("hello", p_char = '@') p_print(p_char = '@', text = "hello", times=2) p_print(p_char = '@', "hello") #ERROR! 41

Unknown number of arguments • You can define functions that accept as many arguments

Unknown number of arguments • You can define functions that accept as many arguments as the user wishes to provide. • Arguments are placed in a tuple. def is_word_in(word, *text_list): for text in text_list: if word in text: print(word, "is in" , text) is_word_in("run", "rune", "cat", "prune", "apple" , "runner") is_word_in("run") 42

Functions are python objects too! def my_func(): print(“hello”) return “bye” a = my_func b

Functions are python objects too! def my_func(): print(“hello”) return “bye” a = my_func b = my_func() >>> a <function my_func at 0 x 0000033 CC 158> >>> a() hello 'bye' Can be assigned, passed as arguments, returned, stored inside lists, etc! What is this good for? Later in the course… 43

Machine Learning: The Perceptron Algorithm 44

Machine Learning: The Perceptron Algorithm 44

Motivation • An apple farmer wants to build an automated machine to sort the

Motivation • An apple farmer wants to build an automated machine to sort the good apples from the bad ones. 45

 • All apples get weighed on a conveyor belt. • Some are good,

• All apples get weighed on a conveyor belt. • Some are good, some are bad. • An employee manually checked some apples for us, and filled out a table: (sorted by weight): # Weight (grams) is good? 1. 167 +1 2. 140 +1 3. 127 -1 4. 123 +1 5. 108 -1 6. 95 -1 7. 94 -1 46

A Classifier • 47

A Classifier • 47

Using more data • This works well, but some bad apples still make it

Using more data • This works well, but some bad apples still make it through, and some good apples are sometimes thrown away. • The farmer decides to measure more about each apple. He buys a fancy machine that measures volume as well. 48

Weight(g) 106. 4 104. 9 98. 65 90. 77 90. 25 93. 81 95.

Weight(g) 106. 4 104. 9 98. 65 90. 77 90. 25 93. 81 95. 40 82. 91 97. 69 98. 33 97. 27 94. 99 84. 47 102. 7 88. 80 91. 25 105. 2 108. 8 83. 38 94. 42 106. 9 volume(cm^2) 91. 5900 150. 3068 121. 5275 97. 1082 151. 2822 141. 6362 155. 1541 127. 0971 104. 9738 94. 1554 157. 3876 125. 1220 92. 2624 153. 6885 99. 2165 156. 3115 143. 2191 165. 4494 141. 8630 95. 3217 129. 7784 good? -1 +1 -1 -1 +1 +1 +1 -1 -1 -1 +1 +1 +1 -1 -1 +1 Some data 1. Hard to visualize 2. What is a good classification rule? 49

Volume weight 50

Volume weight 50

A Linear Classifier • Consider a straight line that separates the data. Good apples

A Linear Classifier • Consider a straight line that separates the data. Good apples are on one side, bad on the other Volume weight 51

A Linear Classifier • Now, if our data is any good, we can use

A Linear Classifier • Now, if our data is any good, we can use this line for classification. • when the machine measures an apple it can decide for itself: Volume weight 52

 • How do we find the right classification rule? • Where is the

• How do we find the right classification rule? • Where is the best line? How do we express it mathematically? • Can we make the machine look at the data and “learn”? Generalize from the training data? Volume weight 53

Formalization • 54

Formalization • 54

Linear Classifiers • 56

Linear Classifiers • 56

A bit about vectors • 57

A bit about vectors • 57

A bit about vectors • (0, 0) 58

A bit about vectors • (0, 0) 58

A bit about vectors • 59

A bit about vectors • 59

The geometric interpretation • (0, 0) 60

The geometric interpretation • (0, 0) 60

Separating hyperplanes • (0, 0) 61

Separating hyperplanes • (0, 0) 61

Separating hyperplanes (0, 0) 62

Separating hyperplanes (0, 0) 62

The Perceptron Algorithm • 63

The Perceptron Algorithm • 63

Understanding the alg. • If at some point no error is found then indeed

Understanding the alg. • If at some point no error is found then indeed we have a great linear separator. • If the data cannot be linearly separated, the algorithm will run forever – assumes that data is separable! • “Theorem”: if there exists a linear separator, the Perceptron Algorithm finds it! & There are nice theoretical bounds on running time. 64

Intuition 1 (algebraic): • New weight vector New bias The “old” value (negative) A

Intuition 1 (algebraic): • New weight vector New bias The “old” value (negative) A positive value 65

Intuition 2 (geometric): • (0, 0) 66

Intuition 2 (geometric): • (0, 0) 66

Counting the Errors • 67

Counting the Errors • 67

Extras 68

Extras 68

Data That is not Linearly Separable • There may be several approaches: • Find

Data That is not Linearly Separable • There may be several approaches: • Find “best” linear separator possible even when there is no perfect separation • Or, use a more complex function (non-linear) • Voted Perceptron 69

Perceptrons -> Artificial Neural Networks 70

Perceptrons -> Artificial Neural Networks 70

Perceptrons -> Artificial Neural Networks 71

Perceptrons -> Artificial Neural Networks 71

Other things you might want machines to learn • To play games better than

Other things you might want machines to learn • To play games better than humans • To parallel park a car, or fly planes on their own • To recognize faces, handwriting, road signs etc. 72

 • Math input panel in windows 7 73

• Math input panel in windows 7 73