Newer
Older
"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
}