pr3/python/numpy/numpy.ipynb

510 lines
9.4 KiB
Plaintext
Raw Normal View History

2023-06-23 23:05:46 +02:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [4 5 6]]\n",
"[[1. 2. 3.]\n",
" [4. 5. 6.]]\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"# Array mit Ganzzahlen anlegen\n",
"a = np.array([[1,2,3], [4,5,6]])\n",
"print(a)\n",
"\n",
"# Array mit Fließkommazahlen anlegen\n",
"b = np.array([[1,2,3], [4,5,6]], float)\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0. 0. 0.]\n",
" [0. 0. 0.]]\n",
"(2, 3)\n",
"[[1 0 0]\n",
" [0 2 0]\n",
" [0 0 3]]\n"
]
}
],
"source": [
"# Matrix nur mit Nullen\n",
"\n",
"a = np.zeros((2,3))\n",
"\n",
"print(a)\n",
"print(a.shape)\n",
"\n",
"# Diagonal-Matrix\n",
"\n",
"b = np.diag([1,2,3])\n",
"\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.31437735 0.70638017 0.893382 ]\n",
" [0.88031899 0.61047408 0.93577239]]\n"
]
}
],
"source": [
"a = np.random.random((2,3))\n",
"\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0. 0.]\n",
" [0. 0.]]\n",
"[[8. 0.]\n",
" [0. 0.]]\n"
]
}
],
"source": [
"a = np.zeros((2,2))\n",
"\n",
"print(a)\n",
"\n",
"a[0][0] = 7\n",
"#oder a[0,0] = 7\n",
"\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"(2, 3)\n",
"6\n",
"int64\n",
"[[1 4]\n",
" [2 5]\n",
" [3 6]]\n"
]
}
],
"source": [
"a = np.array([[1,2,3], [4,5,6]])\n",
"\n",
"print(a.ndim)\n",
"print(a.shape)\n",
"print(a.size)\n",
"print(a.dtype)\n",
"print(a.T)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 3 4 12]\n",
"[2 2 0 1]\n",
"[ 0 3 8 15]\n"
]
}
],
"source": [
"a = np.arange(4)\n",
"\n",
"b = np.array([2,3,2,4])\n",
"\n",
"print(b * a)\n",
"print(b - a)\n",
"\n",
"c = [2,3,4,5]\n",
"\n",
"print(a * c)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "operands could not be broadcast together with shapes (3,) (4,) ",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[13], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m a \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39marange(\u001b[39m3\u001b[39m)\n\u001b[1;32m 2\u001b[0m b \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39marange(\u001b[39m4\u001b[39m)\n\u001b[0;32m----> 4\u001b[0m \u001b[39mprint\u001b[39m(a\u001b[39m+\u001b[39;49mb)\n",
"\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (3,) (4,) "
]
}
],
"source": [
"a = np.arange(3)\n",
"b = np.arange(4)\n",
"\n",
"print(a+b)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3 4 5]\n"
]
}
],
"source": [
"# Aufgabe\n",
"\n",
"x = np.array([0,1,2])\n",
"y = np.array([3])\n",
"\n",
"print(x+y)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n",
"6\n",
"[-1 2 -1]\n",
"[[1 1 1]\n",
" [2 2 2]\n",
" [3 3 3]]\n"
]
}
],
"source": [
"# Listen werden automatisch in Vektoren konvertiert\n",
"\n",
"u = [1,2,3]\n",
"v = [1,1,1]\n",
"\n",
"print(np.inner(u, v))\n",
"print(np.dot(u, v))\n",
"print(np.cross(u, v))\n",
"\n",
"print(np.outer(u,v))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[4 5 6]\n",
"[[1 2 3]\n",
" [4 5 6]]\n",
"[[3]\n",
" [6]]\n"
]
}
],
"source": [
"a = np.array([[1,2,3], [4,5,6]])\n",
"\n",
"# 2. Zeile, alle Spalten\n",
"print(a[1, :])\n",
"\n",
"# 1. und 2. Zeile, alle Spalten\n",
"print(a[0:2])\n",
"\n",
"# Alle Zeilen, Spalten 3 und 4\n",
"print(a[:, 2:4])\n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3]\n",
"[4 5 6]\n",
"[1 2 3]\n",
"[4 5 6]\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n"
]
}
],
"source": [
"# Iterieren über Matrizen\n",
"\n",
"a = np.array([[1,2,3], [4,5,6]])\n",
"\n",
"# Zeilen und Spaltenweise iterieren\n",
"for row in a:\n",
" for column in a:\n",
" print(column)\n",
"\n",
"# Über alle Elemente iterieren\n",
"for element in a.flat:\n",
" print(element)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1. 2. 3.5]\n",
" [4. 5. 6. ]\n",
" [7. 8. 9.3]]\n"
]
}
],
"source": [
"# Matrix aus Datei lesen\n",
"\n",
"m = np.loadtxt(\"matrix.txt\")\n",
"\n",
"print(m)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1. 1.]\n",
" [1. 1.]\n",
" [1. 1.]]\n",
"[[1. 1. 1.]\n",
" [1. 1. 1.]]\n"
]
}
],
"source": [
"# Matrix-Operationen\n",
"\n",
"a = np.ones((3,2))\n",
"\n",
"b = a.T # Transponierte Matrix\n",
"\n",
"print(a)\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5 7 9]\n",
"[ 6 15]\n",
"21\n",
"[[1 2 3]\n",
" [5 7 9]]\n",
"[[ 1 3 6]\n",
" [ 4 9 15]]\n"
]
}
],
"source": [
"# Matrix-Operationen\n",
"\n",
"a = np.array([[1,2,3],[4,5,6]])\n",
"\n",
"# Spalten und Zeilensumme\n",
"print(a.sum(axis=0))\n",
"print(a.sum(axis=1))\n",
"\n",
"print(a.sum())\n",
"\n",
"# Kumulative Summe\n",
"print(a.cumsum(axis=0))\n",
"print(a.cumsum(axis=1))"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"6\n",
"[1 4]\n",
"[3 6]\n"
]
}
],
"source": [
"# Matrix-Operationen\n",
"\n",
"a = np.array([[1,2,3],[4,5,6]])\n",
"\n",
"#Maximum und Minimum\n",
"print(a.min())\n",
"print(a.max())\n",
"\n",
"#Maximum und Minimum entlang einer Achse\n",
"print(a.min(axis=1))\n",
"print(a.max(axis=1))"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1. 0. 0.]\n",
" [0. 1. 0.]\n",
" [0. 0. 1.]]\n",
"14\n"
]
}
],
"source": [
"# Matrix-Operationen\n",
"\n",
"# 3x3 Identitätsmatrix\n",
"id = np.eye(3)\n",
"\n",
"print(id)\n",
"\n",
"# Spur berechnen\n",
"\n",
"a = np.array([[1,2,3],[4,5,6],[7,6,8]])\n",
"print(a.trace())"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 4]\n",
" [2 5]\n",
" [3 6]]\n",
"[[1 2 3]\n",
" [4 5 6]]\n"
]
}
],
"source": [
"# Matrix Operationen\n",
"\n",
"a = np.array([1,2,3])\n",
"b = np.array([4,5,6])\n",
"\n",
"print(np.column_stack([a,b]))\n",
"\n",
"print(np.row_stack((a,b)))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.10.9"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}