Commit 8aaaa572 authored by christian.foerster's avatar christian.foerster
Browse files

still work in progress but getting there

parent cbef1e07
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %line_magic"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %%cell_magic (must be in the first line of the cell)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# list the help to magix commands\n",
"%magic"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# list content of current folder\n",
"%ls"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%lsmagic"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%file test_simple.py\n",
"# writing content of cell to file\n",
"\n",
"def test_one():\n",
" assert 1 + 1 == 2\n",
" \n",
"def test_two():\n",
" \"\"\" this test will fail !\"\"\"\n",
" a = [1, 2, 3]\n",
" b = [3, 4]\n",
" assert a + b == [1, 2, 3, 4]\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# get current working directory\n",
"%pwd\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# return the history of a notebook\n",
"%hist"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# pip install in notebook\n",
"%pip install numpy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# run a python script from inside a notebook\n",
"%run test_simple.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# run shell commands and capture output!\n",
"%sx "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# time your code\n",
"%time some_list=[a+b**2 for a,b in enumerate(range(1000000))]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# time your code - more suffisticated\n",
"%timeit some_list=[a+b**2 for a,b in enumerate(range(1000000))]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a=\"22\"\n",
"b=33\n",
"\n",
"# list varaibles of certain type quickly\n",
"%who function str"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"There's a lot more o discover... convert cells to html, run javascript in cells, render as svg, ..."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:code id: tags:
``` python
# %line_magic
```
%% Cell type:code id: tags:
``` python
# %%cell_magic (must be in the first line of the cell)
```
%% Cell type:code id: tags:
``` python
# list the help to magix commands
%magic
```
%% Cell type:code id: tags:
``` python
# list content of current folder
%ls
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
%lsmagic
```
%% Cell type:code id: tags:
``` python
%%file test_simple.py
# writing content of cell to file
def test_one():
assert 1 + 1 == 2
def test_two():
""" this test will fail !"""
a = [1, 2, 3]
b = [3, 4]
assert a + b == [1, 2, 3, 4]
```
%% Cell type:code id: tags:
``` python
# get current working directory
%pwd
```
%% Cell type:code id: tags:
``` python
# return the history of a notebook
%hist
```
%% Cell type:code id: tags:
``` python
# pip install in notebook
%pip install numpy
```
%% Cell type:code id: tags:
``` python
# run a python script from inside a notebook
%run test_simple.py
```
%% Cell type:code id: tags:
``` python
# run shell commands and capture output!
%sx
```
%% Cell type:code id: tags:
``` python
# time your code
%time some_list=[a+b**2 for a,b in enumerate(range(1000000))]
```
%% Cell type:code id: tags:
``` python
# time your code - more suffisticated
%timeit some_list=[a+b**2 for a,b in enumerate(range(1000000))]
```
%% Cell type:code id: tags:
``` python
a="22"
b=33
# list varaibles of certain type quickly
%who function str
```
%% Cell type:markdown id: tags:
There's a lot more o discover... convert cells to html, run javascript in cells, render as svg, ...
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Numpy\n",
"\n",
"For all of you that love matlab numpy will be very familiar. (https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html)\n",
"\n",
"Why use numpy? Because it's very fast! (https://stackoverflow.com/questions/7596612/benchmarking-python-vs-c-using-blas-and-numpy)\n",
"\n",
"**A more comprehensive tutorial:** (https://docs.scipy.org/doc/numpy/user/quickstart.html)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Arrays Examples"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([3,4,5,6])\n",
"rng = np.arange(100)\n",
"ones = np.ones(1000000)\n",
"zeros = np.zeros((100,100)) # 2d array shape passed as tuple\n",
"repeat = np.repeat(2,1000)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Shapes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"repeat.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 3d array\n",
"repeat.reshape((10,10,10))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# flatten array to 1d\n",
"repeat.flatten()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Indexing and Slicing"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rng[:5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rng[79]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rng[3::5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rng_2d = rng.reshape((10,10))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# all lines, starting with column 5\n",
"rng_2d[:,4:] "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# first 5 lines, all column\n",
"rng_2d[:-5,:] "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rng_2d[3:5,6:9]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Boolean Indexing"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 2d boolean array\n",
"boolean_arr = rng_2d > 45\n",
"boolean_arr"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# losing shape of course!\n",
"rng_2d[boolean_arr]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rng_2d[rng_2d[:,0] > 40, 0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rng = np.arange(10000000)\n",
"# symbols: & and, | or, ~ not\n",
"%timeit rng[(rng<90) & (rng>67)] # \n",
"%timeit rng[np.logical_and(rng<90,rng>67)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Performance Comparison"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vector_len = 10000000"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"square = []\n",
"for i in range(vector_len):\n",
" square.append(i**2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"square_np = np.square(np.arange(vector_len))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"square_even = []\n",
"for i in range(vector_len):\n",
" i_2 = i**2\n",
" if i_2 < (vector_len / 2) ** 2:\n",
" square_even.append(i_2)\n",
" else:\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"square_even_np = np.square(np.arange(vector_len))\n",
"square_even_np = square_even_np[square_even_np < (vector_len / 2) ** 2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:
# Numpy
For all of you that love matlab numpy will be very familiar. (https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html)
Why use numpy? Because it's very fast! (https://stackoverflow.com/questions/7596612/benchmarking-python-vs-c-using-blas-and-numpy)
**A more comprehensive tutorial:** (https://docs.scipy.org/doc/numpy/user/quickstart.html)
%% Cell type:code id: tags:
``` python
import numpy as np
```
%% Cell type:markdown id: tags:
## Arrays Examples
%% Cell type:code id: tags:
``` python
arr = np.array([3,4,5,6])
rng = np.arange(100)
ones = np.ones(1000000)
zeros = np.zeros((100,100)) # 2d array shape passed as tuple
repeat = np.repeat(2,1000)
```
%% Cell type:markdown id: tags:
## Shapes
%% Cell type:code id: tags:
``` python
repeat.shape
```
%% Cell type:code id: tags:
``` python
# 3d array
repeat.reshape((10,10,10))
```
%% Cell type:code id: tags:
``` python
# flatten array to 1d
repeat.flatten()
```
%% Cell type:markdown id: tags:
## Indexing and Slicing
%% Cell type:code id: tags:
``` python
rng[:5]
```
%% Cell type:code id: tags:
``` python
rng[79]
```
%% Cell type:code id: tags:
``` python
rng[3::5]
```
%% Cell type:code id: tags:
``` python
rng_2d = rng.reshape((10,10))
```
%% Cell type:code id: tags:
``` python
# all lines, starting with column 5
rng_2d[:,4:]
```
%% Cell type:code id: tags:
``` python
# first 5 lines, all column
rng_2d[:-5,:]
```
%% Cell type:code id: tags:
``` python
rng_2d[3:5,6:9]
```
%% Cell type:markdown id: tags:
## Boolean Indexing