python-intro
python-intro
October 9, 2019
Felix Biessmann
Lecture 1 - Introduction
2 Short intro
• Name
• Educational backgound (Physics/Computer Science/…?)
• Work experience
• Experience with Python? If yes, what project?
• I’m Felix Biessmann (ß = ss)
• BSc in Cognitive Science
– Computational Linguistics
– Neuroscience
• MSc in Neuroscience
– Brain-Computer-Interfaces
– Functional Magnetic Resonance Imaging data
• PhD in Machine Learning
– Biomedical Applications
– Multimodal neuroimaging data
– Text analysis on web data
• Assistant Professor for Machine Learning at Korea University, Seoul
• Amazon Research
– Computer Vision
– Recommender Systems
– Machine Learning Infrastructure
– ML for Data Quality
• Einstein Center for Digital Future / Beuth
– Data Quality
1
– ML for Humans
3 Why Python?
3.0.1 Popularity
3.0.2 Popularity
https://stackoverflow.blog/2017/09/06/incredible-growth-python/
3.0.3 Popularity
According to a comprehensive stackoverflow survey Python is the most wanted language for the
second year in a row, meaning that it is the language that developers who do not yet use it most
often say they want to learn.
3.0.4 Popularity
• Simple
• Versatile
• No boilerplate code
• De facto standard for Data Science / Machine Learning
• All technical interviews I made at Amazon: Candidates chose Python
3.0.5 Libraries
• NumPy
• SciPy
• Matplotlib
• IPython
• Pandas
2
• sklearn
• tensorflow
• pytorch
• mxnet
• …
3.0.6 Time
Python lovers are quick to point out how intuitive Python is.
But intution is strongly related to familiarity.
Many developers used to traditional (compiled, typed) languages won’t find Python intuitive.
If you want to dive deep into the philosophy behind Python, try this easter egg in a Python shell:
[1]: import this
• Most systems come with (an out-dated version of) python pre-installed
3
• Download binaries for your system at https://www.python.org/downloads/
• You can use system specific package managers (apt for linux, homebrew for OSX)
• Or you use Anaconda (preferred if you don’t like fiddling with installations)
4.2 Python 2 vs 3
5 Virtual Environments
• For different projects you will need different dependencies (or versions thereof)
• It is good practice to encapsulate your dependencies in a virtual environment
• Virtualenv (python documentation)
python3 -m venv [path_to_venv]
source [path_to_venv]/bin/activate
• Anaconda
conda create -n my_env python=3.7
conda activate my_env
To avoid cluttered dependencies, you might want to install package only in a virtual environment,
not system-wide.
• For plain python
– pip install [packagename]
• For Anaconda installations
– conda install [packagename]
4
7 How to Run Python Code
Open a terminal (e.g. by hitting cmd-space and type terminal and enter)
Create a new virtual environment and activate it
python3 -m venv venv
source venv/bin/activate
Upgrade the python package manager and install jupyter
pip install --upgrade pip
pip install prompt_toolkit==2.0.3
pip install jupyter
5
7.4 The IPython Interpreter
In [1]:
Visual difference: - Python uses >>> - IPython uses numbered commands (e.g. In [1]:)
ipython has the same functionalities as a standard python shell
In [1]: 1 + 1
Out[1]: 2
In [2]: x = 5
In [3]: x * 3
Out[3]: 15
… and many more
6
7.8 Some Nice IPython Functionalities
In [8]: b = [1, 2, 3]
In [9]: b?
Type: list
String Form:[1, 2, 3]
Length: 3
Docstring:
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
In [10]: print?
Docstring:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Returns
-------
the_sum : type of arguments
"""
return a + b
In [11]: add_numbers?
Signature: add_numbers(a, b)
Docstring:
Add two numbers together
Returns
7
-------
the_sum : type of arguments
File: <ipython-input-9-6a548a216e27>
Type: function
import numpy as np
np.*load*?
np.__loader__
np.load
np.loads
np.loadtxt
np.pkgload
In [17]: %paste
x = 5
y = 7
if x > 5:
x += 1
y = 8
## -- End pasted text --
[2]: %timeit x = 4 + 5
14.8 ns ± 0.206 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
<ipython-input-185-48a597a45538> in foo()
8
1 def foo():
2 bar = [3] # bar is a list with one entry
----> 3 return bar[5] # this tries to return the 6th entry, which will not work
ipdb> bar
[3]
9
• remote execution
• inline plotting
• interactive plots
• has many problems
• hidden state
• version control is difficult
• testing is difficult
• check out Joel Gru’s Talk
[3]: ### Inline Plotting
from matplotlib import pyplot as plt
import numpy as np
%matplotlib inline
plt.plot(np.sin( np.arange(-np.pi,np.pi,.1)));
10
x = 5
print("Result is", 3 * x)
Go to directory of file and type python filename in a shell:
$ python test.py
Running test.py
Result is 15
Pros - all of IPython’s advantages - inline plotting - code and markdown cells allow better docu-
mentation
Cons - too convenient (for people too lazy to organize code) - hidden state - version control is
difficult
Pros - version control - clean reproducible execution (e.g. no hidden state) - precise timing
Cons - not interactive - debugging more difficult
8 Python Syntax
11
Python does not have multiline comments
But any decent editor will take care of that
8.4 Intendation!
9 Exercises
12
– in a Jupyter notebook
• write a *.py file that, when executed prints Hello World and execute that file
– from a shell
– from a Python shell
– from an IPython shell
– from a Jupyter notebook
13