E Catalog for Roll Forming Machine, from Toppo Machinery. Co., Ltd
Python Programming Language Reference -...
Transcript of Python Programming Language Reference -...
Python Programming Language Reference
Daniel ToppoPictet Asset Management
20192 – Financial Econometrics 2
Spring 2017
Python
2Intro Lecture
The language basics
Python language – Basics
3Intro Lecture
Sample code #1# This is a comment>>> s1 = "Hello" # 's1' holds 'Hello'>>> s2 = " World" # 's2' holds ' World'>>> print (s1 + s2) # string concatenation'Hello World'
>>> x = (1+2)*3.4 # simple arithmetic operation>>> print (x) # let's print x in the terminal10.2
>>> for i in [1,2,3]: # loop! statement ends with a semi-colon… print (i) # we are in the loop block -> take care of indentation!123>>> if i==3: # condition! statement ends with a semi-colon… print ("bingo!") # we are in the condition block -> indentation again!'bingo'
Python language – Basics
4Intro Lecture
Sample code #1 – Explained! Comments: start with a dash (#)
print will print values on the terminal
Variables are assigned with the = identifier
Compare variables with the == identifier
Boolean (aka logical) operators: and | or | not
Arithmetic operators: + | - | * | / | %
Strings operators:o + concatenationo * repetitiono % when formatting strings
Python language – Basics
5Intro Lecture
Comments Starts with a dash (#) Everything that is right to the # is ignored by the interpreter To span a comment on multiple lines, every line must start
with a dash (#)
# This is a comment – completely ignored by the interpreterprint("Hello World!") # this is another comment
# This is a comment which# is spanned over multiple# lines
Python language – Basics
6Intro Lecture
Identifiers and Keywords A python identifier is a name used to define
o a variableo a functiono a module (file containing Python statements & definitions)o a Class (i.e. a template, specification representing an object)
An identifier starts with o a letter [a-z], or an underscore (_) ([A-Z] for Classes)o followed by letters | underscores | digitso punctuation characters are NOT allowed within an identifier
x = 1 # valid1abd = "Rainbow" # invalid – an identifier cannot start with a digit_a_string_ = "Hello" # validlistA1 = ['a', x, "aaa"] # validx@3 = 0 # invalid – @ is not allowed in an identifier
Python language – Basics
7Intro Lecture
Identifiers and Keywords
False assert del for in or while
None break elif from is pass with
True class else global lambda raise yield
and continue except if nonlocal return
as def finally import not try
The table below lists the Python Keywords These cannot be used as ordinary identifiers (i.e., variable |
constant | function | class, etc.) in your programs
Python language – Datatypes & Operators
8Intro Lecture
Python Datatypes – Overview
Data Type ExamplesNumbers int (signed integers) float (real values) complex
103.453.5 + 7.2j
String "Hello"List [1, 2, 3]Tuple (1, "London", 2.2, 6j)Dictionary {"Name" : "Tony", "Age" : 15}
Python language – Datatypes & Operators
9Intro Lecture
Numbers – Types
Type DescriptionInteger Limited precision
Positive | Negative whole number (no fractional part)
Float Real numbers Integer and fractional parts
Complex Real and imaginary parts (a + bj) Parts are floating point numbers j represents the squared root of -1
See Python Reference in the Appendix to this presentation for detailed information about numbers
Python language – Datatypes & Operators
10Intro Lecture
Numbers – FunctionsFunction DescriptionComparison functions int (x) float (x) complex (x, y)
convert x to a integer number convert x to a floating point number convert x and y to a complex number
(real part = x; imaginary part = y)Mathematical functions abs (x) | fabs(x) ceil (x) | floor (x) cmp (x, y) exp (x) | log (x) | log10 (x) min(x1,x2,…) |
max(x1,x2,…) pow (x, y) round (x[,n]) sqrt (x)
absolute value of x: 𝑥𝑥 ceiling of x: 𝑥𝑥 | floor of x: 𝑥𝑥 comparison 𝑒𝑒𝑥𝑥| ln(𝑥𝑥)| 𝑙𝑙𝑙𝑙𝑙𝑙10(𝑥𝑥) min | max of arguments 𝑥𝑥𝑦𝑦 round x to n digits from the decimal
point 𝑥𝑥
Python language – Datatypes & Operators
11Intro Lecture
Numbers – FunctionsFunction DescriptionTrigonometric functions cos (x) | sin (x) | tan (x) acos (x) | asin (x) | atan (x) hypot (x, y) degrees (x) radians (x)
cosine, sine, tangent arccos, arcsin, arctan Euclidian norm: 𝑥𝑥2 + 𝑦𝑦2 convert x from radians to degrees convert x from degrees to radians
Random functions choice (seq) random() seed ([x])
shuffle (list)
pick a random item from sequence generates a random float: 0 ≤ 𝑟𝑟 ≤ 1 Sets the integer starting value for
generating a random floating number Shuffles the items of a list
Python language – Datatypes & Operators
12Intro Lecture
Strings Strings are immutable ordered sequences of characters Represented in a variety of ways:
o Single quoteso Double quoteso Triple quotes (they can span multi lines)
See Python Reference in the Appendix to this presentation for detailed information about strings
aString = "I'm double-quoted!" # Note the embedded 'single' quotesanotherString = 'I am "single-quoted"! ‘ # Note the embedded "double" quotesspannedString = '''I am spanned over
multiple lines!'''
Python language – Datatypes & Operators
13Intro Lecture
String – OperatorsOperators DescriptionSpecific string operators + * [i] [ : ]
in | not in
concatenation repetition slicing : returns the ith character range slicing: returns the characters
within the given range True if the character is | is not in the
given string
Python language – Datatypes & Operators
14Intro Lecture
String – Formatting A string can be formatted using the % operator
>>> print ("Hello! My name is %s and I have %d legs" %("John", 5))Hello! My name is John and I have 5 legs
Operators Description %c %d | %i %e | %E %s %f %u %x | %X
character signed decimal integer exponential notation string floating point number unsigned decimal integer hexadecimal integer
Python language – Datatypes & Operators
15Intro Lecture
String – Functions
Function Description capitalize() count(str) startswith(str) | endswith(str) lower() | upper()
len() find(str)
isalnum() | isalpha() | isdigit()| isnumeric() | isdecimal() | isspace()
lstrip() | rstrip() | strip()
capitalizes the first letter of the string counts the occurrences of str in the
string checks if the string starts | ends with str converts the string to lowercase |
uppercase returns the length of the string finds str in the string. If found, return
the starting index within the string, otherwise, return -1
checks if string is alphanumeric |alphabetic | digit | numeric | decimals | only spaces
removes all leading | trailing whitespaces | leading and trailing whitespaces
Python language – Datatypes & Operators
16Intro Lecture
Sequences & Dictionaries
aString = "Bob" aList = [1, aString, "toto"] # [1, 'Bob', 'toto']aTuple = (1, 2, aString) # (1, 2, 'Bob')aDictionary = {"Name" : aString, "Age" : 52} # {'Age': 52, 'Name': 'Bob'}
Type Description
String Ordered sequence of characters Defined using quotes " " or ' ' Immutable
List Ordered sequence of mixed types Defined using square brackets [ ] Mutable
Tuple Ordered sequence of mixed types Defined using parenthesis ( ) Immutable
Dictionary Unordered sequence of key-value items Defined using curly braces { } Mutable
Python language – Datatypes & Operators
17Intro Lecture
List & Tuple – OperatorsOperators DescriptionSpecific string operators
+ * [i] [ : ]
in | not in
concatenation repetition slicing : returns the ith character range slicing: returns the characters
within the given range True if the character is | is not in
the given string
Python language – Datatypes & Operators
18Intro Lecture
List & Tuple – FunctionsFunction DescriptionList & Tuple methods
cmp (list1, list2) len (list) max (list) | min (list) list (seq) | tuple (seq)
compares element of list1 and list2 returns the total length of list return the item of list with the max | min
value converts a tuple into a list & vice-versa
List only methods
append (obj) | extend (seq) insert (obj, index) count (obj) index (obj) remove (obj) | pop ()
reverse () | sort ([func])
appends obj | seq to the list insert obj at the given index counts how many time obj is in the list returns the lowest index of obj in list removes obj from the list | removes and
returns the last object from the list reverses elements of the list | sorts the list
using a comparing function func if given
Python language – Datatypes & Operators
19Intro Lecture
Dictionary – FunctionsOperators Description cmp (dict1, dict2) len (dict) str (dict) list (seq) | tuple (seq)
dict.clear() dict.copy() dict.get(key)
dict.has_key(key) dict.keys() | dict.values() dict.items()
dict.update(dict2)
compares element of dict1 and dict2 returns the total length of dict returns a string representation of dict converts a tuple into a list & vice-versa
removes all elements of of dict returns a copy of dict returns the value of dict associated to
the key key checks if dict has key as key returns a list of dict's keys | dict's values returns a list of dict's elements (keys,
values) adds dict2 elements to dict
Python language – Datatypes & Operators
20Intro Lecture
Python Datatypes – Mutability Immutable : the values of the sequence (tuple | string) CANNOT
be modified Mutable : the values of a list CAN be modified
o the list references the SAME memory location after modification
o list are slower than immutable sequences>>> aString = "Hello" # 'aString' is immutable, it CANNOT be changed!>>> aString[0] = "h" # the interpreter will not let you do that…
Traceback (most recent call last):TypeError : 'str' object does not support item assignment
>>> aList = [1, aString, "toto"]>>> anotherList = aList # 'anotherList' points to 'aList' -> [1, 'Hello', 'toto']>>> aList[2] = "titi" # [1, 'Hello', 'titi']>>> anotherList # [1, 'Hello', 'titi']
Python language – Datatypes & Operators
21Intro Lecture
Python Operators – Overview
Type of operator OperatorsArithmetic + (Addition) | - (Substration)
* (Multiplication) | / (Division)% (Modulus) | ** (Exponent)
Boolean (Logical) and | or | not
Comparison == (is equal) | != (not equal)>= (bigger or equal than) | <= (smaller or equal)> (strictly bigger than) | < (strictly smaller than)
Assignment = (main assignment operator)+= (addition assignment) | += (substr. assignment)*= (mult. assignment) | /= (division assignment)%= (modulus assignment) | **= (exp. assignment)
Python language – Datatypes & Operators
22Intro Lecture
Python Operators – Overview
Type of operator OperatorsMembership in (a value is in a specified sequence)
not in (a value is NOT in a specified sequence)
Identity is (two objects are equal, i.e. point to the same memory location)is not (two objects are NOT equal)
Python language – Datatypes & Operators
23Intro Lecture
Python Operators – Overview
Type of operator OperatorsString operators + (concatenation)
* (repetition)[] (slicing) | [:] (range slicing)in | not in (membership)% (formatting)
List | Tuples operators [] (slicing) | [:] (range slicing)in | not in (membership)
Python language – Datatypes & Operators
24Intro Lecture
Sample code #2>>> a = 2 # 'a' holds (i.e. points to) '2'>>> a == 2 # compares 'a' to '2'True>>> a += a**3 # a = a + a^3 = 10>>> a % 4 # 2 (10 = 4*2 + 2)2
>>> s = "Hello">>> 'H' in s # membership operatorTrue>>> s[1:4] # extracts the 1st (included) to the 4th (excluded) characters'ell'>>> s[:2] + s[2:] # slicing and concatenation'Hello'>>> s[-1] # Negative lookup -> from right, starting with -1'o'>>> "H" in s and "s" not in s or ("H" + "e" + "l"*2 + "o") in sTrue
Python language – Datatypes & Operators
25Intro Lecture
Assignment Assignment means binding a name (variable) to a reference
to some objecto A reference is a location in the computer memory that is assigned
to the variableo The variable points to the memory place of the referenced object
When changing the value of the variable, a new object is created into the memory, and the variable points to that new object
variable: a
a = 10a = a + 12
12
MemoryValue: 10
Type: Integer
Value: 22Type: Integer
1
2
Python language – Datatypes & Operators
26Intro Lecture
Assignment Python automatically "knows" the type of the variable based on
the assigned objecto there is no need to define the type when defining a variableo this process is called type inference
If you try to access a variable before having created it -> error!
x = 1 # 'x' is of type 'integer'_a_string_ = "Hello" # a 'string'listA1 = ['a', x, "aaa"] # a 'list't = (1, 2, 3) # a 'tuple'aDict = {"Name" : "Bob", "Age" : 52} # a 'dictionary'
>>> zTraceback (most recent call last):File "<ipython-input-4-a8a78d0ff555>", line 1, in <module>
NameError: name 'z' is not defined
Python language – Control flows
27Intro Lecture
Conditional execution The if statement is used for conditional execution The statements within the if expression belong to a new group of
statements The group of statements
o starts with a semi-colon ":"o has indentation (using the <tab> key)
x = random()if (x < 0.5): # take care of the semi-colon ':' after the statement!
print ("x is lower than 0.5!") # and of the indentation
if (x > 0.5): # let's start a new block of statementsif (x < 0.75): # a nested 'if ' condition!!
print ("x is lower than 0.75!") # only executed if x < 0.75!!print ("x > 0.5") # executed if x > 0.5
Python language – Control flows
28Intro Lecture
Conditional execution An if statement can be followed by
o one (or many) optional elif statement(s), which evaluate a new boolean expression and execute when the previous booleanexpression is False
o one (or many) optional else statement(s), which execute when the last boolean expression is evaluated to False
The (optional) else statement is the last element of the if-elif-else chain
else and elif statements cannot live on their own, they must follow an if statement!
if (x < 0.5) : # take care of the semi-colon ':' after the statement!print ("x is lower than 0.5!") # and of the indentation
elif (x >= 0.5 and x < 0.75) : # evaluated when 0.5 <= x < 0.75print ("x is greater than or equal to 0.5 but smaller than 0.75!")
else :print ("x is big!") # evaluated when x >= 0.75
Python language – Control flows
29Intro Lecture
Loops Loops are blocks of code executed several times Like conditional statements, the loop block
o starts with a semi-colon ":"o has indentation (using the <tab> key)
Type of loop Descriptionfor loop Executes a block of statements a
specific number of times depending on the value of a counter
while loop Executes a block of instructions while a certain condition is being True
The condition is tested before executing the block of statements
Python language – Control flows
30Intro Lecture
Loops
# a 'while' loopx = 1while x < 10 : # semi-colon ':' after the statement -> start of a new block
print ("x is lower than 10") # indentation!x += 1 # increment x -> the condition is met
while x<10
# a 'for' loopaRange = ['a', 'b', 'c', 'd']for x in aRange : # semi-colon ':' after the statement -> start of a new block
print ("Current element: " + x)
Python language – Control flows
31Intro Lecture
Loops Loops also have specific clauses:
o break : terminates the nearest enclosed loop, skipping the optional else clause
o continue: continues with the next cycle of the nearest enclosed loopo else: executed when the loop terminates through exhaustion of the
condition, or when the condition becomes False, but not when the loop is terminated by the break statement
x = 1while x < 10 :
print ("x is " + str (x))x += 1if (x > 5) :
break # loop stops if x > 5!
for s in ['H', 'e', 'l', 'zzzz', 'l', 'o'] : if (s == 'zzzz') :
continue # the loop will skip the 'zzzz' element of the listprint (s)
Python language – Functions
32Intro Lecture
Functions - Definition A function is a named section containing a block of statements
o reusableo performs a specific tasko may return (using the return statement) or not some value
Provides modularity for an application:o defined only onceo can be used when required at several different points in the program
The keyword def defines a function:o followed by the function nameo and the parenthesized list of parameterso and with a final semi-colon ':' indicating the start of the function bodyo the body must be indented (like loops or conditional statements)
An optional string literal as first statement of the body will be interpreted as the function documentation (see example below)
Python language – Functions
33Intro Lecture
Functions - Examplesdef even(n) : # check whether a number is even
"""Checks if a number is even""" # function documentationif (n % 2 == 0) :
print (str (n) + " is even!")else :
print (str (n) + " is not even!")
>>> even (19)19 is not even!
>>> even (224)224 is even!
And here is how to call the function:
Python language – Functions
34Intro Lecture
Functions argumentsA function can be called using the following types of arguments:
Type of argument Descriptionrequired argument mandatory arguments that must match the
function definition must be passed in correct order
keyword argument the arguments are identified by their name arguments can be passed in an arbitrary
order
default argument arguments may have default value if the value is not provided for a default
argument, the default value is used
variable-length arguments arbitrary argument list – used to call the function with an arbitrary list of arguments
defined with an asterisk (*) before their name
Python language – Functions
35Intro Lecture
Functions arguments - Examplesdef letsTalk (name, *args) : # required and variable-length parameters
print (name + " is my name")for arg in args :
print (arg)
>>> letsTalk ("Gigi", 'Hi', 'there!') Gigi is my nameHithere!
def sayMyNameAndAge (name, age=25) : # age has a default valueprint (name + " is " + str (age) + " years old")
>>> sayMyNameAndAge (age=30, name="Gigio") # keyword parametersGigio is my 30 years old
>>> sayMyNameAndAge ("Gigio") # as age is optional, we can skip itGigio is my 25 years old
Python language – Functions
36Intro Lecture
Functions – Scope of variables There are two scopes of variables in Python
o Global variableo Local variable
A variable defined inside the body of a function is only accessible from within the function; it is a local variable
A variable declared outside any function is a global variable; it can be accessed from within any declared function
x = 5 # 'x' is a global variable – defined outside any function, but # available within the body of defined functions below
def f (y) :z = x**2 + y**2 # 'z' and 'y' are local variablesreturn z
def exploreVariables (y) :result = f (4) # 'result' is a local variable
Python language – Modules
37Intro Lecture
Modules – Definition A module is a file grouping Python definitions and statements
of related functionalities
The file can be composed ofo variableso functionso classeso runnable code
A module can then be imported in a script or in an interactive instance of the interpreter
A module called moduleA is stored in a file named moduleA.py
It is sometimes called library in other programming languages
Python language – Modules
38Intro Lecture
Modules – Examples
def positiveSum (*values) : # sums up positive values only result = 0for value in values :
if (value > 0) :result += value
return result
def negatePositiveSum (*values) : # sums up positive values and negates resultreturn -positiveSum(*values)
utils.py # utils module is stored in filename called 'utils.py'
>>> import utils # Here, we import the 'utils' module that is stored in the # 'utils.py' file created above. We can then call any # function declared in the 'utils' module
>>> utils.positiveSum (1, 2, -5)3>>> utils.negatePositiveSum (1, 2, -5)-3
Python language – Modules
39Intro Lecture
Modules – Imports A module can contain executable code (i.e. code not stored in a function),
o these statements are intended to initialize the moduleo they are executed only the first time the module is imported
It is possible to import only specific names from a module, or everything using the * wildcard, or give a specific name to an imported module:
When importing a module, Python searches for the module in the following order:o the current directory;o if the module is not found, Python searches in the directories defined in the
PYTHONPATH environment variable. Usually: Windows: C:\python\lib *IX : /usr/local/lib/python
>>> from utils import positiveSum # Only 'positiveSum' is available>>> from utils import * # Everything is available>>> import utils as utl # Names in utils can now be accessed using
the 'utl' namespace (see below)
Python language – Modules
40Intro Lecture
Modules – Namespaces In Python, a name is an identifier that maps to an object; a name can
be:o a variableo a functiono a classo etc.
A namespace is a space that holds a certain number of names
A module acts as a global namespaceo you CANNOT have two objects in a module that share the same nameo however, you CAN have two objects with the same name in two different
modules>>> import FloatingNumbers as fn>>> import IntegerNumbers as in
>>> fn.sum (1.0, 2.1) # Here, the two modules have distinct namespaces: 'fn', >>> in.sum (1, 2) # and 'in', with a function called 'sum' having the same
# name in both namespaces
Python
41Intro Lecture
Useful External Libraries
Python language – External Libraries
42Intro Lecture
NumPy– Scientific computing The NumPy library allows to compute scientific numerical
calculations with Python
It provides easy management for lists and numbers
Official website: http://www.numpy.org/>>> import numpy as np>>> np.pi # 3.141592653589793
>>> np.array([[1, 2], [3, 4], [5, 6]]) # Create a multidimensional array [[1 2][3 4][5 6]]
>>> np.arange(0, 10, 2) # Create a range that starts from 0 to 10, with step = 2 [0 2 4 6 8]
>>> np.linspace(0, 10, 6) # Create a range that starts from 0 to 10, having 6 elements[ 0. 2. 4. 6. 8. 10.]
Python language – External Libraries
43Intro Lecture
NumPy– Arrays creation & manipulations>>> A = np.arange(1, 6, 1) # array([1, 2, 3, 4, 5])>>> B = np.zeros((5, 5)) # a (5x5) matrix composed of zeros >>> C = np.ones((4, 6)) # a (4x6) matrix composed of ones>>> D = np.eye(3) # a (3x3) identity matrix>>> E = np.random.rand(3, 2) # a (3x2) matrix composed of random numbers
# IMPORTANT! Arrays (lists) start with index = 0!>>> A[2:4] # from elements 3 (inclusive) to 5 (exclusive)[3 4]>>> A[3:] # from elements 4 (inclusive) to the end (inclusive)[4 5]
>>> E[2] # third row of 'E'>>> E[:,0] # first column of 'E'>>> E[2, 1] # element at 3rd row, 2nd col of 'E'>>> E[0:2, :] # 1st and 2nd rows, with all columns
Python language – External Libraries
44Intro Lecture
NumPy– Linear algebra>>> import numpy as np>>> A = np.array([[1, 2], [3, 4], [5, 6]])>>> B = np.array([[2, 3], [4, 5], [6, 7]])>>> np.size (A) # Returns the number of elements in the matrix 6>>> np.shape (A) # Returns the 'shape' of the matrix, i.e. the size of the matrix(3, 2)
>>> A*B # Element-wise multiplication! 'A' and 'B' must have the same shape[[ 2 6][12 20][30 42]]
>>> C = np.array([[1, 2, 3], [4, 5, 6]])>>> np.dot(A,C) # Matrix multiplication![[ 9 12 15][19 26 33][29 40 51]]
>>> A.T # Transpose 'A'[[1 3 5][2 4 6]]
Python language – External Libraries
45Intro Lecture
NumPy– Linear algebra>>> A = np.array([[1, 2], [3, 4]]) # Let 'A' be a squared matrix
>>> np.linalg.det(A) # 'A' determinant-2
>>> np.linalg.inv(A) # Inverse of 'A'[[-2. 1. ][ 1.5 -0.5]]
>>> D, V = np.linalg.eig(A) # Eigenvalues and Eigenvector of 'A'[-0.37228132 5.37228132] # Eigenvalues [[-0.82456484 -0.41597356] # Eigenvector [ 0.56576746 -0.90937671]]
>>> A = np.array([[1, 2], [3, 4]])>>> B = np.array([[3, 5], [8, 9]]) >>> covmat = np.cov(A, B) # Covariance Matrix
Python language – External Libraries
46Intro Lecture
Matplotlib – Python plotting Matplotlib is a 2D / 3D Python plotting library
It makes heavy use of NumPy.
Large number of types of plots available:o Line ploto Histogramso Bar chartso Pie chartso Polar plotso Error chartso Scatter plotso Path plotso Streamplotso Etc.
o Official website: http://matplotlib.org/
Python language – External Libraries
47Intro Lecture
Matplotlib – Plot simple lineimport numpy as npimport matplotlib.pyplot as plt # Let's import the plotting library
x = np.linspace(0, 2*np.pi, 100)y = np.sin(x)plt.plot(x, y) # plots sin(x) between 0 and 2*pi
Python language – External Libraries
48Intro Lecture
Matplotlib – Format plot
x = np.linspace(0, 2*np.pi, 100)y = np.sin(x)plt.plot(x, y, "r:o") # format the line (red dots)
A plot can be formatted in multiple different ways Details on how to format the plot can be found at:
http://matplotlib.org/api/lines_api.html
x = np.linspace(0, 2*np.pi, 100)y = np.sin(x)plt.plot(x, y, color="purple", linewidth = 10)
Python language – External Libraries
49Intro Lecture
Matplotlib – Axes, title and legendx = np.linspace(0, 2*np.pi, 100)y = np.sin(x)plt.plot(x, y, label="sin(x)") # plots sin(x) between 0 and 2*pi, and adds a legend
plt.title("sin(x) function plot") # a titleplt.legend() # actually plots the legend! plt.xlabel("x values") # x axis labelplt.ylabel("sin(x) values") # y axis label
plt.xlim(2, 5) # fixes x axis values to [2, 5] rangeplt.ylim(-2, 2) # fixes y axis values to [2, 2] range
Python language – External Libraries
50Intro Lecture
Matplotlib – Multiple plotsx = np.linspace(0, 2*np.pi, 100)y1 = np.sin(x)y2 = np.cos(x)plt.plot(x, y1, label="sin(x)") # plots sin(x) plt.plot(x, y2, label="cos(x)") # plots cos(x) plt.legend()
x = np.linspace(0, 2*np.pi, 100)y1 = np.sin(x)y2 = np.cos(x)plt.subplot(211) # 2 rows x 1 col subplotsplt.plot(x, y1, "ro") # plots sin(x) in 1st subplotplt.subplot(212) # 2 rows x 1 col subplotsplt.plot(x, y2, "b--") # plots cos(x) in 2nd subplot
Python language – External Libraries
51Intro Lecture
Pandas – Data Analysis Library Pandas provides high-performance, easy-to-use data structures
and data analysis tools for Python
Libraries functionalities:o DataFrame object: to manipulate data with indices that can be dates,
strings, etc.o Tools allowing to read / write data from / to Microsoft Excel, CSV files,
text files, SQL database, etc.o Direct data plottingo Intelligent data alignment on index, along with management of
missing datao Creation of pivot tableso Fusing and joining large datasetso Time series analysis
Official website: http://pandas.pydata.org/
Python language – External Libraries
52Intro Lecture
Pandas – Read data from Excel fileimport pandas as pd
dataFile = "salaries.xlsx" # data is defined in Excel filexslx = pd.ExcelFile(dataFile) # creates an xslx object
sheetName = "salaries" # The sheet name where the data is stored
data = xslx.parse(sheetName) # Actually parse the sheet and store thedata in a DataFrame object
dataFile2 = "sales.csv" # data is defined in 'CSV' filedata2 = pd.read_csv(dataFile2)
Python language – External Libraries
53Intro Lecture
Pandas – View datadates = pd.date_range('20161231', periods=31) # creates an array of dates starting
31 dec, 2016 of 31 days
data = pd.DataFrame(np.random.randn(31, 3), index=dates, columns=["A", "B", "C"])
data.head() # Prints the first 5th elements of 'data'A B C
2016-12-31 0.329 1.750 -0.6482017-01-01 -0.619 0.614 -0.0492017-01-02 -0.967 -0.196 0.8282017-01-03 0.796 -0.291 -0.7632017-01-04 -0.661 0.110 -0.284
data.tail() # Prints the last 5th elements of 'data'data.describe() # Prints statistic summary of 'data'data.sort_index(axis=1, ascending=False) # Sort by axis=1 (i.e. by columns)data.sort_values(by='B') # Sort column 'B' values ascendingdata.T # Transpose the data
Python language – External Libraries
54Intro Lecture
Pandas – Select datadata["A"] # Selects the column 'A' from the data
data[0:6] # Slices rows (from 1st row to 5th row)
data.loc["20161231" : "20170102", ['B']] # From 31 dec 2016 to 2 jan 2017, column BB
2016-12-31 1.7502017-01-01 0.6142017-01-02 -0.196
data[data.C > 0] # Boolean indexing -> filters by positive values of column 'C'A B C
2017-01-02 -0.967 -0.286 0.8282017-01-05 0.665 -0.572 0.5022017-01-06 -1.557 -1.305 1.5592017-01-07 0.786 -0.164 1.1522017-01-08 -1.411 0.445 1.075
Python language – External Libraries
55Intro Lecture
StatsModels – Statistics Library StatsModels allows to explore data, estimate statistical models,
and perform statistical tests
Libraries functionalities:o Linear regression modelso Generalized linear modelso Discrete choice modelso Robust linear modelso Nonparametric estimatorso Statistical testso Plotting functionso etc.
Official website: http://statsmodels.sourceforge.net/
Python language – External Libraries
56Intro Lecture
StatsModels – Ordinary Least Squares (OLS)import statsmodels.api as sm
data = pd.read_csv("salaries.csv") # Pandas DataFrame from 'CSV' file
y = data["salary"] # Regressor – dependent variableX = data["yearsOfService"] # Predictor – independent variable
model = sm.OLS(y, X).fit() # Fits a simple ordinary least squares modelmodel.summary() # Display summary of regression results
Python language – External Libraries
57Intro Lecture
SciPy – Scientific Python SciPy is a library for scientific computing
Libraries functionalities:o Numerical integrationo Optimizationo Interpolationo Fourrier Transformso Signal Processingo Statisticso etc.
Official website: http://scipy.org/
Python language – External Libraries
58Intro Lecture
SciPy– Optimizationfrom scipy import optimize
x_root = np.random.random()y_root = np.random.random()
def f (params) : # defines the function to optimizex, y = paramsreturn (x-x_root)**2 + (y-y_root)**2
initParams = np.array([0.5, 0.5]) # initial guess
optimize.minimize(f, initParams, method="SLSQP", bounds=bnds) # let's minimize f!
fun: 4.5442939897139043e-09message: 'Optimization terminated successfully.'success: Truex: array([ 0.12439444, 0.61272226])
Python language – External Libraries
59Intro Lecture
SciPy– Optimization with constraintsfrom scipy import optimize
initParams = np.array([0.5, 0.5]) # initial guess
bnds = ((0, 1), (0, 1)) # bounds for x and y : [0, 1] (inclusive!)
def constraints(params): # A constraint function: 0.5 - (x + y) >= 0x, y = paramsreturn 0.5 - (x + y)
cons = {'type' : 'ineq', 'fun' : constraints} # Here, we define the type of constraint - an# inequality - along with the function# defining the constraint
optimize.minimize(f, initParams, method="SLSQP", bounds=bnds, constraints=cons)
x: array([ 0.48928464, 0.01071536])
Python
60Intro Lecture
Installing and Running
Installing and running
61Intro Lecture
Anaconda Environment Open data science platform Includes Python distribution Available platforms:
o Windowso Mac OSXo Linux
Includes 100+ scientific packages Spyder IDE (Integrated Development Environment) Download at https://www.continuum.io/downloads
Python Shell - Interactive Interpreter
62Intro Lecture
Launched via any Windows / *IX terminalo Type "python" to launch the interpretero To exit the interpreter, type "exit()", or
in *IX terminal: CTRL-D in Windows terminal: CTRL-C or CTRL-Z
The interpreter welcomes you with the ">>>" prompto Allows to evaluate user inputs
Use the terminal to launch python scripts (".py" files):o $ python script.py
Spyder IDE
63Intro Lecture
Text editor with syntax color highlighting and smart indenting -> eases and speeds developments
Shell (Python and IPyhton consoles) for displaying results and interactive evaluation
Data / Files explorers Code analyzer Contextual help Code debugging Etc.
Python
64Intro Lecture
References
Python - References
65Intro Lecture
Online documentation Official Python website: https://docs.python.org/3/ Wiki-Book: https://en.wikibooks.org/wiki/Python_Programming Byte of Python: http://www.swaroopch.com/notes/Python Python Bibliotheca: http://openbookproject.net/pybiblio/ Tutorialspoint: https://www.tutorialspoint.com/python Google: https://developers.google.com/edu/python/ Pythonspot: https://pythonspot.com/ Pythonlearn: http://www.pythonlearn.com/book.php Python for Fun: http://www.openbookproject.net/py4fun/ Pandas: http://pandas.pydata.org/ Numpy: http://www.numpy.org/ Scipy: https://www.scipy.org/
Python - References
66Intro Lecture
BooksMark LutzLearning Python, 5th EditionO'Reilly Media; 5 edition (July 6, 2013)
Paul JonesPython: The Fundamentals Of Python ProgrammingCreateSpace Publishing Platform (Oct. 26, 2016)
Eric MatthesPython Crash Course: A Hands-On, Project-Based Introduction to ProgrammingNo Starch Press; 1 edition (November 30, 2015)
Python - References
67Intro Lecture
Books
Yves HilpischPython for FinanceO′Reilly (Dec. 30, 2014)
Wes McKinneyPython for Data AnalysisO'Reilly Media (Jul. 31, 2016)