Skip to content

Instantly share code, notes, and snippets.

@spectraldani
Last active March 11, 2020 20:11
Show Gist options
  • Save spectraldani/90d7301e30ac5feab85a5c791615b4ef to your computer and use it in GitHub Desktop.
Save spectraldani/90d7301e30ac5feab85a5c791615b4ef to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "i8moYMLk3mSL"
},
"source": [
"# Introdução ao `numpy`\n",
"Escrever texto incial aqui..."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "KZTZcDGW20Y6"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt # Biblioteca para gerar gráficos"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "jbLN6SejnwoL"
},
"source": [
"Vamos criar umas matrizes e vetores para começar...\n",
"\n",
"$$\\begin{aligned}\n",
"\\boldsymbol{A} &= \\begin{bmatrix}2 & 0\\\\ 4 & 6\\\\ 8 & 2\\end{bmatrix}\\ &\n",
"\\boldsymbol{B} &= \\begin{bmatrix}1 & 3\\\\ 5 & 7\\end{bmatrix}\\\\\\\\\n",
"\\boldsymbol{v}_1 &= \\begin{bmatrix}5 & 3\\end{bmatrix}\\ &\n",
"\\boldsymbol{v}_2 &= \\begin{bmatrix}9 & 2 & 1\\end{bmatrix}\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "JDS1cgwngp-B"
},
"outputs": [],
"source": [
"A = np.array([[2,0],[4,6],[8,2]])\n",
"B = np.array([[1,3],[5,7]])\n",
"v1 = np.array([5,3])\n",
"v2 = np.array([9,2,1])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
},
"colab_type": "code",
"id": "lCM_Bmf4hzlv",
"outputId": "fb60843c-679e-44dd-d241-a76c56159772"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dimensão do A: (3, 2)\n",
"Dimensão do B: (2, 2)\n",
"Dimensão do v1: (2,)\n",
"Dimensão do v2: (2,)\n"
]
}
],
"source": [
"print('Dimensão do A: ',A.shape)\n",
"print('Dimensão do B: ',B.shape)\n",
"print('Dimensão do v1: ',v1.shape)\n",
"print('Dimensão do v2: ',v1.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\\begin{aligned}\n",
"\\boldsymbol{A} &= \\begin{bmatrix}2 & 0\\\\ 4 & 6\\\\ 8 & 2\\end{bmatrix}\\\\\n",
"\\boldsymbol{A}^{\\mathbf{T}} &= \\begin{bmatrix}2 & 4 & 8\\\\ 0 & 6 & 2\\end{bmatrix}\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2 4 8]\n",
" [0 6 2]]\n"
]
}
],
"source": [
"print(A.T)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "oe-tmI5o_kcT"
},
"source": [
"$$\\begin{aligned}\n",
"\\mathtt{A[0]} &= \\begin{bmatrix}2 & 0\\end{bmatrix}\\\\\n",
"\\mathtt{A[[0,2]]} &= \\begin{bmatrix}2 & 0\\\\ 8 & 2\\\\ 2 & 0\\end{bmatrix}\\\\\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
},
"colab_type": "code",
"id": "r-crrwNWw5Bo",
"outputId": "8ddcd751-5ae5-4873-d234-4a7f137a4524"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2 0] (2,)\n",
"[[2 0]\n",
" [8 2]] (2, 2)\n",
"[[2 0]] (1, 2)\n"
]
}
],
"source": [
"print(A[0], A[0].shape) # Primeira linha de A\n",
"print(A[[0,2]], A[[0,2]].shape) # Primeira e terceira linha de A\n",
"print(A[[0]], A[[0]].shape) # Primeira linha de A"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "aPuiGsipo1DA"
},
"source": [
"$$\\begin{aligned}\n",
"\\mathtt{A[:,0]} &= \\begin{bmatrix}2 & 4 & 8\\end{bmatrix}\\\\\n",
"\\mathtt{A[:,[0]]} &= \\begin{bmatrix}2 \\\\ 4 \\\\ 8\\end{bmatrix}\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 136
},
"colab_type": "code",
"id": "KKjo_gvZmYgm",
"outputId": "65738d05-9b76-44d6-d9a5-9a24c9107a5a"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2 4 8] (3,)\n",
"[[2 0]\n",
" [4 6]\n",
" [8 2]] (3, 2)\n",
"[[2]\n",
" [4]\n",
" [8]] (3, 1)\n"
]
}
],
"source": [
"print(A[:,0], A[:,0].shape) # Primeira coluna do A\n",
"print(A[:,[0,1]], A[:,[0,1]].shape) # Primeira e segunda coluna do A\n",
"print(A[:,[0]], A[:,[0]].shape) # Primeira coluna do A"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "fVB8eWYuA56y"
},
"source": [
"## Matrizes notáveis\n",
"\n",
"$$\\begin{aligned}\n",
"\\boldsymbol{I}_n = \\mathtt{np.eye(n)} &= \\begin{bmatrix}1 & 0 & \\ldots\\\\ 0 & 1 & \\ldots\\\\ \\vdots&\\vdots&\\vdots\\\\ 0 & \\ldots & 1\\end{bmatrix}&\n",
"\\boldsymbol{0}_{n,m} = \\mathtt{np.zeros((n,m))} &= \\begin{bmatrix}0 & 0 & \\ldots\\\\ 0 & 0 & \\ldots\\\\ \\vdots&\\vdots&\\vdots\\\\ 0 & \\ldots & 0\\end{bmatrix}\\\\\n",
"\\boldsymbol{1}_{n,m} = \\mathtt{np.ones((n,m))} &= \\begin{bmatrix}1 & 1 & \\ldots\\\\ 1 & 1 & \\ldots\\\\ \\vdots&\\vdots&\\vdots\\\\ 1 & \\ldots & 1\\end{bmatrix}\\\\\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 238
},
"colab_type": "code",
"id": "2KZ335xCB20E",
"outputId": "3c80c609-5d15-4d91-a5cd-63351337e12c"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[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.]]\n",
"[[0. 0. 0.]\n",
" [0. 0. 0.]\n",
" [0. 0. 0.]\n",
" [0. 0. 0.]\n",
" [0. 0. 0.]]\n",
"[[1.]\n",
" [1.]\n",
" [1.]]\n"
]
}
],
"source": [
"print(np.eye(5))\n",
"print(np.zeros((5,3)))\n",
"print(np.ones((3,1)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Além dessas matrizes notáveis, também podemos fazer matrizes aleatórias:\n",
"\n",
"`np.random.rand(n,m)` é uma matrix $n$ por $m$ onde $[\\mathtt{np.random.rand(n,m)}]_{ij} \\sim \\mathcal{U}(0,1)$.\n",
"\n",
"Ou seja, $[\\mathtt{np.random.rand(n,m)}]_{ij} \\in [0,1)$\n",
"\n",
"`np.random.randn(n,m)` é uma matrix $n$ por $m$ onde $[\\mathtt{np.random.randn(n,m)}]_{ij} \\sim \\mathcal{N}(0,1)$\n",
"\n",
"Ou seja, $[\\mathtt{np.random.randn(n,m)}]_{ij} \\in (-\\infty,+\\infty)$"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.04171974 0.90439885 0.59571394]\n",
" [0.72900422 0.55129751 0.8801676 ]\n",
" [0.29224732 0.8048552 0.48309872]\n",
" [0.96284565 0.37758793 0.04898316]\n",
" [0.19848108 0.70421063 0.35482943]]\n",
"[[ 1.05600607 -1.48445668]\n",
" [ 0.64788863 -1.16667704]]\n"
]
}
],
"source": [
"print(np.random.rand(5,3))\n",
"print(np.random.randn(2,2))"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "HpiBGmIjmmXO"
},
"source": [
"## Operações lineares"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"colab_type": "code",
"id": "K1mE0VBilKIY",
"outputId": "4f604a8d-e54f-4489-d3b0-547e061e81b5"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[10 0]\n",
" [20 30]\n",
" [40 10]]\n"
]
}
],
"source": [
"print(5 * A)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "hF53unHnsP2W"
},
"source": [
"Ou seja...\n",
"$$\\begin{aligned}\n",
"\\mathtt{5 * A} = 5\\boldsymbol{A} &= \\begin{bmatrix}2\\cdot 5 & 0\\cdot 5\\\\ 4\\cdot 5 & 6\\cdot 5\\\\ 8\\cdot 5 & 2\\cdot 5\\end{bmatrix}\\\\[0.3em]\n",
"&= \\begin{bmatrix}10 & 0\\\\ 20 & 30\\\\ 40 & 10\\end{bmatrix}\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"colab_type": "code",
"id": "mgxWPeBfmtgF",
"outputId": "c2e0d519-2d76-48ef-ebc4-28f2a91ce840"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 4 0]\n",
" [ 8 12]\n",
" [16 4]]\n"
]
}
],
"source": [
"print(A + A)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "MdEFzm9Bmvmc"
},
"source": [
"Ou seja...\n",
"$$\\begin{aligned}\n",
"\\mathtt{A + A} = \\boldsymbol{A}+\\boldsymbol{A} &= \\begin{bmatrix}2 & 0\\\\ 4 & 6\\\\ 8 & 2\\end{bmatrix}+\\begin{bmatrix}2 & 0\\\\ 4 & 6\\\\ 8 & 2\\end{bmatrix}\\\\[0.3em]\n",
"&= \\begin{bmatrix}4 & 0\\\\ 8 & 12\\\\ 26 & 4\\end{bmatrix}\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"colab_type": "code",
"id": "jAkvCHYfnCPF",
"outputId": "d5d6de3d-830b-4de3-e284-92537285c007"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[10 0]\n",
" [20 18]\n",
" [40 6]]\n"
]
}
],
"source": [
"print(v1 * A)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "Z6MOwqaNnH1S"
},
"source": [
"$$\\begin{aligned}\n",
"\\mathtt{v1 * A} = \\boldsymbol{v}_1 \\odot \\boldsymbol{A} &= \\begin{bmatrix}5 & 3\\end{bmatrix}\\odot\\begin{bmatrix}2 & 0\\\\ 4 & 6\\\\ 8 & 2\\end{bmatrix}\\\\[0.3em]\n",
"&= \\begin{bmatrix}5\\cdot 2 & 3\\cdot 0\\\\ 5\\cdot 4 & 3\\cdot 6\\\\ 5\\cdot 8 & 3\\cdot 2\\end{bmatrix}\\\\\n",
"&= \\begin{bmatrix}10 & 0\\\\ 20 & 18\\\\ 40 & 6\\end{bmatrix}\\\\\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"colab_type": "code",
"id": "Kfr0uegMkws5",
"outputId": "559093f4-2be7-4642-fa62-9ad60055eb91"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 7 3]\n",
" [ 9 9]\n",
" [13 5]]\n"
]
}
],
"source": [
"print(v1 + A)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "qKXp3XlQk4Vc"
},
"source": [
"$$\\begin{aligned}\n",
"\\mathtt{v1 + A} = \\boldsymbol{v}_1 \\oplus \\boldsymbol{A} &= \\begin{bmatrix}5 & 3\\end{bmatrix}\\oplus\\begin{bmatrix}2 & 0\\\\ 4 & 6\\\\ 8 & 2\\end{bmatrix}\\\\[0.3em]\n",
"&= \\begin{bmatrix}5 & 3\\\\ 5 & 3\\\\ 5 & 3\\end{bmatrix}+\\begin{bmatrix}2 & 0\\\\ 4 & 6\\\\ 8 & 2\\end{bmatrix}\\\\\n",
"&= \\begin{bmatrix}7 & 3\\\\ 9 & 9\\\\ 13 & 5\\end{bmatrix}\\\\\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "kQ1gTxkteYEE",
"outputId": "44b31cbd-0c34-4a6a-a29f-5b2eb630750b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"operands could not be broadcast together with shapes (3,) (3,2) \n"
]
}
],
"source": [
"try:\n",
" v2 + A\n",
"except Exception as e:\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "X5BP2ZSClM5t",
"outputId": "c9fc5aa3-3c3b-4df6-dcd9-9b8047207c19"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"operands could not be broadcast together with shapes (3,2) (2,2) \n"
]
}
],
"source": [
"try:\n",
" A + B\n",
"except Exception as e:\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Operações não-lineares"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\\begin{aligned}\n",
"\\boldsymbol{A} &= \\begin{bmatrix}2 & 0\\\\ 4 & 6\\\\ 8 & 2\\end{bmatrix}\\\\\n",
"\\mathtt{A ** 2} &= \\begin{bmatrix}2^2 & 0^2\\\\ 4^2 & 6^2\\\\ 8^2 & 2^2\\end{bmatrix}\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 4 0]\n",
" [16 36]\n",
" [64 4]]\n"
]
}
],
"source": [
"print(A ** 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\\begin{aligned}\n",
"\\boldsymbol{A} &= \\begin{bmatrix}2 & 0\\\\ 4 & 6\\\\ 8 & 2\\end{bmatrix}\\\\\n",
"\\mathtt{np.sqrt(A)} &= \\begin{bmatrix}\\sqrt{2} & \\sqrt{0}\\\\ \\sqrt{4} & \\sqrt{6}\\\\ \\sqrt{8} & \\sqrt{2}\\end{bmatrix}\\\\\n",
"\\mathtt{A ** 0.5} &= \\begin{bmatrix}2^\\frac{1}{2} & 0^\\frac{1}{2}\\\\ 4^\\frac{1}{2} & 6^\\frac{1}{2}\\\\ 8^\\frac{1}{2} & 2^\\frac{1}{2}\\end{bmatrix}\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1.41421356 0. ]\n",
" [2. 2.44948974]\n",
" [2.82842712 1.41421356]]\n",
"[[1.41421356 0. ]\n",
" [2. 2.44948974]\n",
" [2.82842712 1.41421356]]\n"
]
}
],
"source": [
"print(np.sqrt(A))\n",
"print(A ** 0.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Operações de agregação"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\\begin{aligned}\n",
"\\boldsymbol{A} &= \\begin{bmatrix}2 & 0\\\\ 4 & 6\\\\ 8 & 2\\end{bmatrix}\\\\\n",
"\\mathtt{np.mean(A)} &= \\frac{2 + 0 + 4 + 6 + 8 + 2}{2\\cdot 3}\\\\\n",
"\\mathtt{np.sum(A)} &= 2 + 0 + 4 + 6 + 8 + 2\\\\\n",
"\\mathtt{np.prod(A)} &= 2 \\cdot 0 \\cdot 4 \\cdot 6 \\cdot 8 \\cdot 2\\\\\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3.6666666666666665, 22, 0)"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.mean(A), np.sum(A), np.prod(A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\\begin{aligned}\n",
"\\boldsymbol{A} &= \\begin{bmatrix}2 & 0\\\\ 4 & 6\\\\ 8 & 2\\end{bmatrix}\\\\\n",
"\\mathtt{np.mean(A, axis=0)} &= \\frac{1}{3}\\begin{bmatrix}2 + 4 + 8 & 0 + 6 + 2\\end{bmatrix}\\\\\n",
"\\mathtt{np.sum(A, axis=0)} &= \\begin{bmatrix}2 + 4 + 8 & 0 + 6 + 2\\end{bmatrix}\\\\\n",
"\\mathtt{np.prod(A, axis=0)} &= \\begin{bmatrix}2 \\cdot 4 \\cdot 8 & 0 \\cdot 6 \\cdot 2\\end{bmatrix}\\\\\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([4.66666667, 2.66666667]), array([14, 8]), array([64, 0]))"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.mean(A, axis=0), np.sum(A, axis=0), np.prod(A, axis=0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\\begin{aligned}\n",
"\\boldsymbol{A} &= \\begin{bmatrix}2 & 0\\\\ 4 & 6\\\\ 8 & 2\\end{bmatrix}\\\\\n",
"\\mathtt{np.mean(A, axis=1)} &= \\frac{1}{2}\\begin{bmatrix}2 + 0 & 4 + 6 & 8 + 2\\end{bmatrix}\\\\\n",
"\\mathtt{np.sum(A, axis=1)} &= \\begin{bmatrix}2 + 0 & 4 + 6 & 8 + 2\\end{bmatrix}\\\\\n",
"\\mathtt{np.prod(A, axis=1)} &= \\begin{bmatrix}2 \\cdot 0 & 4 \\cdot 6 & 8 \\cdot 2\\end{bmatrix}\\\\\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([1., 5., 5.]), array([ 2, 10, 10]), array([ 0, 24, 16]))"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.mean(A, axis=1), np.sum(A, axis=1), np.prod(A, axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[4.66666667, 2.66666667]])"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.mean(A, axis=0, keepdims=True)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-2.66666667, -2.66666667],\n",
" [-0.66666667, 3.33333333],\n",
" [ 3.33333333, -0.66666667]])"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A - np.mean(A, axis=0, keepdims=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "KfbeQa8DmjsG"
},
"source": [
"## Multiplicação de Matriz\n",
"\n",
"Para matrizes $\\boldsymbol{X} \\in \\mathbb{R}^{a\\times b}$ e $\\boldsymbol{Y} \\in \\mathbb{R}^{b\\times c}$, temos que\n",
"$\\boldsymbol{XY} \\in \\mathbb{R}^{a\\times c}$. Em Python, essa operação é representada por $\\texttt{X @ Y}$.\n",
"\n",
"A multiplicação é definida por:\n",
"$$\\begin{aligned}\n",
"(\\boldsymbol{XY})_{ij} = \\sum_k \\boldsymbol{X}_{ik}\\boldsymbol{Y}_{kj}\n",
"\\end{aligned}$$"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
},
"colab_type": "code",
"id": "gmHrXwu8khmU",
"outputId": "74b4871d-d0fa-49f1-8940-0f98800ac753"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 2 6]\n",
" [34 54]\n",
" [18 38]]\n",
"Dimensão de A @ B: (3, 2)\n"
]
}
],
"source": [
"print(A @ B)\n",
"print('Dimensão de A @ B: ',(A@B).shape)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "4fXEq111mI3j",
"outputId": "2ca04104-0df1-4c6c-bcf7-27461bc2e90d"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 3 is different from 2)\n"
]
}
],
"source": [
"try:\n",
" B @ A\n",
"except Exception as e:\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"colab_type": "code",
"id": "yoshGOOdkoJq",
"outputId": "eccfa558-b21a-425d-c05f-af3744c3dda9"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 28 332 204]\n",
"[ 28 332 204]\n"
]
}
],
"source": [
"print(A @ B @ v1)\n",
"print((A @ B) @ v1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "JZY4P3LcAZkL"
},
"source": [
"### Inversão de matriz\n",
"Outra operação bastante comum é multiplicar uma matriz pela inversa dela...\n",
"\n",
"Seja $\\boldsymbol{B}$ uma matriz inversível, então $\\boldsymbol{B}^{-1} \\boldsymbol{B} = \\boldsymbol{I}$\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 136
},
"colab_type": "code",
"id": "8BBG4KNVDVi-",
"outputId": "42fba1b9-a2d3-4dbc-faf4-f73d9125f60c"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 3]\n",
" [5 7]]\n"
]
}
],
"source": [
"print(B)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 136
},
"colab_type": "code",
"id": "8BBG4KNVDVi-",
"outputId": "42fba1b9-a2d3-4dbc-faf4-f73d9125f60c"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-0.875 0.375]\n",
" [ 0.625 -0.125]]\n"
]
}
],
"source": [
"B_inv = np.linalg.inv(B)\n",
"print(B_inv)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 136
},
"colab_type": "code",
"id": "8BBG4KNVDVi-",
"outputId": "42fba1b9-a2d3-4dbc-faf4-f73d9125f60c"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1.00000000e+00 4.44089210e-16]\n",
" [-1.11022302e-16 1.00000000e+00]]\n"
]
}
],
"source": [
"print(B_inv @ B)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Também funciona com vetores:\n",
"$\\boldsymbol{B}^{-1} \\boldsymbol{v}_1$"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "McWrAEwsEUz5",
"outputId": "cf360686-bcbb-4ecf-8667-a8d6af27e838"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-3.25 2.75]\n"
]
}
],
"source": [
"print(B_inv @ v1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "rIK8beRdEZBQ"
},
"source": [
"Porém, existe uma outra forma mais eficiente de computar a mesma coisa:\n",
"\n",
"Se $\\boldsymbol{B}^{-1} \\boldsymbol{v}_1 = \\boldsymbol{x}$, então $\\boldsymbol{B} \\boldsymbol{x} = \\boldsymbol{v}_1$.\n",
"Ou seja, estamos resolvendo o sistema de equações lineares com coeficientes $\\boldsymbol{B}$ e resultado $\\boldsymbol{v}_1$"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "ImCfS6qjEeQF",
"outputId": "fc2c7d0b-3bab-4d88-db07-290fe7023b5f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-3.25 2.75]\n"
]
}
],
"source": [
"print(np.linalg.solve(B,v1))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"colab_type": "code",
"id": "QB_d3qr0EIQh",
"outputId": "00acc07c-f97e-4136-90c7-0d82cfed1e6f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1.00000000e+00 0.00000000e+00]\n",
" [-3.46944695e-17 1.00000000e+00]]\n"
]
}
],
"source": [
"print(np.linalg.solve(B,B))"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "n24XQHP9FRUi"
},
"source": [
"## Leitura de arquivos com dados\n",
"\n",
"Normalmente os dados na disciplina serão recebidos no formato CSV, isso significa que o dado é estruturado da seguinte maneira:\n",
"```\n",
"idade, pressão_sanguínea # Um cabeçalho opcional\n",
"39, 144 # Dados separados por vírgulas ou outro separador\n",
"47, 220\n",
"....\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "QOUfDIt7GPCb"
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 39., 144.],\n",
" [ 47., 220.],\n",
" [ 45., 138.],\n",
" [ 47., 145.],\n",
" [ 65., 162.],\n",
" [ 46., 142.],\n",
" [ 67., 170.],\n",
" [ 42., 124.],\n",
" [ 67., 158.],\n",
" [ 56., 154.],\n",
" [ 64., 162.],\n",
" [ 56., 150.],\n",
" [ 59., 140.],\n",
" [ 34., 110.],\n",
" [ 42., 128.],\n",
" [ 48., 130.],\n",
" [ 45., 135.],\n",
" [ 17., 114.],\n",
" [ 20., 116.],\n",
" [ 19., 124.],\n",
" [ 36., 136.],\n",
" [ 50., 142.],\n",
" [ 39., 120.],\n",
" [ 21., 120.],\n",
" [ 44., 160.],\n",
" [ 53., 158.],\n",
" [ 63., 144.],\n",
" [ 29., 130.],\n",
" [ 25., 125.],\n",
" [ 69., 175.]])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pressão_dataset = np.genfromtxt('./pressão.txt', delimiter=',', skip_header=1)\n",
"pressão_dataset"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 14., 25., 620.],\n",
" [ 28., 25., 1315.],\n",
" [ 41., 25., 2120.],\n",
" [ 55., 25., 2600.],\n",
" [ 69., 25., 3110.],\n",
" [ 83., 25., 3535.],\n",
" [ 97., 25., 3935.],\n",
" [ 111., 25., 4465.],\n",
" [ 125., 25., 4530.],\n",
" [ 139., 25., 4570.],\n",
" [ 153., 25., 4600.],\n",
" [ 14., 27., 625.],\n",
" [ 28., 27., 1215.],\n",
" [ 41., 27., 2110.],\n",
" [ 55., 27., 2805.],\n",
" [ 69., 27., 3255.],\n",
" [ 83., 27., 4015.],\n",
" [ 97., 27., 4315.],\n",
" [ 111., 27., 4495.],\n",
" [ 125., 27., 4535.],\n",
" [ 139., 27., 4600.],\n",
" [ 153., 27., 4600.],\n",
" [ 14., 29., 590.],\n",
" [ 28., 29., 1305.],\n",
" [ 41., 29., 2140.],\n",
" [ 55., 29., 2890.],\n",
" [ 69., 29., 3920.],\n",
" [ 83., 29., 3920.],\n",
" [ 97., 29., 4515.],\n",
" [ 111., 29., 4520.],\n",
" [ 125., 29., 4525.],\n",
" [ 139., 29., 4565.],\n",
" [ 153., 29., 4566.],\n",
" [ 14., 31., 590.],\n",
" [ 28., 31., 1205.],\n",
" [ 41., 31., 1915.],\n",
" [ 55., 31., 2140.],\n",
" [ 69., 31., 2710.],\n",
" [ 83., 31., 3020.],\n",
" [ 97., 31., 3030.],\n",
" [ 111., 31., 3040.],\n",
" [ 125., 31., 3180.],\n",
" [ 139., 31., 3257.],\n",
" [ 153., 31., 3214.]])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"peixe_dataset = np.genfromtxt('./peixe.txt', delimiter=',')\n",
"peixe_dataset"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "dDlFNckOEiwA"
},
"source": [
"## Exercícios"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "cC-WOVEAEsph"
},
"source": [
"### Computar a fórmula da normalização escore-Z\n",
"\n",
"Dado um conjunto de dados $\\boldsymbol{X} = [\\boldsymbol{x}_0, \\boldsymbol{x}_1, \\ldots, \\boldsymbol{x}_n]$, a normalização por escore-Z é dada por:\n",
"$$\\boldsymbol{x_i} = \\frac{\\boldsymbol{x_i} - \\boldsymbol{\\mu}}{\\boldsymbol{\\sigma}}$$\n",
"\n",
"Onde:\n",
"$$\\begin{aligned}\n",
"\\boldsymbol{\\mu} &= \\frac{1}{n}\\sum_i^n \\boldsymbol{x_i}\\\\\n",
"\\boldsymbol{\\sigma} &= \\sqrt{\\frac{1}{n-1}\\sum_i^n (\\boldsymbol{x_i}-\\boldsymbol{\\mu})^2}\\\\\n",
"\\end{aligned}$$\n",
"\n",
"Use a fórmula para normalizar o conjunto de dados `peixe` sem usar nenhum `for` ou `while`."
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 765
},
"colab_type": "code",
"id": "KDAFy_3ZC04Q",
"outputId": "a2c518b9-7be8-4e38-e571-ea9e078161fe"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Média das colunas: [-7.56970244e-17 0.00000000e+00 1.16068771e-16]\n"
]
}
],
"source": [
"# Escrever aqui"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "cC-WOVEAEsph"
},
"source": [
"### Encontrar raízes de funções\n",
"\n",
"Dada uma função $f$, o seguinte procedimento consegue encontrar aproximar zeros desta função:\n",
"\n",
"1. Inicialize $\\tilde{x}_0$ com um chute inicial e escolha uma tolerância $\\epsilon$;\n",
"2. Compute: $$\\tilde{x}_{t} = \\tilde{x}_{t-1} - \\frac{f(\\tilde{x}_{t-1})}{f'(\\tilde{x}_{t-1})}$$\n",
"3. Repita o passo 2 até que $f(\\tilde{x}_t) \\leq \\epsilon$.\n",
"\n",
"Implemente esse procedimento para $f(x) = x^2 - 2$, teste seu resultado computando $\\tilde{x} \\cdot \\tilde{x}$."
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x_tilde: 1.4142156862745099\n",
"x_tilde²: 2.000006007304883\n"
]
}
],
"source": [
"# Escrever aqui"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Resolver uma regressão linear através de mínimos quadrados ordinários\n",
"\n",
"Como visto na aula de regressão linear, dado o seguinte problema:\n",
"$$\\begin{aligned}\n",
"\\boldsymbol{X}\\boldsymbol{W}^{\\mathbf{T}} = \\hat{\\boldsymbol{y}}\\\\\n",
"\\text{Queremos encontrar:}\\\\\n",
"\\tilde{\\boldsymbol{W}} = \\arg\\min_{\\boldsymbol{W}} ||\\boldsymbol{W}^{\\mathbf{T}} \\boldsymbol{X} - \\boldsymbol{y}||^2\n",
"\\end{aligned}$$\n",
"\n",
"Como visto, sabemos que:\n",
"$$\\tilde{\\boldsymbol{W}} = (\\boldsymbol{X}^{\\mathbf{T}}\\boldsymbol{X})^{-1} \\boldsymbol{X}^{\\mathbf{T}}\\boldsymbol{y}$$\n",
"\n",
"Encontre o $\\tilde{\\boldsymbol{W}}$ pro dataset `peixe` onde:\n",
"$$\\boldsymbol{X} = \\mathtt{peixe\\_dataset[:,[1,2]}\\\\\\boldsymbol{y} = \\mathtt{peixe\\_dataset[:,[0]]}$$\n",
"\n",
"Sem utilizar estrutura de repetição alguma, calcule a raíz do erro quadrático médio:\n",
"$$\\mathrm{RMSE} = \\sqrt{\\frac{1}{n}\\sum_i^n (\\boldsymbol{y} - \\hat{\\boldsymbol{y}})^2}$$"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"RMSE: 655.6771573167042\n"
]
}
],
"source": [
"# Escrever aqui"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "AulaNumpy.ipynb",
"provenance": [],
"toc_visible": true
},
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
14,25,620
28,25,1315
41,25,2120
55,25,2600
69,25,3110
83,25,3535
97,25,3935
111,25,4465
125,25,4530
139,25,4570
153,25,4600
14,27,625
28,27,1215
41,27,2110
55,27,2805
69,27,3255
83,27,4015
97,27,4315
111,27,4495
125,27,4535
139,27,4600
153,27,4600
14,29,590
28,29,1305
41,29,2140
55,29,2890
69,29,3920
83,29,3920
97,29,4515
111,29,4520
125,29,4525
139,29,4565
153,29,4566
14,31,590
28,31,1205
41,31,1915
55,31,2140
69,31,2710
83,31,3020
97,31,3030
111,31,3040
125,31,3180
139,31,3257
153,31,3214
idade,pressão
39,144
47,220
45,138
47,145
65,162
46,142
67,170
42,124
67,158
56,154
64,162
56,150
59,140
34,110
42,128
48,130
45,135
17,114
20,116
19,124
36,136
50,142
39,120
21,120
44,160
53,158
63,144
29,130
25,125
69,175
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment