{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python gyakorlat 6" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Bevezető numpy feladatok\n", "\n", "Használj vektorizációt (pl A+A az elemenkénti összeadás) for ciklusok helyett. Minden feladatban egy függvényt kell implementálni, aminek egy `numpy` tömb a bemenete.\n", "\n", "## 1.1 Valósítsd meg a standardizálást 2D tömbökre.\n", "\n", "\\begin{equation*}\n", "X_{std} = \\frac{X - \\mu}{\\sigma},\n", "\\end{equation*}\n", "\n", "Ahol $\\mu$ az értékek átlaga (mean) és $\\sigma$ a szórása (standard deviation)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.2 Normalizálj egy 2D tömböt.\n", "\n", "\\begin{equation*}\n", "X_{norm} = \\frac{X - X_{min}}{X_{max} - X_{min}}\n", "\\end{equation*}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.3 Készítsd el a softmax függvényt.\n", "$$\n", "x_i \\mapsto \\frac{\\exp(x_i)}{\\sum_{j=1}^n \\exp(x_j)}\n", "$$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Vektorizáció\n", "\n", "Írd át az alábbi példákat vektorizált (azaz listák és for ciklusok nélküli) formába .\n", "\n", "## 2.1 Soronkénit euklideszi norma\n", "\n", "Írj egy függvényt, ami kiszámolja egy 2D tömb minden sorának az euklidészi normáját. Használj `numpy` műveleteket és vektorizációt, kerüld el a `for` ciklusokat. Alább a _rossz_ megoldás." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def rowwise_norm(X):\n", " def my_dot(x, y):\n", " result = 0.0\n", " for i in range(len(x)):\n", " result += x[i] * y[i]\n", " return result\n", " return np.array([np.sqrt(my_dot(x, x)) for x in X])\n", "\n", "X = np.arange(5)[:, None]*np.ones((5, 3));\n", "print(X)\n", "print(rowwise_norm(X))\n", "print(rowwise_norm([[1], [-1], [1], [-1]]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.2 Sakktábla\n", "\n", "Írj egy függvényt, ami legyárt egy $n\\times n$-es, $\\pm1$-eket sakktáblaszerűen tartalmazó tömböt $\\left(M_{i,j} = (-1)^{i+j}\\right)$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def chessboard(n):\n", "\n", "chessboard(5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3. Broadcast kvíz\n", "\n", "Működnek az alábbi műveletek és ha igen, mi az eredmény alakja (shape)?\n", "* Próbáld meg kitalálni a választ a cella futtatása nélkül." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.ones(3) + np.ones((3,3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.ones(3) + np.ones((4, 3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.ones(3) + np.ones((3, 4))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.ones(3)[:, None] + np.ones((3, 4))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.ones((1, 2, 3)) + np.ones((1, 3))[:, None, :]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.ones((1, 2, 3)) + np.ones((1, 3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4. Numpy haladó feladatok\n", "\n", "## 4.1 Blokk-mátrix\n", "\n", "Írj függvényt __`blockmatrix`__ néven, ami a következő négyzetes mátrixot generálja:\n", "\n", "$$\n", "\\left(\\begin{array}{ccc|ccc}\n", " 1 & & 0& 0 & \\cdots & 0 \\\\\n", " & \\ddots & & \\vdots & & \\vdots \\\\\n", " 0& & 1 & 0 & \\cdots & 0 \\\\\\hline\n", " 0 & \\cdots & 0 & 1 & \\cdots & 1 \\\\\n", " \\vdots & & \\vdots & \\vdots & & \\vdots \\\\\n", " 0 & \\cdots & 0 & 1 & \\cdots & 1\n", "\\end{array}\\right)\n", "$$\n", "\n", "A bal felső blokk egy egységmátrix, a jobb alsó blokk egy négyzetes csupa $1$ mátrix, a maradék két blokk csupa $0$ mátrixok. A függvény két természetes számot várjon bemenetnek, az első a jobb felső egységmátrix a második a bal alsó $1$ mátrix mérete. \n", "A függvényben a numpy csomag `ones`, `zeros`, `eye` generátorait és konkatenációt használhatsz." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4.2 Blokk-mátrix tetszőleges négyzetes mátrixokból\n", "\n", "Írj függvényt ami tetszőleges számú négyzetes mátrixot vár bemenetként és a kimenete egy blokkmátrix, ami a bemenetként kapott mátrixokat tartalmazza a főátlóban, minden más eleme 0." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4.3 Numerikus derivált\n", "\n", "A következő __`derivate`__ függvény egy $\\mathbb{R}\\mapsto\\mathbb{R}$ függvény deriváltját numerikusan közelíti a véges differenciák módszerével. Implementáld a függvényt `numpy` utasításokkal `for` ciklus helyett!\n", "\n", "A bemenet egy 1D `f np.array`a függvényértékekkel és egy opcionális 1D `x np.array` az x értékekkel. Ha `x` nincs megadva, akkor alapértelmezetten `x = [0, 1, ..., n]`. \n", "\n", "A kimenet a bement hosszánál egyel rövidebb 1D `f np.array`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def derivate(f, x=None):\n", " if x is None:\n", " x = np.arange(len(f))\n", " return np.array([(f[i+1] - f[i]) / (x[i+1] - x[i]) for i in range(len(x) - 1)])\n", "\n", "derivate(np.arange(10)**2)" ] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }