{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = [1,2,3]\n", "L" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n", "[1, 2, 3]\n", "[1, 2, 3]\n" ] } ], "source": [ "for i in L:\n", " print(L)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3]\n", "[1 2 3]\n", "[1 2 3]\n" ] } ], "source": [ "B = np.array([1,2,3])\n", "for e in B:\n", " print(B)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 4, 6])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = B + B\n", "B" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 4, 8, 12])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = B*2\n", "B" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 1, 2, 3]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = L*2\n", "L" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for ** or pow(): 'list' and 'int'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mL\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for ** or pow(): 'list' and 'int'" ] } ], "source": [ "L**2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L2 = []\n", "for i in L:\n", " L2.append(i*i)\n", "print(L2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.append(4)\n", "L" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2. , 2.82842712, 3.46410162])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sqrt(B)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1.38629436, 2.07944154, 2.48490665])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.log(B)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([5.45981500e+01, 2.98095799e+03, 1.62754791e+05])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.exp(B)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A bit about dot product\n", "\n", "1. First method: $a.b = a^{T} = \\sum_{d = 1}^ba_{d}b_{d}$\n", "2. Second method: $a.b = |a||b|cos\\theta_{ab}$ - not really convenient" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "a = np.array([1,2])\n", "b = np.array([2,1])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "dot = 0" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "for i,j in zip(a,b):\n", " dot += i*j\n", "dot" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 2])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a*b" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## A matrix is essentially a list of lists\n", "\n" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "M = np.array([[1,2], [3,4]])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "L = [[1,2], [3,4]]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[0]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[0][0]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[0][0]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[0,0]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "M2 = np.matrix([[1,2], [3,4]]) #works somewhat similarly to np.array but not exactly same." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M2" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "A = np.array(M2) # converting it into a matrix" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A # you get the same matrix but as an array" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 3],\n", " [2, 4]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.T # Transposing values with T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summarizing: \n", "A matrix is just a 2-dimensional numpy array and a vector is a 1-dimensional numpy array. Or you can see it this way: A matrix is actually like a 2-dimensional vector (a 2-dimensional math object that contains numbers) and a vector is a 1-dim mathematical object containing numbers).
\n", "A col vector is 3x1 and a row vector is 1x3. All you need to remember is 1-D array, which is a vector and 2 or multiple dimensional arrays that are matrices." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Different ways in generating data " ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([1,2,3,4]) #tedious way" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "Z = np.zeros(10) #gives us a vector of 10s using function zeros" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Z # will give you a vector with length 10 of all zeros" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "Z = np.zeros((10, 10)) # for a matrix of 10x10" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Z" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "O = np.ones((10, 10)) # for ones use ones function" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "O # gives you a 10x10 matrix with all numbers" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "RandomNumbers = np.random.random((10,10))" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.36025123, 0.72314466, 0.75980308, 0.4889344 , 0.07937359,\n", " 0.58874256, 0.23199633, 0.15954813, 0.08774048, 0.54883146],\n", " [0.61296478, 0.83142723, 0.42934756, 0.95675646, 0.49010267,\n", " 0.93463107, 0.26782182, 0.27939492, 0.00624542, 0.84110478],\n", " [0.70859914, 0.20049763, 0.73385319, 0.27699811, 0.35205101,\n", " 0.86048545, 0.65022327, 0.0734312 , 0.56611973, 0.77298901],\n", " [0.98237141, 0.58668367, 0.05017497, 0.2214889 , 0.9140263 ,\n", " 0.43997838, 0.18401642, 0.92542465, 0.28174944, 0.15925305],\n", " [0.40004748, 0.27031424, 0.7939485 , 0.55662059, 0.57639734,\n", " 0.22026038, 0.66965651, 0.46994527, 0.64628966, 0.47208764],\n", " [0.25350073, 0.49198554, 0.78046933, 0.80788364, 0.43132166,\n", " 0.44066448, 0.23294878, 0.88109595, 0.03925883, 0.79499483],\n", " [0.00282458, 0.09053165, 0.28571994, 0.28643215, 0.88058071,\n", " 0.95198574, 0.24897644, 0.49677318, 0.54125003, 0.70117 ],\n", " [0.69306255, 0.08100459, 0.99756119, 0.10458792, 0.34034728,\n", " 0.66861924, 0.57013185, 0.29346688, 0.89079804, 0.78827665],\n", " [0.10804648, 0.72548439, 0.31488051, 0.09796583, 0.2034459 ,\n", " 0.8884706 , 0.52542411, 0.36312296, 0.82495533, 0.1989712 ],\n", " [0.24139471, 0.30244504, 0.21736982, 0.05061938, 0.52921445,\n", " 0.11172595, 0.57531755, 0.64141966, 0.80048169, 0.02479354]])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "RandomNumbers # in a matrix of size 10x10" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object cannot be interpreted as an integer", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mGaussianNumbers\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32mmtrand.pyx\u001b[0m in \u001b[0;36mnumpy.random.mtrand.RandomState.randn\u001b[0;34m()\u001b[0m\n", "\u001b[0;32mmtrand.pyx\u001b[0m in \u001b[0;36mnumpy.random.mtrand.RandomState.standard_normal\u001b[0;34m()\u001b[0m\n", "\u001b[0;32m_common.pyx\u001b[0m in \u001b[0;36mnumpy.random._common.cont\u001b[0;34m()\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object cannot be interpreted as an integer" ] } ], "source": [ "GaussianNumbers = np.random.randn((10,10))" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "# It failed becuase Randn function takes each of the dimensions as individual objects, while all others took tuples.\n", "GaussianNumbers = np.random.randn(10,10)\n" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.68873721, 0.40646635, -0.46884281, -0.83962372, 0.83088416,\n", " -0.15753662, -0.4931879 , -1.18580301, -1.65746007, 1.03332734],\n", " [-0.8391799 , 0.01795441, 1.83797408, 0.7605684 , 0.47645297,\n", " 0.23032845, 1.42804264, 0.8264387 , 1.09563666, -1.66961777],\n", " [ 0.63255906, 1.25713922, -0.30602351, 1.39022427, 1.10564676,\n", " -0.09887949, 0.83629044, 0.3796102 , -0.18988134, 0.198571 ],\n", " [-0.70496607, -1.03108825, -0.47010157, 0.53912367, -0.09601299,\n", " -1.16649096, -0.43630682, -0.04293193, -0.85203087, 0.48162699],\n", " [-0.44425368, 0.27800336, 0.00838862, 1.260695 , 1.03102215,\n", " 0.80398706, 0.49112148, 0.03444604, 0.7957862 , 0.70906202],\n", " [-0.70750981, -0.30315887, -1.07026434, -2.57104665, -1.40422339,\n", " 0.98663442, -1.43032106, 0.74071976, 1.10793193, 0.48403673],\n", " [ 0.40077711, 2.03026688, -1.27116926, -2.26781125, -0.82574864,\n", " 1.20537537, -0.25784436, 1.44374597, -0.59805266, 0.01893792],\n", " [ 0.04064512, 0.36757901, -0.88900875, 0.82229535, 0.94278058,\n", " 1.07100589, -0.53000048, 0.55559764, 0.95282399, -0.447541 ],\n", " [ 1.43817237, -0.98333888, 0.07721662, 0.04494657, -1.08003903,\n", " -2.06406738, -0.62480185, 1.59943142, 0.82769623, 1.00406658],\n", " [-0.59786427, -1.31757633, 0.13186859, 0.39142888, -0.2578217 ,\n", " -0.38322703, -1.00889703, 0.44773885, -0.65285235, 0.04785387]])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "GaussianNumbers # will give a gauissian distribution with mean 0 and variance 1" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.04353282914845535" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "GaussianNumbers.mean()" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8851575805787485" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "GaussianNumbers.var()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrix Products or multiplication\n", "1. Matrix multiplication : Inner dimensions must match! if we have A of size (2,3) and B of size (3,3), we can multiply AB (inner dimension is 3) but we cannot multiply BA where inner dimension is 3 and 2!\n", "2. The definition of matrix multiplication is as follows: $$C(i,j) = \\sum_{k=1}^K A(i,k)B(k,j)$$\n", "3. This means $(i,j)^{th}$ entry of C is the sum of the multiplication of all the corresponding element from $i^{th}$ row of A and $j^{th}$ col of B. In other words, $C(ij)$ is the dot product of $i^{th}$ row of A and $j^{th}$ col of B. That is, $C(ij)$ is the dot product of Because of this we use the dot function in numpy and that does what you see as matrix multiplication.\n", "4. Instinctively (in maths that is) you'd want to do an element by element multiplication, for vectors we can do with a * operation, as you may imagine for a 2D array , the * also does an element-wise multiplication ,meaningwhwn you use an * for multidimensional arrya, both must be exactly the same size. This may mean a bit weird, because in other (programming) languages * means real matrix multiplication. So, to summarize -- asterisk(*) means element by element multiplication while dot function does a matrix multiplication.\n", "5. One more funny thing in maths equations, there isn't a well defined real symbol to use a element by element multiplication. Sometimes folks use circle with a . insidde or an x.\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### More matrix operations " ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "# matrix inverse\n", "A = np.array([[1,2], [3,4]])" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "A_inverse = np.linalg.inv(A)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-2. , 1. ],\n", " [ 1.5, -0.5]])" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A_inverse" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-2. , 2. ],\n", " [ 4.5, -2. ]])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A*A_inverse" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1.00000000e+00, 1.11022302e-16],\n", " [0.00000000e+00, 1.00000000e+00]])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.dot(A_inverse) #gives us an identity matrix" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1.0000000e+00, 4.4408921e-16],\n", " [0.0000000e+00, 1.0000000e+00]])" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A_inverse.dot(A) # note this gives us an identity matrix as well" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-2.0000000000000004" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# matrix determinant\n", "np.linalg.det(A)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 4])" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.diag(A) # gives us diagonal elements in a vector" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 0],\n", " [0, 2]])" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Note this: if you pass a 2D array to diag, you get a 1D array (as above), if you pass a 1D array\n", "# 2 get a 2D array where off diagonals are zero as below\n", "np.diag([1,2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Outer product / inner product\n", "1. Outer product: $C(i,j) = A(i)B(j)$\n", " 1.1 Example: $$\\sum = E{(x - μ)(x - μ)^T} ≈ \\frac{1}{N-1}\\sum_{n=1}^N(x_n - \\overline{x})(x_n - \\overline{x})T$$\n", "2. Inner product: C = sum over i {A(i)B(i)}" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[3, 4],\n", " [6, 8]])" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Doing an outer product\n", "a = np.array([1,2])\n", "b = np.array([3,4])\n", "np.outer(a,b)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# dot product ,as you may recall is also known as inner product\n", "np.inner(a,b)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dot(b) # same as above" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Matrix trace is the sum of the diagonals of a matrix\n", "np.diag(A).sum() # See In [92]" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.trace(A)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "# Eigenvalues and Eigenvectors\n", "X = np.random.randn(100,3)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "covariance = np.cov(X)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(100, 100)" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "covariance.shape" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "covariance = np.cov(X.T) # transpose X" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.93667764, -0.04833602, -0.04214239],\n", " [-0.04833602, 0.96862658, 0.02143732],\n", " [-0.04214239, 0.02143732, 0.89064545]])" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "covariance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Eigenvalues and Eigenvectors\n", "\n", "1. eigenvalues, eigenvectors = np.eig(C) # Symmetric matrix is equal of a Transpose of itself\n", "or\n", "2. eigenvalues, eigenvectors = np.eigh(C) # While a Hermitian matrix ia a conjugate of transpose of itself\n", "\n", "So, Symmetricc $A = A^T$, while Hermitian matrix of $A = A^H$ or simply $A^H$ is conjugate transpose of A\n", "\n", "3. Convariance is a symmetric matrix, so we should be able to use eigh..." ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([0.86512642, 0.91291087, 1.01791237]),\n", " array([[ 0.54664956, 0.58209677, -0.60194486],\n", " [ 0.08270375, 0.67781206, 0.73056888],\n", " [ 0.83326727, -0.44914826, 0.32238408]]))" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linalg.eigh(covariance) #gives a tuple" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(np.linalg.eigh(covariance))" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([1.01791237, 0.91291087, 0.86512642]),\n", " array([[-0.60194486, 0.58209677, 0.54664956],\n", " [ 0.73056888, 0.67781206, 0.08270375],\n", " [ 0.32238408, -0.44914826, 0.83326727]]))" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linalg.eig(covariance) # will give us a tuple, first tuple contains 3 eigenvalues and sector tuple contains eigenvectors stored in columns" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(np.linalg.eig(covariance))" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "# You may have notice they can be in different order, try running it again to check it out for yourself!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solving a Linear System\n", "\n", "1. $Ax = b$ # A is matrix, x is col vector of values we are trying to solve for and B is vector of numbers\n", "2. Solution = $A^{-1} = x = A^{-1}b$\n", "3. Notes: Will be adding soon..." ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([3, 4])" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2])" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.array([1,2])\n", "b" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2.22044605e-16, 5.00000000e-01])" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.linalg.inv(A).dot(b)\n", "x" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [], "source": [ "x = np.linalg.solve(A, b) # much intuitive and efficient way to do it. also more accurate." ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.5])" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A simple example...\n", "1. Problem description: In a party, singles pay a entry ticket of 2.50 dollars and couples paid for a single 4.00 dollars . At this big rock concert event, some 20,000 tickets were sold and 75,500 dollars was made by the organizers. How many singles and how many couples partied that night?\n", "\n", " 1.1 X1 = number of singles, X2 = number of couples => \n", " 1.2 X1 + X2 = 20,000 => \n", " 1.3 2.5X1 + 4.00X2 = 75,000\n", "\n", "2. As a matrix this is how it looks like: $$ \n", "\\begin{bmatrix}\n", " 1 & 1 \\\\\n", " 2.5 & 4 \\\\\n", "\\end{bmatrix}\n", "\\begin{bmatrix}\n", " x1 \\\\\n", " x2 \\\\\n", "\\end{bmatrix} = \n", "\\begin{bmatrix}\n", " 20000 \\\\\n", " 75000 \\\\\n", "\\end{bmatrix}$$" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "party_goers = np.array([[1,1], [2.5,4]]) # This is our 2D matrix A\n", "collections = np.array([20000, 75000]) #This is our 1D matrix / vector b" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There were 3333.333333333331 singles and 16666.666666666668 couples; and apparently some one cheated on their partner that night ;)\n" ] } ], "source": [ "np.linalg.solve(party_goers, collections)\n", "print(\"There were \", np.linalg.solve(party_goers, collections)[0],\\\n", " \" singles and \", np.linalg.solve(party_goers, collections)[1] ,\\\n", " \" couples; and apparently some one cheated on their partner that night ;)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More stuff coming here!\n", "Don't go away as I explain and we learn together the following, we first do a recap so you refresh your thoughts again on:\n", "1. Numpy Specific\n", " 1.1 Arrays\n", " 1.2. Array Indexing\n", " 1.3. Datatypes\n", " 1.4. Array Math\n", " 1.5. Broadcasting\n", "2. A super fast refresh on Python lists, dictionaries, classes and functions\n", "3. Pandas w.r.t Numpy\n", "4. Scipy w.r.t Numpy\n", "5. Matplotlib w.r.t Numpy" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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.8.3" } }, "nbformat": 4, "nbformat_minor": 2 }