Slides
Slides
Slides
Tutorial contents:
1. System setup
2. How to python
- Operators, data types, built-in functions
- Functions, modules and classes
- Control structures (conditionals and loops)
- Pythonians: args/kwargs, list comprehensions and iterators
INSTITUT FÜR
2 CONTENTS NEUROINFORMATIK
SYSTEM SETUP
Version information
We will be using Python 3.6.8 with the following packages:
numpy 1.15.4
scipy 0.19.1
matplotlib 2.0.2
sklearn (scikit-learn) 0.19.1
tensorflow 1.12.0
keras 2.2.4
Code will most probably work on other versions as well, but we cannot guarantee
You can use the virtual environment from Moodle. Instructions are provided
below.
INSTITUT FÜR
4 How to python NEUROINFORMATIK
Using the Anaconda environment
●
Download Miniconda (https://docs.conda.io/en/latest/miniconda.html, Python 3.* version)
or Anaconda (https://www.anaconda.com/distribution/, Python 3.* version). In order to
save time and disk space, Miniconda is recommended.
●
Install Anaconda.
Windows: You do not need to add python to your system path in the process.
●
Download the file annvenv.yml from Moodle
●
Open a terminal (Windows: the Anaconda Prompt)
●
In the terminal / Anaconda Prompt, navigate to the directory in which annvenv.yml is
located (use cd dir/to/file)
●
Create the environment with
conda env create -f annvenv.yml
●
Activate it with
conda activate annvenv
INSTITUT FÜR
5 How to python NEUROINFORMATIK
Alternative
Of course you can install python and the packages without using the Anaconda
environment.
INSTITUT FÜR
6 How to python NEUROINFORMATIK
Multiple python versions
●
Use your installed versions – as long as they are newer than the ones we use
in this course it will work most probably (and as long as the primary version
number is identical)
●
Use pyenv to manage multiple python versions. It works on linux and mac.
Information on installation can be found here:
https://github.com/pyenv/pyenv-installer,
for usage see here https://github.com/pyenv/pyenv#table-of-contents and here
https://github.com/pyenv/pyenv/blob/master/COMMANDS.md
INSTITUT FÜR
7 How to python NEUROINFORMATIK
IDE: Spyder
INSTITUT FÜR
8 How to python NEUROINFORMATIK
CIP rooms in ID
On the computers in CIP1 and CIP2 Anaconda and the required packages are
already installed. You do not need to create or activate the environment.
INSTITUT FÜR
9 How to python NEUROINFORMATIK
HOW TO PYTHON
Arithmetic
+ - * / // ** %
Relational
Assignment
= += -= /= […]
Logical
and or not
INSTITUT FÜR
11 How to python NEUROINFORMATIK
Data types
are implicit in python
Numbers
Others
INSTITUT FÜR
12 How to python NEUROINFORMATIK
Accessing list elements
lst = [1, 2, 3, 4, 'potato']
lst[0] lst[-1]
List slicing
Examples
lst[4:1:-2]
lst[2:]
lst[:-1]
lst[::-1]
INSTITUT FÜR
13 How to python NEUROINFORMATIK
More on lists
lst1 = [1, 2, 3]
3 in lst1
4 in lst1
lst1.append([4, 'potato'])
lst1.extend([4, 'potato'])
lst1.pop() or lst1.pop(2)
lst1.index('potato')
etc.
INSTITUT FÜR
14 How to python NEUROINFORMATIK
Dictionaries
Dictionary access
dct['one']
dct.keys() dct.values()
Note:
You can also use numbers or tuples, e.g. (1, 'a') as keys
INSTITUT FÜR
15 How to python NEUROINFORMATIK
Built-in functions
Console output
print()
Type conversion
Numerical
abs()
List properties
Other
INSTITUT FÜR
16 How to python NEUROINFORMATIK
HOW TO PYTHON
File hello.py:
print("Hello World")
python hello.py
INSTITUT FÜR
18 How to python NEUROINFORMATIK
Defining functions
File fun.py:
import fun
fun.inc(1337)
fun.inc()
INSTITUT FÜR
19 How to python NEUROINFORMATIK
Modules
Directory structure:
fun.py
mod/__init__.py (double underscore)
mod/fun2.py
Script fun2.py:
def dec(x):
y = x - 1
return y
import fun
from mod import fun2
a = fun.inc(1337)
fun2.dec(a)
INSTITUT FÜR
20 How to python NEUROINFORMATIK
Classes
File potato.py
def report(self):
print("This potato weighs " + str(self.weight) + " grams.")
import potato
p1 = potato.Potato()
p2 = potato.Potato(250)
p1.report()
p2.report()
INSTITUT FÜR
21 How to python NEUROINFORMATIK
HOW TO PYTHON
Control structures
if statements
def sign(x):
if x < 0:
return -1
elif x > 0:
return 1
else:
return 0
INSTITUT FÜR
23 How to python NEUROINFORMATIK
for loops
or
for i in range(5):
print(i)
INSTITUT FÜR
24 How to python NEUROINFORMATIK
while loops
i = 0
while i < 10:
print(i)
i += 1
Note:
For both types of loops, the break and continue statements are available.
INSTITUT FÜR
25 How to python NEUROINFORMATIK
HOW TO PYTHON
Pythonians
*args, **kwargs
lst = [3, 4, 5]
dct = dict(x=3, a=10, y=4, z=5)
add(*lst)
add(**dct)
INSTITUT FÜR
27 How to python NEUROINFORMATIK
List comprehensions
lst = list(range(10))
[abs(n-5) for n in lst]
INSTITUT FÜR
28 How to python NEUROINFORMATIK
Iterators
Make an iterator from a list
def gen():
i = 0
while True:
yield i
i += 1
g = gen()
next(g)
next(g)
...
INSTITUT FÜR
29 How to python NEUROINFORMATIK
MATRIX CALCULATIONS
WITH NUMPY
What is numpy?
Package containing functions for vector calculations.
import numpy as np
arr[0]
arr.dtype
arrF = arr.astype(float)
INSTITUT FÜR
31 Matrix calculations with numpy NEUROINFORMATIK
Defining arrays
arr1 = np.arange(5)
arr2 = np.zeros(5)
arr3 = np.ones(5)
Shape
arr10 = np.arange(10)
arr10.shape
arr10.reshape((2, 5))
INSTITUT FÜR
32 Matrix calculations with numpy NEUROINFORMATIK
Random numbers
rand1 = np.random.rand(2,5)
rand2 = np.random.randn(10)
INSTITUT FÜR
33 Matrix calculations with numpy NEUROINFORMATIK
Array indexing
Give indices for both dimensions
arr11[0, 4]
arr11[:, 2]
Fancy indexing
arr[ind] = 0
INSTITUT FÜR
34 Matrix calculations with numpy NEUROINFORMATIK
Operations on arrays
a = np.array([[1,3,5], [-5,-7,-9]])
b = np.array([[2,4,6], [6,8,10]])
a+b
a*b
a<b
b-5
etc.
np.abs(a)
np.exp(b)
INSTITUT FÜR
35 Matrix calculations with numpy NEUROINFORMATIK
Basic data processing
a = np.array([[1,3,5], [-5,-7,-9]])
np.max(a)
np.min(a)
np.mean(a)
np.std(a) Note that this is population std. For sample std use np.std(a, dof=1)
np.mean(a, axis=0)
np.mean(a, axis=1)
np.save("a.npy", a)
a1 = np.load("a.npy")
INSTITUT FÜR
36 Matrix calculations with numpy NEUROINFORMATIK
np.nonzero()
arr0 = np.array([[0,1,2,0,3,0],[0,4,5,0,0,6]])
nnz = np.nonzero(arr0)
nnz
arr0[nnz]
arr10 = np.arange(10)
nnz10 = np.nonzero(np.abs(arr10-5) > 2)
nnz10
arr10[nnz10]
INSTITUT FÜR
37 Matrix calculations with numpy NEUROINFORMATIK
Also useful
np.sum(np.abs(arr10-5) > 2)
np.array_equal(a, b)
t1 = np.isnan(a)
t2 = np.isinf(b)
any / all
np.any(t1)
np.all(t2)
INSTITUT FÜR
38 Matrix calculations with numpy NEUROINFORMATIK
Inner and outer product
(inner
product)
(outer
product)
INSTITUT FÜR
39 Matrix calculations with numpy NEUROINFORMATIK
PLOTTING WITH PYPLOT
Plot
import matplotlib.pyplot as plt
x = np.arange(20)-10
y = np.abs(x)
plt.plot(x,y)
plt.show()
Subplot
plt.subplot(1,2,1)
plt.plot(x,y)
y2 = np.exp(x)
plt.subplot(1,2,2)
plt.plot(x,y2)
plt.show()
INSTITUT FÜR
41 Plotting with pyplot NEUROINFORMATIK
Scatter (and more...)
xr = np.random.randn(1000)
yr = np.random.rand(1000)
plt.scatter(xr,yr,label="Data")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.title("This is very useful")
plt.legend()
plt.savefig("example.pdf")
plt.show()
INSTITUT FÜR
42 Plotting with pyplot NEUROINFORMATIK
Bar
x = [0, 1]
y = [0.5, 0.7]
std = [0.2, 0.25]
plt.bar(x, y, width=0.8, yerr=std)
plt.show()
INSTITUT FÜR
43 Plotting with pyplot NEUROINFORMATIK
Imshow
m = np.eye(10)
plt.imshow(m, interpolation='none', cmap="Greys")
plt.show()
INSTITUT FÜR
44 Plotting with pyplot NEUROINFORMATIK
More options
plt.imshow(m, interpolation='none',
cmap="Greys")
plt.xlim([-10,20])
plt.ylim([15,-5])
plt.xticks([0,4,8,12])
plt.yticks([1,5])
plt.show()
plt.axis('off')
INSTITUT FÜR
45 Plotting with pyplot NEUROINFORMATIK
DONE!