Python (2/3) PU Deep Learning
Total Page:16
File Type:pdf, Size:1020Kb
Python (2/3) PU Deep Learning Nils Reiter, [email protected] April 27, 2021 (Summer term 2021) Recap I Git I Branches, Remotes I Python I Running Python scripts I Python syntax I Data types I Standard library I Exercise 2 I Registrierung der Studienleistung ! Ilias I GitHub Reiter Python (2/3) 2021-04-27 2 / 27 Beratungangebote sind da, damit man sie wahrnimmt I Alle Dozent:innen bieten Sprechstunden an: Termin nach Absprache und online I Studienberatung I Fachspezifisch: Jürgen Hermes, IDH https://dh.phil-fak.uni-koeln.de/mitarbeiterinnen/wissenschaftliche-mitarbeiterinnen/ dr-juergen-hermes I Fachübergreifend: https://verwaltung.uni-koeln.de/abteilung21/ I Schreibzentrum der Philosophischen Fakultät https://schreibzentrum.phil-fak.uni-koeln.de I Psychologische Beratung: Studierendenwerk https://www.kstw.de/beratung/psychologische-beratung I Gewaltschutz I http://www.hilfetelefon.de I Notunterkünfte im Studierendenwerk: https://www.kstw.de I Sozialdienst katholischer (Frauen|Männer) https://www.skf-koeln.de|https://www.skm-koeln.de Ergebnisse der IDH-Umfrage Reiter Python (2/3) 2021-04-27 3 / 27 Ergebnisse der IDH-Umfrage Beratungangebote sind da, damit man sie wahrnimmt I Alle Dozent:innen bieten Sprechstunden an: Termin nach Absprache und online I Studienberatung I Fachspezifisch: Jürgen Hermes, IDH https://dh.phil-fak.uni-koeln.de/mitarbeiterinnen/wissenschaftliche-mitarbeiterinnen/ dr-juergen-hermes I Fachübergreifend: https://verwaltung.uni-koeln.de/abteilung21/ I Schreibzentrum der Philosophischen Fakultät https://schreibzentrum.phil-fak.uni-koeln.de I Psychologische Beratung: Studierendenwerk https://www.kstw.de/beratung/psychologische-beratung I Gewaltschutz I http://www.hilfetelefon.de I Notunterkünfte im Studierendenwerk: https://www.kstw.de I Sozialdienst katholischer (Frauen|Männer) https://www.skf-koeln.de|https://www.skm-koeln.de Reiter Python (2/3) 2021-04-27 3 / 27 Today List Comprehension Classes and Functions Input/Output Exercise Slides will only scratch the surface, reading documentation is necessary! Reiter Python (2/3) 2021-04-27 4 / 27 Section 1 List Comprehension I Sometimes, with ellipsis: S = f1; 2; 3;:::; 100g I Assumption: Recognizable pattern Background: Set-builder notation S = fx j p(x)g I j: »such that«; p(x): predicate I S contains all values of x for which p(x) returns true List Comprehension Background: Defining Sets Enumeration I Explicit listing of set elements: S = f1; 5; 7; 9g Reiter Python (2/3) 2021-04-27 6 / 27 Background: Set-builder notation S = fx j p(x)g I j: »such that«; p(x): predicate I S contains all values of x for which p(x) returns true List Comprehension Background: Defining Sets Enumeration I Explicit listing of set elements: S = f1; 5; 7; 9g I Sometimes, with ellipsis: S = f1; 2; 3;:::; 100g I Assumption: Recognizable pattern Reiter Python (2/3) 2021-04-27 6 / 27 I S contains all values of x for which p(x) returns true List Comprehension Background: Defining Sets Enumeration I Explicit listing of set elements: S = f1; 5; 7; 9g I Sometimes, with ellipsis: S = f1; 2; 3;:::; 100g I Assumption: Recognizable pattern Background: Set-builder notation S = fx j p(x)g I j: »such that«; p(x): predicate Reiter Python (2/3) 2021-04-27 6 / 27 List Comprehension Background: Defining Sets Enumeration I Explicit listing of set elements: S = f1; 5; 7; 9g I Sometimes, with ellipsis: S = f1; 2; 3;:::; 100g I Assumption: Recognizable pattern Background: Set-builder notation S = fx j p(x)g I j: »such that«; p(x): predicate I S contains all values of x for which p(x) returns true Reiter Python (2/3) 2021-04-27 6 / 27 I Logical or: S = fx j p1(x) _ p2(x)g I S contains all values of x for which p1(x) or p2(x) is true I Domain of x: S = fx j x 2 X ^ p(x)g I S contains all elements of X, for which p(x) is true Examples fx j x + x = x ∗ x ^ x 2 Ng = f2g fx j x 2 N ^ x < 5g = f1; 2; 3; 4; 5g fx j x = y ∗ 2 ^ y 2 Yg = f2; 4; 6g if Y = f1; 2; 3g List Comprehension Background: More Complex Predicates I Logical and: S = fx j p1(x) ^ p2(x)g I S contains all values of x for which p1(x) and p2(x) is true Reiter Python (2/3) 2021-04-27 7 / 27 I Domain of x: S = fx j x 2 X ^ p(x)g I S contains all elements of X, for which p(x) is true Examples fx j x + x = x ∗ x ^ x 2 Ng = f2g fx j x 2 N ^ x < 5g = f1; 2; 3; 4; 5g fx j x = y ∗ 2 ^ y 2 Yg = f2; 4; 6g if Y = f1; 2; 3g List Comprehension Background: More Complex Predicates I Logical and: S = fx j p1(x) ^ p2(x)g I S contains all values of x for which p1(x) and p2(x) is true I Logical or: S = fx j p1(x) _ p2(x)g I S contains all values of x for which p1(x) or p2(x) is true Reiter Python (2/3) 2021-04-27 7 / 27 Examples fx j x + x = x ∗ x ^ x 2 Ng = f2g fx j x 2 N ^ x < 5g = f1; 2; 3; 4; 5g fx j x = y ∗ 2 ^ y 2 Yg = f2; 4; 6g if Y = f1; 2; 3g List Comprehension Background: More Complex Predicates I Logical and: S = fx j p1(x) ^ p2(x)g I S contains all values of x for which p1(x) and p2(x) is true I Logical or: S = fx j p1(x) _ p2(x)g I S contains all values of x for which p1(x) or p2(x) is true I Domain of x: S = fx j x 2 X ^ p(x)g I S contains all elements of X, for which p(x) is true Reiter Python (2/3) 2021-04-27 7 / 27 List Comprehension Background: More Complex Predicates I Logical and: S = fx j p1(x) ^ p2(x)g I S contains all values of x for which p1(x) and p2(x) is true I Logical or: S = fx j p1(x) _ p2(x)g I S contains all values of x for which p1(x) or p2(x) is true I Domain of x: S = fx j x 2 X ^ p(x)g I S contains all elements of X, for which p(x) is true Examples fx j x + x = x ∗ x ^ x 2 Ng = f2g fx j x 2 N ^ x < 5g = f1; 2; 3; 4; 5g fx j x = y ∗ 2 ^ y 2 Yg = f2; 4; 6g if Y = f1; 2; 3g Reiter Python (2/3) 2021-04-27 7 / 27 Lists in Python 1 # an empty list 2 x = [] 3 # a list with two elements 4 x = [1, 2] 5 # a list with three elements of different types 6 x = [1, True, "Hello"] Enumeration of list elements List Comprehension List Comprehension I Python-specific! I Special syntax for specifying lists I Similar to set-builder notation Reiter Python (2/3) 2021-04-27 8 / 27 Enumeration of list elements List Comprehension List Comprehension I Python-specific! I Special syntax for specifying lists I Similar to set-builder notation Lists in Python 1 # an empty list 2 x = [] 3 # a list with two elements 4 x = [1, 2] 5 # a list with three elements of different types 6 x = [1, True, "Hello"] Reiter Python (2/3) 2021-04-27 8 / 27 List Comprehension List Comprehension I Python-specific! I Special syntax for specifying lists I Similar to set-builder notation Lists in Python 1 # an empty list 2 x = [] 3 # a list with two elements 4 x = [1, 2] 5 # a list with three elements of different types 6 x = [1, True, "Hello"] Enumeration of list elements Reiter Python (2/3) 2021-04-27 8 / 27 3 4 l1 = ["the", "dog", "barks"] 5 l2 = [x for x in l1 if "e" in x] # ["the"] List Comprehension List Comprehension Examples 1 l1 = [1,2,3] 2 l2 = [x*2 for x in l1] # [2,4,6] Reiter Python (2/3) 2021-04-27 9 / 27 List Comprehension List Comprehension Examples 1 l1 = [1,2,3] 2 l2 = [x*2 for x in l1] # [2,4,6] 3 4 l1 = ["the", "dog", "barks"] 5 l2 = [x for x in l1 if "e" in x] # ["the"] Reiter Python (2/3) 2021-04-27 9 / 27 List Comprehension List Comprehension Formal [ expr for x in list for/if … ] optional I Expression expr is evaluated in context of for loop I Expression can be full-fledged python expression e.g., another list comprehension! I One for loop is required, more are optional I expr often uses x, but doesn’t have too I list needs to be a list (or coercible into one) I Evaluates to a list (and the whole thing is an expression) Reiter Python (2/3) 2021-04-27 10 / 27 demo Section 2 Classes and Functions 1 def myFunction(arguments): 2 # execute code 3 return Classes and Functions Functions I Sub-programs in your program I Built-in functions: print(...), input(...), str(...),… I Custom functions: Defined by yourself Reiter Python (2/3) 2021-04-27 13 / 27 Classes and Functions Functions I Sub-programs in your program I Built-in functions: print(...), input(...), str(...),… I Custom functions: Defined by yourself 1 def myFunction(arguments): 2 # execute code 3 return Reiter Python (2/3) 2021-04-27 13 / 27 1 def myFunction(arg1, arg2, arg3): 2 # do something 3 return Classes and Functions Function Arguments I Arguments are (like everything else) dynamically typed I Multiple arguments are listed with a comma Reiter Python (2/3) 2021-04-27 14 / 27 Classes and Functions Function Arguments I Arguments are (like everything else) dynamically typed I Multiple arguments are listed with a comma 1 def myFunction(arg1, arg2, arg3): 2 # do something 3 return Reiter Python (2/3) 2021-04-27 14 / 27 1 def myFunction(arg1, arg2="bla"): 2 print(arg2) 3 return Classes and Functions Function Arguments Type checking and default values Listing: Type Checking 1 def myFunction(arg1, arg2): 2 if not isinstance(arg1, str): 3 # throw exception 4 raise TypeError 5 # do something 6 return Reiter Python (2/3) 2021-04-27 15 / 27 Classes and Functions Function Arguments Type checking and default values Listing: Type Checking 1 def myFunction(arg1, arg2): 2 if not isinstance(arg1, str): 3 # throw exception 4 raise TypeError 5 # do something 6 return 1 def myFunction(arg1, arg2="bla"): 2 print(arg2) 3 return Reiter Python (2/3) 2021-04-27 15 / 27 Classes and Functions Function Arguments Named arguments Argument named can be used when calling a function 1 def myFunc(a1, a2): 2 # do something 3 return 4 5 myFunc(1,2) # a1 = 1, a2 = 2 6 myFunc(a2=1, a1=2) # a2 = 1, a1 = 2 Reiter Python (2/3) 2021-04-27