Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Einführung in Python und NumPy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variablen und Datentypen in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Primitive Datentypen"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 Hello World 3.14 True (4+3j)\n",
"<class 'int'> <class 'str'> <class 'float'> <class 'bool'> <class 'complex'>\n"
]
}
],
"source": [
"a = 5\n",
"b = \"Hello World\"\n",
"c = 3.14\n",
"d = True\n",
"e = 4 + 3j\n",
"print(a, b, c, d, e)\n",
"print(type(a), type(b), type(c), type(d), type(e))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variablen überschreiben"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"50\n",
"Hello World\n"
]
}
],
"source": [
"var = 50\n",
"print(var)\n",
"var = \"Hello World\" # Neuer Datentyp\n",
"print(var)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mehrere Variablen initialisieren"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 6 7\n",
"ha ha ha\n"
]
}
],
"source": [
"x, y, z = 5, 6, 7\n",
"i = j = k = \"ha\"\n",
"print(x, y, z)\n",
"print(i, j, k)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variablen tauschen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"a, b = 1, 2\n",
"a, b = b, a\n",
"print(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Operatoren"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n",
"-2\n",
"8\n",
"0.5\n",
"0\n",
"2\n",
"16\n",
"2.0\n"
]
}
],
"source": [
"a, b = 2, 4\n",
"print(a + b)\n",
"print(a - b)\n",
"print(a * b)\n",
"print(a / b)\n",
"print(a // b)\n",
"print(a % b)\n",
"print(a**b)\n",
"print(b**(1/a))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variablen mit Operatoren neu zuweisen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12 35 16807\n"
]
}
],
"source": [
"i = j = k = 7\n",
"i += 5\n",
"j *= 5\n",
"k **= 5\n",
"print(i, j, k)\n",
"# Kein i++"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Boolesche Operatoren"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"False\n",
"True\n",
"True\n",
"True\n",
"False\n",
"False\n"
]
}
],
"source": [
"print(1 + 1 == 2)\n",
"print(1 + 2 != 2)\n",
"print(1 + 2 > 3)\n",
"print(1 + 2 <= 3)\n",
"print(1 + 2 is 3)\n",
"print(True or False) # ||\n",
"print(True and False) # &&\n",
"print(not True) # !"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Listen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"python numpy scipy 10 True\n",
"True 10 scipy numpy python\n",
"5\n"
]
}
],
"source": [
"l = [\"python\", \"numpy\", \"scipy\", 10, True]\n",
"print(l[0], l[1], l[2], l[3], l[4])\n",
"print(l[-1], l[-2], l[-3], l[-4], l[-5])\n",
"print(len(l))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Listen bearbeiten"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['python', 'numpy', 'scipy', 10, True, False]\n",
"['python', 'numpy', 'scipy', 'matplotlib', True, False]\n"
]
}
],
"source": [
"l.append(False)\n",
"print(l)\n",
"l[3] = \"matplotlib\"\n",
"print(l)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Operatoren auf Listen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 8]\n",
"[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]\n",
"True\n",
"False\n"
]
}
],
"source": [
"l1, l2 = [1,2,3,4], [5,6,7,8]\n",
"l = l1 + l2\n",
"print(l)\n",
"print(3 * l1)\n",
"print(5 in l2)\n",
"print(5 in l1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mehrdimensionale Listen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
"1 4 6\n",
"[1, 2, 3] [4, 5, 6] [7, 8, 9]\n"
]
}
],
"source": [
"A = [[1,2,3], [4,5,6], [7,8,9]]\n",
"print(A[0][0], A[1][0], A[1][2])\n",
"print(A[0], A[1], A[2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### List Slices\n",
"Notation: liste[start: stop: step]"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 8]\n",
"[3, 4, 5]\n",
"[3, 4, 5, 6, 7, 8]\n",
"[1, 2, 3, 4, 5]\n",
"[1, 2, 3, 4, 5, 6, 7, 8]\n",
"[3, 5]\n",
"[6, 5, 4]\n",
"[8, 7, 6, 5, 4, 3, 2, 1]\n"
]
}
],
"print(l[2:5]) # der Index von stop ist nicht mit dabei\n",
"print(l[2:]) # stop weglassen => bis zum Ende\n",
"print(l[:5]) # start weglassen => vom Anfang an\n",
"print(l[2:5:2]) # Das erste Element (start) ist immer mit dabei\n",
"print(l[5:2:-1])\n",
"print(l[::-1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tupel"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Audi 250 4.7 True\n",
"True 4.7 250 Audi\n",
"4\n",
"True\n"
]
}
],
"source": [
"t = (\"Audi\", 250, 4.7, True)\n",
"print(t[0], t[1], t[2], t[3])\n",
"print(t[-1], t[-2], t[-3], t[-4])\n",
"print(len(t))\n",
"print(\"Audi\" in t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tupel können nicht verändert werden, nur zusammengefügt!"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('Audi', 250, 4.7, True, 'white')\n",
"4.7 white\n"
]
}
],
"t += (\"white\",) # es wird ein neues Tupel erstellt\n",
"print(t)\n",
"a, b, c, d, e = t\n",
"print(c, e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dictionaries"
]
},
{
"cell_type": "code",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Audi\n",
"dict_keys(['brand', 'ps', 'acc', 'color'])\n",
"dict_values(['Audi', 250, 4.7, 'white'])\n",
"dict_items([('brand', 'Audi'), ('ps', 250), ('acc', 4.7), ('color', 'white')])\n"
]
}
],
"source": [
"c = {\n",
" \"brand\": \"Audi\",\n",
" \"ps\": 250,\n",
" \"acc\": 4.7\n",
"}\n",
"\n",
"c[\"color\"] = \"white\"\n",
"\n",
"print(c[\"brand\"])\n",
"print(c.keys())\n",
"print(c.values())\n",
"print(c.items()) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Funktionen und Verzweigungen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello world\n"
]
}
],
" print(\"Hello world\")\n",
"\n",
"greet()"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"16\n"
]
}
],
"def square(x: int) -> int:\n",
" return x**2\n",
"\n",
"print(square(4))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Optionale Parameter und Parameter mit Namen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"100\n",
"25\n"
]
}
],
"source": [
"def pow(x, y = 0):\n",
" return x**y\n",
"\n",
"print(pow(10))\n",
"print(pow(10, 2))\n",
"print(pow(y=2, x=5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mehrere Rückgabewerte (Tupel)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9 16\n",
"(9, 16)\n"
]
}
],
"source": [
"def f(x, y):\n",
" return x**2, y**2\n",
"\n",
"a, b = f(3, 4)\n",
"print(a, b)\n",
"ab = f(3, 4)\n",
"print(ab)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bedingte Anweisungen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 -1 0\n"
]
}
],
"source": [
"def sign(x):\n",
" if x < 0:\n",
" return -1\n",
" elif x > 0:\n",
" return 1\n",
" else:\n",
" return 0\n",
" \n",
"print(sign(17), sign(-3), sign(0))"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True False\n"
]
}
],
"source": [
"def positive(x):\n",
" return True if x > 0 else False\n",
"\n",
"print(positive(14), positive(0))\n",
"\n",
"# C/Java return x > 0 ? True : False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Schleifen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"[1, 2, 3, 4, 5]\n",
"[1, 3, 5, 7, 9]\n"
]
}
],
"for i in range(5): # Python For-Schleifen iterieren immer über ein iterable z.B. Liste\n",
" print(i)\n",
" \n",
"print(list(range(1, 6)))\n",
"print(list(range(1, 10, 2)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Funktionsweise: range(start, stop, step)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### List Comprehensions"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 4, 9, 16]\n",
"[0, 4, 16, 36, 64]\n"
]
}
],
"source": [
"a = [i**2 for i in range(5)]\n",
"print(a)\n",
"\n",
"b = [i**2 for i in range(10) if i % 2 == 0]\n",
"print(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Über Datenstrukturen iterieren"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5, 6, 7, 8]\n"
]
}
],
"source": [
"print(l2)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n",
"6\n",
"7\n",
"8\n"
]
}
],
"source": [
"for el in l2:\n",
" print(el)"
]
},
{
"cell_type": "code",
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 5\n",
"1 6\n",
"2 7\n",
"3 8\n"
]
}
],
"source": [
"for i in range(len(l2)):\n",
" print(i, l2[i])"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 5\n",
"1 6\n",
"2 7\n",
"3 8\n"
]
}
],
"source": [
"for i, el in enumerate(l2):\n",
" print(i, el)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'brand': 'Audi', 'ps': 250, 'acc': 4.7, 'color': 'white'}\n"
]
}
],
"source": [
"print(c)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"brand Audi\n",
"ps 250\n",
"acc 4.7\n",
"color white\n"
]
}
],
"source": [
"for key, val in c.items():\n",
" print(key, val)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Objektorientierte Programmierung"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"class Person:\n",
" def __init__(self, name):\n",
" self.name = name\n",
" \n",
" def greet(self):\n",
" print(\"Hallo, ich bin {}\".format(self.name))\n",
" \n",
" def __repr__(self):\n",
" return \"Person: {}\".format(self.name)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hallo, ich bin Anna\n",
"Person: Anna\n"
]
}
],
"source": [
"p = Person(\"Anna\")\n",
"p.greet()\n",
"print(p)"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"class Student(Person):\n",
" def __init__(self, name, age, height):\n",
" Person.__init__(self, name)\n",
" self.__age = age # private\n",
" self._height = height # protected\n",
" \n",
" def __repr__(self):\n",
" return \"Person[name={},age={},height={}]\"\\\n",
" .format(self.name, self.__age, self._height)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hallo, ich bin Anna\n",
"Person[name=Anna,age=18,height=175]\n"
]
}
],
"source": [
"s = Student(\"Anna\", 18, 175)\n",
"s.greet()\n",
"print(s)\n",
"# print(s.__age)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Funktionale Programmierung"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"81\n",
"16\n"
]
}
],
"source": [
"g = lambda x: x**4\n",
"h = lambda x, y: x**(2*y)\n",
"print(g(3))\n",
"print(h(2,2))"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 8]\n"
]
}
],
"source": [
"print(l)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<map object at 0x0000029981C05508>\n",
"[1, 4, 9, 16, 25, 36, 49, 64]\n"
]
}
],
"source": [
"lsq = map(lambda x: x**2, l)\n",
"print(lsq)\n",
"print(list(lsq))"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 3, 5, 7]\n"
]
}
],
"source": [
"print(list(filter(lambda x: x % 2 == 1, l)))"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"36\n"
]
}
],
"source": [
"from functools import reduce\n",
"print(reduce(lambda a, b: a + b, l))"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n"
]
}
],
"source": [
"print(reduce(lambda a, b: a if a > b else b, l))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## == vs. is"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n"
]
}
],
"source": [
"print(5 == 5)\n",
"print(5 is 5)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n",
"True\n"
]
}
],
"print([1,2,3] == [1,2,3])\n",
"print([1,2,3] is [1,2,3])\n",
"print(a is b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## NumPy Grundlagen"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### NumPy Arrays"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4 5 6]\n",
"1\n",
"6\n",
"[2 3 4]\n",
"[1 2]\n",
"[6 5 4 3 2 1]\n",
"[2 4 6]\n",
"[1 3 4 5]\n",
"[1 3 6]\n"
]
}
],
"source": [
"a = np.array([1, 2, 3, 4, 5, 6])\n",
"print(a)\n",
"print(a[0])\n",
"print(a[-1])\n",
"print(a[1:4])\n",
"print(a[:2])\n",
"print(a[::-1])\n",
"print(a[1::2])\n",
"print(a[[0, 2, 3, 4]]) # Liste von Indizes\n",
"print(a[[True, False, True, False, False, True]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Operatoren auf NumPy Arrays"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3 4 5 6 7 8]\n",
"[ 3 6 9 12 15 18]\n",
"[ 1 4 9 16 25 36]\n",
"[ 2 4 8 16 32 64]\n"
]
}
],
"print(a + 2)\n",
"print(3 * a)\n",
"print(a**2)\n",
"print(2**a)"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"b = np.array([6, 5, 4, 3, 2, 1]) # b = a[::-1]"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[7 7 7 7 7 7]\n",
"[ 6 10 12 12 10 6]\n",
"[ 1 32 81 64 25 6]\n"
]
}
],
"source": [
"print(a + b)\n",
"print(a * b)\n",
"print(a**b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Zweidimensionale Arrays (Matrizen)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]\n",
"1 6\n"
]
}
],
"source": [
"A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
"print(A)\n",
"print(A[0,0], A[1, 2])"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3]\n",
"[1 4 7]\n"
]
}
],
"source": [
"print(A[0])\n",
"print(A[:, 0])"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2]\n",
" [4 5]]\n"
]
}
],
"source": [
"print(A[:2, :2])"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2 1]\n",
" [5 4]]\n"
]
}
],
"source": [
"print(A[:2, 1::-1])"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[4 5]\n",
" [1 2]]\n"
]
}
],
"source": [
"print(A[1::-1, :2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Werte überschreiben"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]\n"
]
}
],
"source": [
"B = A.copy()\n",
"print(B)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[12 12 12]\n",
" [ 4 5 6]\n",
" [ 7 8 9]]\n"
]
}
],
"source": [
"B[0] = 12\n",
"print(B)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[12 -5 12]\n",
" [ 4 -5 6]\n",
" [ 7 -5 9]]\n"
]
}
],
"source": [
"B[:, 1] = -5\n",
"print(B)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[12 -5 12]\n",
" [ 4 -5 6]\n",
" [ 5 6 7]]\n"
]
}
],
"source": [
"B[2] = [5, 6, 7]\n",
"print(B)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[12 12 12]\n",
" [ 4 -5 6]\n",
" [ 5 12 7]]\n"
]
}
],
"source": [
"B[:, 1] = B[0]\n",
"print(B)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dimensionen hinzufügen"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]]\n"
]
}
],
"source": [
"print(A[None])"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[1 2 3]]\n",
"\n",
" [[4 5 6]]\n",
"\n",
" [[7 8 9]]]\n"
]
}
],
"source": [
"print(A[:, None])"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[1]\n",
" [2]\n",
" [3]]\n",
"\n",
" [[4]\n",
" [5]\n",
" [6]]\n",
"\n",
" [[7]\n",
" [8]\n",
" [9]]]\n"
]
}
],
"source": [
"print(A[:, :, None])"
]
},
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[ 0 -1]\n",
" [-1 -1]\n",
" [-2 -4]]\n",
"\n",
" [[-1 0]\n",
" [-2 0]\n",
" [-3 -3]]]\n",
"[[[ 0 -1]\n",
" [-1 0]]\n",
"\n",
" [[-1 -1]\n",
" [-2 0]]\n",
"\n",
" [[-2 -4]\n",
" [-3 -3]]]\n"
]
}
],
"source": [
"X = np.array([[1,0],[0,1]])\n",
"Y = np.array([[1,1],[2,1],[3,4]])\n",
"print(X[:, None] - Y[None, :])\n",
"print(X[None, :] - Y[:, None])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Boolesche Funktionen auf NumPy Arrays"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"b = np.array([1, 0, 1])"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ True False True]\n"
]
}
],
"source": [
"print(b == 1)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[False True False]\n"
]
}
],
"source": [
"print(b < 1)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [7 8 9]]\n"
]
}
],
"source": [
"print(A[b == 1])"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [7 8 9]]\n"
]
}
],
"source": [
"print(A[[True, False, True]])"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[False False False]\n",
" [False False True]\n",
" [ True True True]]\n"
]
}
],
"source": [
"print(A > 5)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[6 7 8 9]\n"
]
}
],
"source": [
"print(A[A > 5])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Nützliche NumPy Funktionen"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"X = np.ndarray((4,4))\n",
"A = np.zeros((3, 4))\n",
"B = np.ones((3,4))\n",
"c = np.arange(0, 1, .1)\n",
"d = np.linspace(0, 1, 10)\n",
"I = np.identity(4)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[4.67296746e-307 1.69121096e-306 8.01092457e-307 1.24610994e-306]\n",
" [1.42418987e-306 1.37961641e-306 1.60220528e-306 1.24611266e-306]\n",
" [9.34598925e-307 1.24612081e-306 1.11260755e-306 1.60220393e-306]\n",
" [1.51320640e-306 9.34609790e-307 1.24610723e-306 1.24610723e-306]]\n"
]
}
],
"source": [
"print(X) # zufällige Werte"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0. 0. 0. 0.]\n",
" [0. 0. 0. 0.]\n",
" [0. 0. 0. 0.]]\n"
]
}
],
"source": [
"print(A)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1. 1. 1. 1.]\n",
" [1. 1. 1. 1.]\n",
" [1. 1. 1. 1.]]\n"
]
}
],
"source": [
"print(B)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]\n"
]
}
],
"source": [
"print(c)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556\n",
" 0.66666667 0.77777778 0.88888889 1. ]\n"
]
}
],
"source": [
"print(d)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1. 0. 0. 0.]\n",
" [0. 1. 0. 0.]\n",
" [0. 0. 1. 0.]\n",
" [0. 0. 0. 1.]]\n"
]
}
],
"source": [
"print(I)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1. 2. 3. 4.]\n",
" [ 5. 6. 7. 8.]\n",
" [ 9. 10. 11. 12.]\n",
" [13. 14. 15. 16.]]\n"
]
}
],
"source": [
"M = np.linspace(1, 16, 16).reshape((4,4))\n",
"print(M)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1. 5. 9. 13.]\n",
" [ 2. 6. 10. 14.]\n",
" [ 3. 7. 11. 15.]\n",
" [ 4. 8. 12. 16.]]\n"
]
}
],
"source": [
"print(M.T)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"136.0 8.5 20922789888000.0\n"
]
}
],
"source": [
"print(M.sum(), M.mean(), M.prod())"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[28. 32. 36. 40.] [ 7. 8. 9. 10.] [ 585. 1680. 3465. 6144.]\n"
]
}
],
"source": [
"print(M.sum(0), M.mean(0), M.prod(0))"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10. 26. 42. 58.] [ 2.5 6.5 10.5 14.5] [2.400e+01 1.680e+03 1.188e+04 4.368e+04]\n"
]
}
],
"source": [
"print(M.sum(1), M.mean(1), M.prod(1))"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1. 2. 3. 4.]\n"
]
}
],
"source": [
"x = np.linspace(1,4, 4)\n",
"print(x)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 30. 70. 110. 150.]\n"
]
}
],
"source": [
"print(M.dot(x))"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 30. 70. 110. 150.]\n"
]
}
],
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
"source": [
"print(M @ x)"
]
},
{
"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",
}
},
"nbformat": 4,
"nbformat_minor": 4
}