1758 lines
35 KiB
Plaintext
1758 lines
35 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## [NumPy](http://www.numpy.org/)\n",
|
||
"\n",
|
||
"A NumPy egy alacsony szintű matematikai csomag, numerikus számításokhoz.\n",
|
||
"\n",
|
||
"- Alapvető adatszerkezete az [n dimenziós tömb](https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html).\n",
|
||
"- C nyelven íródott. A szokásos tömbműveletek hatékonyan vannak benne megvalósítva.\n",
|
||
"- Többek között tartalmaz lineáris algebrai és véletlenszám generáló almodult.\n",
|
||
"- Számos magasabb szintű csomag (pl. scipy, matplotlib, pandas, scikit-learn) épül rá.\n",
|
||
"\n",
|
||
"A NumPy külső csomag, a telepítésére többféle lehetőség van, például:\n",
|
||
"- `pip install numpy --user`\n",
|
||
"- `sudo apt-get install python3-numpy`\n",
|
||
"- `conda install numpy`"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# A NumPy modul importálása np néven.\n",
|
||
"import numpy as np"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"'1.24.3'"
|
||
]
|
||
},
|
||
"execution_count": 2,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Verzió lekérdezése.\n",
|
||
"np.__version__"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"#### Tömbök létrehozása"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 1 dimenziós, egész számokból álló tömb létrehozása.\n",
|
||
"a = np.array([2, 3, 4])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([2, 3, 4])"
|
||
]
|
||
},
|
||
"execution_count": 4,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"a"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"numpy.ndarray"
|
||
]
|
||
},
|
||
"execution_count": 5,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# A tömb objektum típusa.\n",
|
||
"type(a)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"1"
|
||
]
|
||
},
|
||
"execution_count": 6,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Hány dimenziós a tömb?\n",
|
||
"a.ndim"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"(3,)"
|
||
]
|
||
},
|
||
"execution_count": 7,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# A dimenziók mérete.\n",
|
||
"a.shape"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 8,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"dtype('int32')"
|
||
]
|
||
},
|
||
"execution_count": 8,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Az elemek típusának lekérdezése.\n",
|
||
"# A NumPy tömbök homogének (kivéve az objektumtömböt).\n",
|
||
"a.dtype"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 2 dimenziós, lebegőpontos tömb létrehozása.\n",
|
||
"b = np.array([[2.0, 3.0, 4.0], [5.5, 6.6, 7.7]])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 10,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[2. , 3. , 4. ],\n",
|
||
" [5.5, 6.6, 7.7]])"
|
||
]
|
||
},
|
||
"execution_count": 10,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 11,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"2\n",
|
||
"(2, 3)\n",
|
||
"float64\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# Dimenziók száma, mérete, az elemek típusa.\n",
|
||
"print(b.ndim)\n",
|
||
"print(b.shape)\n",
|
||
"print(b.dtype)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 12,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([3, 4, 5, 6], dtype=uint8)"
|
||
]
|
||
},
|
||
"execution_count": 12,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Az elemek adattípusának beállítása, 1. példa.\n",
|
||
"np.array([3, 4, 5, 6], dtype='uint8')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 13,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([3.3, 4.4], dtype=float32)"
|
||
]
|
||
},
|
||
"execution_count": 13,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Az elemek adattípusának beállítása, 2. példa.\n",
|
||
"np.array([3.3, 4.4], dtype='float32')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 14,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[0., 1., 1., 0., 1., 0., 1., 1., 0., 1.],\n",
|
||
" [0., 0., 1., 0., 1., 1., 0., 1., 0., 1.],\n",
|
||
" [0., 0., 1., 0., 0., 0., 1., 1., 0., 0.],\n",
|
||
" [0., 1., 0., 0., 1., 0., 1., 1., 0., 0.],\n",
|
||
" [1., 0., 1., 1., 0., 0., 1., 0., 1., 1.],\n",
|
||
" [1., 0., 1., 0., 0., 1., 1., 0., 1., 0.],\n",
|
||
" [1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],\n",
|
||
" [0., 0., 0., 0., 0., 1., 0., 1., 0., 1.],\n",
|
||
" [1., 1., 0., 1., 0., 1., 1., 1., 0., 0.],\n",
|
||
" [1., 0., 1., 0., 1., 0., 0., 1., 0., 1.]])"
|
||
]
|
||
},
|
||
"execution_count": 14,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Tömb betöltése szövegfájlból.\n",
|
||
"np.genfromtxt('matrix.txt')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 15,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
|
||
]
|
||
},
|
||
"execution_count": 15,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Nullákból álló tömb létrehozása, 1. példa.\n",
|
||
"np.zeros(10)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 16,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[0, 0],\n",
|
||
" [0, 0],\n",
|
||
" [0, 0]], dtype=int16)"
|
||
]
|
||
},
|
||
"execution_count": 16,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Nullákból álló tömb létrehozása, 2. példa.\n",
|
||
"np.zeros((3, 2), dtype='int16')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 17,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([1, 1, 1, 1, 1, 1, 1], dtype=uint32)"
|
||
]
|
||
},
|
||
"execution_count": 17,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Egyesekből álló tömb létrehozása, 1. példa.\n",
|
||
"np.ones(7, dtype='uint32')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 18,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[[1., 1.],\n",
|
||
" [1., 1.]],\n",
|
||
"\n",
|
||
" [[1., 1.],\n",
|
||
" [1., 1.]]])"
|
||
]
|
||
},
|
||
"execution_count": 18,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Egyesekből álló tömb létrehozása, 2. példa.\n",
|
||
"np.ones((2, 2, 2))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 19,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[1., 0., 0., 0., 0.],\n",
|
||
" [0., 1., 0., 0., 0.],\n",
|
||
" [0., 0., 1., 0., 0.],\n",
|
||
" [0., 0., 0., 1., 0.],\n",
|
||
" [0., 0., 0., 0., 1.]])"
|
||
]
|
||
},
|
||
"execution_count": 19,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Egységmátrix létrehozása.\n",
|
||
"np.eye(5)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 20,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([0. , 0.4, 0.8, 1.2, 1.6])"
|
||
]
|
||
},
|
||
"execution_count": 20,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Értéktartomány létrehozása a lépésköz megadásával.\n",
|
||
"np.arange(0, 2, 0.4)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 21,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([-1. , -0.5, 0. , 0.5, 1. , 1.5, 2. ])"
|
||
]
|
||
},
|
||
"execution_count": 21,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Értéktartomány létrehozása az elemszám megadásával.\n",
|
||
"np.linspace(-1, 2, 7)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 22,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([2, 3, 4, 5, 6])"
|
||
]
|
||
},
|
||
"execution_count": 22,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Vektorok összefűzése.\n",
|
||
"a = np.array([2, 3])\n",
|
||
"b = np.array([4, 5, 6])\n",
|
||
"np.concatenate([a, b])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 23,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[2, 3, 4, 2, 3, 4],\n",
|
||
" [5, 6, 7, 5, 6, 7]])"
|
||
]
|
||
},
|
||
"execution_count": 23,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Mátrixok összefűzése vízszintesen.\n",
|
||
"a = np.array([[2, 3, 4],\n",
|
||
" [5, 6, 7]])\n",
|
||
"np.hstack([a, a])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 24,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[2, 3, 4],\n",
|
||
" [5, 6, 7],\n",
|
||
" [2, 3, 4],\n",
|
||
" [5, 6, 7],\n",
|
||
" [2, 3, 4],\n",
|
||
" [5, 6, 7]])"
|
||
]
|
||
},
|
||
"execution_count": 24,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Mátrixok összefűzése függőlegesen.\n",
|
||
"np.vstack([a, a, a])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"#### Elemek és résztömbök"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 25,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# Hozzunk létre egy példamátrixot!\n",
|
||
"a = np.array([[3, 4, 5],\n",
|
||
" [6, 7, 8]])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 26,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"4"
|
||
]
|
||
},
|
||
"execution_count": 26,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Elem kiválasztása (az indexelés 0-tól indul).\n",
|
||
"a[0, 1] # 0. sor, 1. oszlop"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 27,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"4"
|
||
]
|
||
},
|
||
"execution_count": 27,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"a[(0, 1)]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 28,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"4"
|
||
]
|
||
},
|
||
"execution_count": 28,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# ...a háttérben ez történik:\n",
|
||
"np.ndarray.__getitem__(a, (0, 1))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 29,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([6, 7, 8])"
|
||
]
|
||
},
|
||
"execution_count": 29,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Teljes sor kiválasztása.\n",
|
||
"a[1, :]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 30,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([6, 7, 8])"
|
||
]
|
||
},
|
||
"execution_count": 30,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Így is lehetne.\n",
|
||
"a[1]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 31,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"1"
|
||
]
|
||
},
|
||
"execution_count": 31,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# A kiválasztott sor egy 1 dimenziós tömb.\n",
|
||
"a[1].ndim"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 32,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([5, 8])"
|
||
]
|
||
},
|
||
"execution_count": 32,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Oszlop kiválasztása.\n",
|
||
"a[:, 2]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 33,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[3, 4],\n",
|
||
" [6, 7]])"
|
||
]
|
||
},
|
||
"execution_count": 33,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Résztömb kiválasztása.\n",
|
||
"a[:, :-1]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 34,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[4, 3, 4],\n",
|
||
" [7, 6, 7]])"
|
||
]
|
||
},
|
||
"execution_count": 34,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Adott indexű oszlopok kiválasztása.\n",
|
||
"a[:, [1, 0, 1]]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 35,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([4, 6, 8])"
|
||
]
|
||
},
|
||
"execution_count": 35,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Elemek kiválasztása logikai feltétel alapján.\n",
|
||
"# (Az eredmény 1 dimenziós.)\n",
|
||
"a[a % 2 == 0]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 36,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[100, 4, 5],\n",
|
||
" [ 6, 7, 8]])"
|
||
]
|
||
},
|
||
"execution_count": 36,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# A tömb elemei módosíthatók.\n",
|
||
"a[0, 0] = 100\n",
|
||
"a"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 37,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[100, 20, 5],\n",
|
||
" [ 6, 30, 8]])"
|
||
]
|
||
},
|
||
"execution_count": 37,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Oszlop módosítása.\n",
|
||
"a[:, 1] = [20, 30]\n",
|
||
"a"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"#### Tömbműveletek"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 38,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# Hozzunk létre 2 példatömböt!\n",
|
||
"a = np.array([[1, 2 ,3],\n",
|
||
" [4, 5, 6]])\n",
|
||
"b = np.array([[1, 1, 1],\n",
|
||
" [2, 2, 2]])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 39,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[2, 3, 4],\n",
|
||
" [6, 7, 8]])"
|
||
]
|
||
},
|
||
"execution_count": 39,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Elemenkénti összeadás.\n",
|
||
"a + b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 40,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[0, 1, 2],\n",
|
||
" [2, 3, 4]])"
|
||
]
|
||
},
|
||
"execution_count": 40,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Elemenkénti kivonás.\n",
|
||
"a - b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 41,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[ 1, 2, 3],\n",
|
||
" [ 8, 10, 12]])"
|
||
]
|
||
},
|
||
"execution_count": 41,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Elemenkénti szorzás.\n",
|
||
"a * b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 42,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[1. , 2. , 3. ],\n",
|
||
" [2. , 2.5, 3. ]])"
|
||
]
|
||
},
|
||
"execution_count": 42,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Elemenkénti osztás.\n",
|
||
"a / b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 43,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[1, 2, 3],\n",
|
||
" [2, 2, 3]])"
|
||
]
|
||
},
|
||
"execution_count": 43,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Elemenkénti egészosztás.\n",
|
||
"a // b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 44,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[ 1, 2, 3],\n",
|
||
" [16, 25, 36]])"
|
||
]
|
||
},
|
||
"execution_count": 44,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Elemenkénti hatványozás.\n",
|
||
"a**b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 45,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"ename": "ValueError",
|
||
"evalue": "operands could not be broadcast together with shapes (3,) (2,) ",
|
||
"output_type": "error",
|
||
"traceback": [
|
||
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
||
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
|
||
"Cell \u001b[1;32mIn[45], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# A művelet nem feltétlenül végezhető el.\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m4\u001b[39m]) \u001b[38;5;241m+\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m6\u001b[39m])\n",
|
||
"\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (3,) (2,) "
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# A művelet nem feltétlenül végezhető el.\n",
|
||
"np.array([2, 3, 4]) + np.array([5, 6])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 46,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[1, 2, 3],\n",
|
||
" [4, 5, 6]])"
|
||
]
|
||
},
|
||
"execution_count": 46,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Jelenítsük meg újra az \"a\" tömböt!\n",
|
||
"a"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 47,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[ 2.71828183, 7.3890561 , 20.08553692],\n",
|
||
" [ 54.59815003, 148.4131591 , 403.42879349]])"
|
||
]
|
||
},
|
||
"execution_count": 47,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Elemenkénti függvények (exp, log, sin, cos, ...).\n",
|
||
"np.exp(a)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 48,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[ 0.84147098, 0.90929743, 0.14112001],\n",
|
||
" [-0.7568025 , -0.95892427, -0.2794155 ]])"
|
||
]
|
||
},
|
||
"execution_count": 48,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"np.sin(a)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 39,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"1 6 21 3.5 1.707825127659933\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# Statisztikai műveletek: min, max, sum, mean (átlag), std (szórás).\n",
|
||
"print(a.min(), a.max(), a.sum(), a.mean(), a.std())"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 40,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([5, 7, 9])"
|
||
]
|
||
},
|
||
"execution_count": 40,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Oszloponkénti statisztikák.\n",
|
||
"# A 0. dimenzió azaz a sorok mentén aggregálunk, ezért ez a dimenzió fog eltűnni.\n",
|
||
"a.sum(axis=0) # oszloponkénti összeg"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 41,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([3, 6])"
|
||
]
|
||
},
|
||
"execution_count": 41,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Soronkénti statisztikák.\n",
|
||
"# A 1. dimenzió azaz az oszlopok mentén aggregálunk, ezért ez a dimenzió fog eltűnni.\n",
|
||
"a.max(axis=1) # soronkénti maximum"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 42,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[1., 2., 3.],\n",
|
||
" [4., 5., 6.]], dtype=float32)"
|
||
]
|
||
},
|
||
"execution_count": 42,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Típuskonverzió.\n",
|
||
"b = a.astype('float32')\n",
|
||
"b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 43,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"[[1 2 3]\n",
|
||
" [4 5 6]]\n",
|
||
"[[1 4]\n",
|
||
" [2 5]\n",
|
||
" [3 6]]\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# Transzponálás.\n",
|
||
"print(a)\n",
|
||
"print(a.T)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 44,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[ 1, 20, 3],\n",
|
||
" [ 4, 5, 6]])"
|
||
]
|
||
},
|
||
"execution_count": 44,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# A transzponálás nem végez másolást, csak egy új nézetet hoz létre az eredeti adattartalomra.\n",
|
||
"a.T[1, 0] = 20\n",
|
||
"a"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 45,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])"
|
||
]
|
||
},
|
||
"execution_count": 45,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Hozzunk létre egy 12 elemű példatömböt!\n",
|
||
"a = np.arange(12)\n",
|
||
"a"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 46,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[ 0, 1, 2, 3, 4, 5],\n",
|
||
" [ 6, 7, 8, 9, 10, 11]])"
|
||
]
|
||
},
|
||
"execution_count": 46,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Átalakítás 2×6-ossá.\n",
|
||
"a.reshape((2, 6))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 47,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[ 0, 1, 2, 3, 4, 5],\n",
|
||
" [ 6, 7, 8, 9, 10, 11]])"
|
||
]
|
||
},
|
||
"execution_count": 47,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Elég csak az egyik dimenzió méretét megadni, a másik helyére írhatunk (-1)-et.\n",
|
||
"a.reshape((2, -1))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 48,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[ 0, 1, 2],\n",
|
||
" [ 3, 4, 5],\n",
|
||
" [ 6, 7, 8],\n",
|
||
" [ 9, 10, 11]])"
|
||
]
|
||
},
|
||
"execution_count": 48,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Átalakítás 4×3-assá.\n",
|
||
"a.reshape((-1, 3))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 49,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"ename": "ValueError",
|
||
"evalue": "cannot reshape array of size 12 into shape (5,newaxis)",
|
||
"output_type": "error",
|
||
"traceback": [
|
||
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
||
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
|
||
"Cell \u001b[1;32mIn[49], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Ha olyan méretet adunk meg, ahol az összelemszám nem jöhet ki 12-re, akkor hibaüzenetet kapunk.\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m a\u001b[38;5;241m.\u001b[39mreshape((\u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m))\n",
|
||
"\u001b[1;31mValueError\u001b[0m: cannot reshape array of size 12 into shape (5,newaxis)"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# Ha olyan méretet adunk meg, ahol az összelemszám nem jöhet ki 12-re, akkor hibaüzenetet kapunk.\n",
|
||
"a.reshape((5, -1))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 50,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[[ 0, 1, 2],\n",
|
||
" [ 3, 4, 5]],\n",
|
||
"\n",
|
||
" [[ 6, 7, 8],\n",
|
||
" [ 9, 10, 11]]])"
|
||
]
|
||
},
|
||
"execution_count": 50,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Átalakítás 2×2×3-assá.\n",
|
||
"a.reshape((2, 2, 3))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 51,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([200, 3, 4])"
|
||
]
|
||
},
|
||
"execution_count": 51,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Az értékadás NumPy esetén sem végez másolást.\n",
|
||
"a = np.array([2, 3, 4])\n",
|
||
"b = a\n",
|
||
"b[0] = 200\n",
|
||
"a"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 52,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([2, 3, 4])"
|
||
]
|
||
},
|
||
"execution_count": 52,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Másolni a copy metódussal lehet.\n",
|
||
"a = np.array([2, 3, 4])\n",
|
||
"b = a.copy()\n",
|
||
"b[0] = 200\n",
|
||
"a"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 53,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([0, 3], dtype=int64)"
|
||
]
|
||
},
|
||
"execution_count": 53,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Keresés.\n",
|
||
"# Példa: Mely indexeknél találhatók az 5-nél kisebb elemek?\n",
|
||
"a = np.array([3, 10, 11, 4, 7, 8])\n",
|
||
"idxs = np.where(a < 5)[0]\n",
|
||
"idxs"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 54,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([3, 4])"
|
||
]
|
||
},
|
||
"execution_count": 54,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"a[idxs]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 55,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([ 3, 4, 7, 8, 10, 11])"
|
||
]
|
||
},
|
||
"execution_count": 55,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Rendezés helyben.\n",
|
||
"a = np.array([3, 10, 11, 4, 7, 8])\n",
|
||
"a.sort()\n",
|
||
"a"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 56,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"[ 3 10 11 4 7 8]\n",
|
||
"[ 3 4 7 8 10 11]\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# Rendezés új tömbbe.\n",
|
||
"a = np.array([3, 10, 11, 4, 7, 8])\n",
|
||
"b = np.sort(a)\n",
|
||
"print(a)\n",
|
||
"print(b)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"Megjegyzés:\n",
|
||
"- NumPy tömbök rendezésére ne használjuk a standard sorted függvényt!\n",
|
||
"- Ugyanígy, NumPy tömbök esetén ne használjuk a standard math modul függvényeit!"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 57,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([11, 10, 8, 7, 4, 3])"
|
||
]
|
||
},
|
||
"execution_count": 57,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Rendezés csökkenő sorrendbe.\n",
|
||
"np.sort(a)[::-1]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 58,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"[ 3 10 11 4 7 8]\n",
|
||
"[0 3 4 5 1 2]\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# A rendezett tömb elemei mely indexeknél találhatók az eredeti tömbben?\n",
|
||
"idxs = a.argsort()\n",
|
||
"print(a)\n",
|
||
"print(idxs)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 59,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([ 3, 4, 7, 8, 10, 11])"
|
||
]
|
||
},
|
||
"execution_count": 59,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# A tömb rendezése az indextömb segítségével.\n",
|
||
"a[idxs]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 60,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"2"
|
||
]
|
||
},
|
||
"execution_count": 60,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Van argmin és argmax függvény is.\n",
|
||
"a.argmax()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 61,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"24"
|
||
]
|
||
},
|
||
"execution_count": 61,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Két vektor skaláris szorzata.\n",
|
||
"a = np.array([3, 4, 5])\n",
|
||
"b = np.array([2, 2, 2])\n",
|
||
"a @ b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 62,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[ 29, 56],\n",
|
||
" [ 56, 110]])"
|
||
]
|
||
},
|
||
"execution_count": 62,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Mátrixszorzás.\n",
|
||
"A = np.array([[2, 3, 4],\n",
|
||
" [5, 6, 7]])\n",
|
||
"A @ A.T"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 63,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[29, 36, 43],\n",
|
||
" [36, 45, 54],\n",
|
||
" [43, 54, 65]])"
|
||
]
|
||
},
|
||
"execution_count": 63,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"A.T @ A"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"#### [Broadcastolás](https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)\n",
|
||
"- A broadcastolás a különböző alakú operandusok kezelésére szolgál.\n",
|
||
"- Példa: \n",
|
||
"```\n",
|
||
"A (4d tömb): 8 x 1 x 6 x 5\n",
|
||
"B (3d tömb): 7 x 1 x 5\n",
|
||
"Eredmény (4d tömb): 8 x 7 x 6 x 5\n",
|
||
"```"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 64,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([20, 30, 40])"
|
||
]
|
||
},
|
||
"execution_count": 64,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Vektor szorzása skalárral.\n",
|
||
"a = np.array([2, 3, 4])\n",
|
||
"b = 10\n",
|
||
"\n",
|
||
"# a: 3\n",
|
||
"# b: -\n",
|
||
"\n",
|
||
"a * b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 65,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"ename": "ValueError",
|
||
"evalue": "operands could not be broadcast together with shapes (2,) (3,) ",
|
||
"output_type": "error",
|
||
"traceback": [
|
||
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
||
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
|
||
"Cell \u001b[1;32mIn[65], line 8\u001b[0m\n\u001b[0;32m 3\u001b[0m b \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m6\u001b[39m])\n\u001b[0;32m 5\u001b[0m \u001b[38;5;66;03m# a: 2\u001b[39;00m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;66;03m# b: 3\u001b[39;00m\n\u001b[1;32m----> 8\u001b[0m a \u001b[38;5;241m+\u001b[39m b\n",
|
||
"\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,) (3,) "
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# Példa nem broadcastolható tömbökre.\n",
|
||
"a = np.array([2, 3])\n",
|
||
"b = np.array([4, 5, 6])\n",
|
||
"\n",
|
||
"# a: 2\n",
|
||
"# b: 3\n",
|
||
"\n",
|
||
"a + b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 66,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[ 3, 8, 15],\n",
|
||
" [ 5, 12, 21]])"
|
||
]
|
||
},
|
||
"execution_count": 66,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Mátrix szorzása vektorral.\n",
|
||
"A = np.array([[3, 4, 5],\n",
|
||
" [5, 6, 7]])\n",
|
||
"b = np.array([1, 2, 3])\n",
|
||
"\n",
|
||
"# A: 2 3\n",
|
||
"# b: - 3\n",
|
||
"\n",
|
||
"A * b # Oszloponkénti szorzás."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 67,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([[ 3, 4, 5],\n",
|
||
" [10, 12, 14]])"
|
||
]
|
||
},
|
||
"execution_count": 67,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Soronkénti szorzás.\n",
|
||
"A = np.array([[3, 4, 5],\n",
|
||
" [5, 6, 7]])\n",
|
||
"b = np.array([1, 2])\n",
|
||
"(A.T * b).T"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3 (ipykernel)",
|
||
"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.11.3"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 2
|
||
}
|