{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Vetores e Álgebra Matricial para Estatística em linguagem Python\n", "\n", "por Cibele Russo\n", "\n", "ICMC USP São Carlos\n", "\n", "Leitura recomendada: https://hadrienj.github.io/posts/Deep-Learning-Book-Series-2.1-Scalars-Vectors-Matrices-and-Tensors/" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Vetores (coluna)**" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1],\n", " [2],\n", " [5]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[1],[2],[5]])\n", "a" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[3],\n", " [5],\n", " [3]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.array([[3],[5],[3]])\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Soma de dois vetores**" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[4],\n", " [7],\n", " [8]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a+b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Produto interno**" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[28]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = np.dot(np.transpose(b),a)\n", "c" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[28]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = np.transpose(b).dot(a)\n", "c" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[28]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = b.T.dot(a)\n", "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Matrizes**" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[7, 5, 3],\n", " [5, 6, 2],\n", " [2, 2, 8]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.array([[7,5,3],[5,6,2],[2,2,8]])\n", "A" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 6, 3, 3],\n", " [12, 6, 6],\n", " [ 4, 5, 8]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = np.array([[6,3,3],[12,6,6],[4,5,8]])\n", "B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Multiplicação por escalar**" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 2],\n", " [ 4],\n", " [10]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w = 2\n", "w*a" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[14, 10, 6],\n", " [10, 12, 4],\n", " [ 4, 4, 16]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w*A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Produto de duas matrizes (não é comutativo)**" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[114, 66, 75],\n", " [110, 61, 67],\n", " [ 68, 58, 82]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.dot(B)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 63, 54, 48],\n", " [126, 108, 96],\n", " [ 69, 66, 86]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B.dot(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Propriedade:** \n", " \n", " (A B)^T = B^T A^T" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[114, 110, 68],\n", " [ 66, 61, 58],\n", " [ 75, 67, 82]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.transpose(A.dot(B))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[114, 110, 68],\n", " [ 66, 61, 58],\n", " [ 75, 67, 82]])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.transpose(B).dot(np.transpose(A))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Determinante de uma matriz**" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "122.00000000000003" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linalg.det(A)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linalg.det(B)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Traço de uma matriz**" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.trace(A)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.trace(B)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Posto de uma matriz**" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linalg.matrix_rank(A)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linalg.matrix_rank(B)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Dimensões de uma matriz**\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.shape" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 1)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Matriz Identidade**" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.eye(3)\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[7., 5., 3.],\n", " [5., 6., 2.],\n", " [2., 2., 8.]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.eye(3).dot(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Matriz inversa**" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.36065574, -0.27868852, -0.06557377],\n", " [-0.29508197, 0.40983607, 0.00819672],\n", " [-0.01639344, -0.03278689, 0.13934426]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A_inv = np.linalg.inv(A)\n", "A_inv\n" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 0., -0.],\n", " [ 0., 1., -0.],\n", " [ 0., -0., 1.]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Conferindo\n", "\n", "np.round(A.dot(A_inv),2)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., -0., 0.],\n", " [ 0., 1., -0.],\n", " [-0., 0., 1.]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.round(A_inv.dot(A),2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Autovalores e autovetores**\n" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([13.3914202 , 1.48862351, 6.11995629]),\n", " array([[-0.67062299, -0.66402049, -0.34287821],\n", " [-0.57909059, 0.74727695, -0.43819835],\n", " [-0.46359346, -0.02557262, 0.83091319]]))" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linalg.eig(A)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([13.3914202 , 1.48862351, 6.11995629])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lam = np.linalg.eig(A)[0]\n", "lam\n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.66566978, -0.37640085, -0.64436491],\n", " [ 0.74584214, -0.30715729, -0.5910786 ],\n", " [-0.02456111, 0.87405766, -0.48520094]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e = np.linalg.eigh(A)[1]\n", "e" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-0.66566978, 0.74584214, -0.02456111])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e[:,0]" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0000000000000004" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.transpose(e[:,0]).dot(e[:,0])" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-2.7755575615628914e-17" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.transpose(e[:,0]).dot(e[:,1])" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "# Matriz de autovetores ortogonal\n", "\n", "P = np.transpose(e).dot(e)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]])" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.round(np.linalg.inv(P),2)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., -0., -0.],\n", " [-0., 1., -0.],\n", " [-0., -0., 1.]])" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.round(np.transpose(P),2)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0000000000000009" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linalg.det(P)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Decomposição de Cholesky**" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[7, 5, 3],\n", " [5, 6, 2],\n", " [2, 2, 8]])" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2.64575131, 0. , 0. ],\n", " [1.88982237, 1.55838744, 0. ],\n", " [0.75592895, 0.3666794 , 2.70076242]])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "W = np.linalg.cholesky(A) \n", "W" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[7., 5., 2.],\n", " [5., 6., 2.],\n", " [2., 2., 8.]])" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "W.dot(W.T)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Produto de Kronecker**" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[7., 5., 3., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [5., 6., 2., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [2., 2., 8., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 7., 5., 3., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 5., 6., 2., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 2., 2., 8., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 7., 5., 3., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 5., 6., 2., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 2., 2., 8., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 7., 5., 3.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 5., 6., 2.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 2., 2., 8.]])" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.kron(np.eye(4), A)" ] }, { "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", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }