A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python...

24
A. Python Crash Course

Transcript of A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python...

Page 1: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

A. Python Crash Course

Page 2: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

2

Agenda§ A.1 Installing Python & Co

§ A.2 Basics

§ A.3 Data Types

§ A.4 Conditions

§ A.5 Loops

§ A.6 Functions

§ A.7 I/O

§ A.8 OLS with Python

Machine Learning / Python Crash Course

Page 3: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

3

A.1 Installing Python & Co§ You can download and install

Python directly fromhttps://www.python.org

§ Since we’re going to use several libraries for numericalcomputation (numpy), data analysis (pandas),machine learning (sklearn), andvisualization (matplotlib),it is easier to install Anaconda,which bundles all things requiredhttps://www.continuum.io/downloads

Machine Learning / Python Crash Course

Page 4: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

4

Running Python§ Python can be used interactively; for that it is convenient

to use ipython, which provides syntax highlightingand auto completion

Machine Learning / Python Crash Course

Python 3.6.0 |Anaconda custom (x86_64)| (default, Dec 23 2016, 13:19:00)Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.? -> Introduction and overview of IPython's features.%quickref -> Quick reference.help -> Python's own help system.object? -> Details about 'object', use 'object??' for extra details.

In [1]: x = 2

In [2]: x*2Out[2]: 4

Page 5: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

5

Running Python§ You can also run a file of python code (common suffix: .py)

by giving it as an argument to python or ipython

§ Most editors (e.g., Sublime and Emacs) support directlyexecuting the current file (i.e., sending it to python)

Machine Learning / Python Crash Course

your-machine:~$ python your-file.py

Page 6: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

6

A.2 Basics§ Comments

§ single-line comments: # your short comment§ multi-line comments: ’’’ your long comment ’’’

§ Two important differences between Python and Java

§ Python is dynamically-typed, i.e., the type of a variable is determined at runtime and can change during execution

§ Python uses indentation (i.e., spaces or tabs) insteadto group statements into blocks of code

Machine Learning / Python Crash Course

Page 7: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

7

Dynamic Typing and Indentation§ Dynamic typing

§ Code indentation

Machine Learning / Python Crash Course

x = 2type(x) # returns intx = 'Hello World'type(x) # returns str

if (x % 2 == 0): print("even")

else:print("odd")

Page 8: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

8

A.3 Data Types§ Python supports, among others, the following basic types

§ int for integers (e.g., x = 2)§ float for floating point numbers (e.g., x = 3.14)§ str for strings (e.g., x = ‘Hello World’)§ we can build a tuple from multiple other values

(e.g., c = (49.14, 6.58))

§ In addition, Python supports the following container types

§ list to store multiple values in a particular order§ set to store multiple without order and repetitions§ dict to store key-value pairs

Machine Learning / Python Crash Course

Page 9: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

9

Lists§ Lists

Machine Learning / Python Crash Course

l = [] # create an empty listl.append(2) # insert 2 at the endl.append('x') # insert 'x' at the endl[1] = -2 # replace 'x' by -2l.insert(1,0) # insert 1 at position 1l.sort() # sort l in-place

l = [1,2,3,4,5,6,7] # create new listl.reverse() # reverse list in-placel[:2] # returns first two elements [7,6] l[-2:] # returns last two elements [2,1]l[1:3] # returns second and third element [6,5]

l = [[1,2],[3,4]] # lists can be nestedl[1][0] # returns first element of second list [3]

Page 10: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

10

Sets§ Sets

Machine Learning / Python Crash Course

u = set([]) # create empty set uu.add(2) # add 2 to setu.add('x') # add 'x' to setu.add(2) # add 2 to set -- no effectu.remove('x') # remove 'x' from set

v = set([2,3]) # create another set

union = u | v # compute union: {2,3}intersection = u & v # compute intersection: {2}

Page 11: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

11

Dictionaries§ Dictionaries

Machine Learning / Python Crash Course

c = {} # create an empty dictionaryc[1] = 'c' # associate value 'c' with key 1c[2] = 'b' # associate value 'b' with key 2c[3] = 'a' # associate value 'a' with key 3

k = sorted(c.keys()) # get sorted list of keysv = sorted(c.values()) # get sorted list of values

Page 12: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

12

A.4 Conditions§ Conditions (very similar to Java)

§ Conditionals (similar to (c ? a : b) in Java) exist

Machine Learning / Python Crash Course

if (x == 1):print "two"

elif (x == 2):print "two"

else:print "other"

output = ("even" if x % 2 == 0 else "odd")

Page 13: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

13

A.5 Loops§ For loops are typically used to iterate over the items in a list

§ While loops

Machine Learning / Python Crash Course

# loop over some prime numbersprimes = [1,2,3,5,7,11,13]for prime in primes:

print(str(prime) + " is a prime number")

# loop over the numbers 0, 1, ..., 9for n in range(0,10):print(n)

b = 2 # basee = 10 # exponentr = 1 # resultwhile e > 0:

r = r*b e = e-1

print(r) # prints 1024

Page 14: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

14

A.6 Functions§ Functions can be defined using the keyword def

§ Functions can have more than one return value

Machine Learning / Python Crash Course

def fak(n):if n==1:return 1

else:return n*fak(n-1)

def split_in_halves(l):middle = int(len(l)/2) left_half = l[:middle]right_half = l[middle:]return left_half, right_half

left, right = split_in_halves([1, 2, 3, 4, 5])left # returns [1, 2]right # returns [3, 4, 5]

Page 15: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

15

Functions§ Function arguments can have default values

Machine Learning / Python Crash Course

def greet(m='Hello'):print(m)

greet() # prints "Hello"greet("Hi") # prints "Hi"

Page 16: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

16

Math§ Mathematical functions (e.g., sin and cos) are provided by the

module math, which we first need to import

Machine Learning / Python Crash Course

import math

math.gcd(10,3) # greatest common divisormath.sin(0) # sinemath.cos(0) # cosinemath.floor(3.14) # floormath.ceil(3.14) # ceil

help(math) # more information about the math module

Page 17: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

17

A.7 I/O§ Reading a text file line by line

§ Writing to a text file

Machine Learning / Python Crash Course

import os

file = open('/path/to/your/file')for line in file:

print(line)

file.close()

import os

file = open('/path/to/your/file','w')for i in range(0, 100):file.write(str(i) + "\n")

file.close()

Page 18: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

18

A.8 OLS with Python§ Let’s now look at how we can create the plots from today’s

lecture and apply ordinary least squares

§ Use pandas to read the file autos-mpg.data

which gives us a pandas.core.frame.DataFramecontaining all rows and columns from the file(similar to a table in a relational database)

Machine Learning / Python Crash Course

import pandas as pd

cars = pd.read_csv('/path/to/autos-mpg.data', header=None, sep='\s+')

Page 19: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

19

Extracting the Features of Interest§ The first column contains the mpg values; the fourth column

contains the hp of the car

§ Let’s extract those columns into separate variables

which gives us two numpy.ndarrayas numpy’s array data type

Machine Learning / Python Crash Course

import numpy as np

mpg = cars.iloc[:,0].values # returns a numpy.ndarrayhp = cars.iloc[:,3].values # returns a numpy.ndarray

Page 20: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

20

Plotting the Data§ Next, we can create a scatter plot of the data

which gives us

Machine Learning / Python Crash Course

import matplotlib.pyplot as plt

plt.scatter(hp, mpg, color='blue', marker='x')plt.xlabel('Power [hp]')plt.ylabel('Fuel consumption [miles/gallon]')plt.show()

Page 21: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

21

Fitting a Regression Line§ Now, we can fit a regression line to predict mpg

based on hp

which gives us a model reg with the optimal coefficients

Machine Learning / Python Crash Course

from sklearn import linear_model

x = [] # we need an list of lists herefor i in hp:x.append([i])

y = mpgreg = linear_model.LinearRegression()reg.fit(x,y)

reg.intercept_ # returns 39.93586102117046 as the optimal w0reg.coef_[0] # returns -0.15784473335365357 as the optimal w1

Page 22: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

22

Plotting the Regression Line§ Finally, we plot the regression line in our scatter plot

which gives us

Machine Learning / Python Crash Course

plt.scatter(hp, mpg, color='blue', marker='x')plt.xlabel('Power [hp]')plt.ylabel('Fuel consumption [miles/gallon]')plt.plot(x, reg.predict(x), color='red', linewidth=2)plt.show()

Page 23: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

23

Literature

§ E. Mattes: Python Crash Course,No Starch Press, 2016

§ V. L. Ceder: The Quick Python Book,Manning, 2010

Machine Learning / Python Crash Course

Page 24: A. Python Crash Course · 3 A.1 Installing Python & Co § You can download and install Python directly from  § Since we’re going to use several libraries for numerical

24

References§ Python 3.6.1 Documentation

https://docs.python.org/3/

§ LearnPython.orghttps://www.learnpython.org

§ NumPyhttp://www.numpy.org

§ Python Data Analysis Library (pandas)http://pandas.pydata.org

§ scikit-learnhttp://scikit-learn.org/stable/

§ Matplotlibhttps://matplotlib.org

Machine Learning / Python Crash Course