Add datasets, fix a few math linear algebra issues

main
Aurélien Geron 2016-05-03 11:35:17 +02:00
parent 28a0372222
commit c564be1ffd
3 changed files with 48 additions and 45 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.ipynb_checkpoints
.DS_Store
my_*

View File

@ -22,7 +22,7 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
@ -158,7 +158,7 @@
},
{
"cell_type": "code",
"execution_count": 138,
"execution_count": 6,
"metadata": {
"collapsed": false
},
@ -735,7 +735,7 @@
"\n",
"$\\theta = \\arccos{\\left ( \\dfrac{\\textbf{u} \\cdot \\textbf{v}}{\\left \\Vert \\textbf{u} \\right \\| \\times \\left \\Vert \\textbf{v} \\right \\|} \\right ) }$\n",
"\n",
"Note that if $\\textbf{u} \\cdot \\textbf{v} = 0$, it follows that $\\theta = \\dfrac{π}{4}$. In other words, if the dot product of two non-null vectors are orthogonal, it means that they are orthogonal.\n",
"Note that if $\\textbf{u} \\cdot \\textbf{v} = 0$, it follows that $\\theta = \\dfrac{π}{4}$. In other words, if the dot product of two non-null vectors is zero, it means that they are orthogonal.\n",
"\n",
"Let's use this formula to calculate the angle between $\\textbf{u}$ and $\\textbf{v}$ (in radians):"
]
@ -898,9 +898,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Caution**: the `size` attribute represents the number of elements in the `ndarray`, not the matrix's size:\n",
"\n",
"$M_{i,j}$"
"**Caution**: the `size` attribute represents the number of elements in the `ndarray`, not the matrix's size:"
]
},
{
@ -1244,7 +1242,7 @@
"metadata": {},
"source": [
"## Scalar multiplication\n",
"A matrix $M$ can be multiplied by a scalar $\\lambda$. The result is noted $\\lambda \\times M$ or $\\lambda \\cdot M$ or simply $\\lambda M$, and it is a matrix of the same size as $M$ with all elements multiplied by $\\lambda$:\n",
"A matrix $M$ can be multiplied by a scalar $\\lambda$. The result is noted $\\lambda M$, and it is a matrix of the same size as $M$ with all elements multiplied by $\\lambda$:\n",
"\n",
"$\\lambda M =\n",
"\\begin{bmatrix}\n",
@ -1255,6 +1253,10 @@
" \\lambda \\times M_{m1} & \\lambda \\times M_{m2} & \\lambda \\times M_{m3} & \\cdots & \\lambda \\times M_{mn} \\\\\n",
"\\end{bmatrix}$\n",
"\n",
"A more concise way of writing this is:\n",
"\n",
"$(\\lambda M)_{i,j} = \\lambda (M)_{i,j}$\n",
"\n",
"In NumPy, simply use the `*` operator to multiply a matrix by a scalar. For example:"
]
},
@ -1273,7 +1275,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Scalar multiplication is **commutative**, meaning that $\\lambda \\times M = M \\times \\lambda$. For example:"
"Scalar multiplication is also defined on the right hand side, and gives the same result: $M \\lambda = \\lambda M$. For example:"
]
},
{
@ -1291,7 +1293,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"It is also **associative**, meaning that $\\alpha \\times (\\beta \\times M) = (\\alpha \\times \\beta) \\times M$, where $\\alpha$ and $\\beta$ are scalars. For example:"
"This makes scalar multiplication **commutative**.\n",
"\n",
"It is also **associative**, meaning that $\\alpha (\\beta M) = (\\alpha \\times \\beta) M$, where $\\alpha$ and $\\beta$ are scalars. For example:"
]
},
{
@ -1320,7 +1324,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, it is **distributive over addition** of matrices, meaning that $\\lambda \\times (Q + R) = \\lambda \\times Q + \\lambda \\times R$:"
"Finally, it is **distributive over addition** of matrices, meaning that $\\lambda (Q + R) = \\lambda Q + \\lambda R$:"
]
},
{
@ -1349,10 +1353,10 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Matrix dot product\n",
"## Matrix multiplication\n",
"So far, matrix operations have been rather intuitive. But multiplying matrices is a bit more involved.\n",
"\n",
"A matrix $Q$ of size $m \\times n$ can be multiplied by a matrix $R$ of size $n \\times q$. This is also called a **dot product** and it is noted $Q \\cdot R$ or simply $QR$. The result $P$ is an $m \\times q$ matrix where each element is computed as a sum of products:\n",
"A matrix $Q$ of size $m \\times n$ can be multiplied by a matrix $R$ of size $n \\times q$. It is noted simply $QR$ without multiplication sign or dot. The result $P$ is an $m \\times q$ matrix where each element is computed as a sum of products:\n",
"\n",
"$P_{i,j} = \\sum_{k=1}^n{Q_{i,k} \\times R_{k,j}}$\n",
"\n",
@ -1396,10 +1400,10 @@
"source": [
"Let's multiply two matrices in NumPy, using `ndarray`'s `dot` method:\n",
"\n",
"$E = A \\cdot D = \\begin{bmatrix}\n",
"$E = AD = \\begin{bmatrix}\n",
" 10 & 20 & 30 \\\\\n",
" 40 & 50 & 60\n",
"\\end{bmatrix} \\cdot \n",
"\\end{bmatrix} \n",
"\\begin{bmatrix}\n",
" 2 & 3 & 5 & 7 \\\\\n",
" 11 & 13 & 17 & 19 \\\\\n",
@ -1484,9 +1488,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This illustrates the fact that **matrix multiplication is *NOT* commutative**: in general $Q \\cdot R ≠ R \\cdot Q$\n",
"This illustrates the fact that **matrix multiplication is *NOT* commutative**: in general $QR ≠ RQ$\n",
"\n",
"In fact, $Q \\cdot R$ and $R \\cdot Q$ are only *both* defined if $Q$ has size $m \\times n$ and $R$ has size $n \\times m$. Let's look at an example where both *are* defined and show that they are (in general) *NOT* equal:"
"In fact, $QR$ and $RQ$ are only *both* defined if $Q$ has size $m \\times n$ and $R$ has size $n \\times m$. Let's look at an example where both *are* defined and show that they are (in general) *NOT* equal:"
]
},
{
@ -1520,7 +1524,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"On the other hand, **matrix multiplication *is* associative**, meaning that $Q \\cdot (R \\cdot S) = (Q \\cdot R) \\cdot S$. Let's create a $4 \\times 5$ matrix $G$ to illustrate this:"
"On the other hand, **matrix multiplication *is* associative**, meaning that $Q(RS) = (QR)S$. Let's create a $4 \\times 5$ matrix $G$ to illustrate this:"
]
},
{
@ -1536,7 +1540,7 @@
" [2, 5, 1, 0, 5],\n",
" [9, 11, 17, 21, 0],\n",
" [0, 1, 0, 1, 2]])\n",
"A.dot(D).dot(G) # (AB)G"
"A.dot(D).dot(G) # (AB)G"
]
},
{
@ -1547,14 +1551,14 @@
},
"outputs": [],
"source": [
"A.dot(D.dot(G)) # A(BG)"
"A.dot(D.dot(G)) # A(BG)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is also ***distributive* over addition** of matrices, meaning that $(Q + R) \\cdot S = Q \\cdot S + R \\cdot S$. For example:"
"It is also ***distributive* over addition** of matrices, meaning that $(Q + R)S = QS + RS$. For example:"
]
},
{
@ -1583,9 +1587,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The dot product of a matrix $M$ by the identity matrix (of matching size) results in the same matrix $M$. More formally, if $M$ is an $m \\times n$ matrix, then:\n",
"The product of a matrix $M$ by the identity matrix (of matching size) results in the same matrix $M$. More formally, if $M$ is an $m \\times n$ matrix, then:\n",
"\n",
"$M \\cdot I_n = I_m \\cdot M = M$\n",
"$M I_n = I_m M = M$\n",
"\n",
"This is generally written more concisely (since the size of the identity matrices is unambiguous given the context):\n",
"\n",
@ -1620,7 +1624,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Caution**: NumPy's `*` operator performs elementwise multiplication, *NOT* a dot product:"
"**Caution**: NumPy's `*` operator performs elementwise multiplication, *NOT* a matrix multiplication:"
]
},
{
@ -1632,7 +1636,7 @@
},
"outputs": [],
"source": [
"A * B # NOT a dot product"
"A * B # NOT a matrix multiplication"
]
},
{
@ -1689,7 +1693,7 @@
"\n",
"In other words, ($A^T)_{i,j}$ = $A_{j,i}$\n",
"\n",
"Obviously, if $M$ is a $m \\times n$ matrix, then $M^T$ is a $n \\times m$ matrix.\n",
"Obviously, if $M$ is an $m \\times n$ matrix, then $M^T$ is an $n \\times m$ matrix.\n",
"\n",
"Note: there are a few other notations, such as $M^t$, $M$, or ${^t}M$.\n",
"\n",
@ -2165,7 +2169,7 @@
" plt.grid()\n",
"\n",
"P_rescaled = 0.60 * P\n",
"plot_transformation(P, P_rescaled, \"$P$\", \"$0.6 × P$\", arrows=True)\n",
"plot_transformation(P, P_rescaled, \"$P$\", \"$0.6 P$\", arrows=True)\n",
"plt.show()"
]
},
@ -2173,8 +2177,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dot product Projection onto an axis\n",
"The dot product is more complex to visualize, but it is also the most powerful tool in the box.\n",
"### Matrix multiplication Projection onto an axis\n",
"Matrix multiplication is more complex to visualize, but it is also the most powerful tool in the box.\n",
"\n",
"Let's start simple, by defining a $1 \\times 2$ matrix $U = \\begin{bmatrix} 1 & 0 \\end{bmatrix}$. This row vector is just the horizontal unit vector."
]
@ -2274,7 +2278,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dot product Rotation\n",
"### Matrix multiplication Rotation\n",
"Now let's create a $2 \\times 2$ matrix $V$ containing two unit vectors that make 30° and 120° angles with the horizontal axis:\n",
"\n",
"$V = \\begin{bmatrix} \\cos(30°) & \\sin(30°) \\\\ \\cos(120°) & \\sin(120°) \\end{bmatrix}$"
@ -2300,7 +2304,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's look at the dot product $V \\cdot P$:"
"Let's look at the product $VP$:"
]
},
{
@ -2318,7 +2322,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The first row is equal to $V_{1,*} \\cdot P$, which is the coordinates of the projection of $P$ onto the 30° axis, as we have seen above. The second row is $V_{2,*} \\cdot P$, which is the coordinates of the projection of $P$ onto the 120° axis. So basically we obtained the coordinates of $P$ after rotating the horizontal and vertical axes by 30° (or equivalently after rotating the polygon by -30° around the origin)! Let's plot $V \\cdot P$ to see this:"
"The first row is equal to $V_{1,*} P$, which is the coordinates of the projection of $P$ onto the 30° axis, as we have seen above. The second row is $V_{2,*} P$, which is the coordinates of the projection of $P$ onto the 120° axis. So basically we obtained the coordinates of $P$ after rotating the horizontal and vertical axes by 30° (or equivalently after rotating the polygon by -30° around the origin)! Let's plot $VP$ to see this:"
]
},
{
@ -2330,7 +2334,7 @@
"outputs": [],
"source": [
"P_rotated = V.dot(P)\n",
"plot_transformation(P, P_rotated, \"$P$\", \"$V \\cdot P$\", [-2, 6, -2, 4], arrows=True)\n",
"plot_transformation(P, P_rotated, \"$P$\", \"$VP$\", [-2, 6, -2, 4], arrows=True)\n",
"plt.show()"
]
},
@ -2345,7 +2349,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dot product Other linear transformations\n",
"### Matrix multiplication Other linear transformations\n",
"More generally, any linear transformation $f$ that maps n-dimensional vectors to m-dimensional vectors can be represented as an $m \\times n$ matrix. For example, say $\\textbf{u}$ is a 3-dimensional vector:\n",
"\n",
"$\\textbf{u} = \\begin{pmatrix} x \\\\ y \\\\ z \\end{pmatrix}$\n",
@ -2364,13 +2368,13 @@
"d & e & f\n",
"\\end{bmatrix}$\n",
"\n",
"Now, to compute $f(\\textbf{u})$ we can simply do a dot product:\n",
"Now, to compute $f(\\textbf{u})$ we can simply do a matrix multiplication:\n",
"\n",
"$f(\\textbf{u}) = F \\cdot \\textbf{u}$\n",
"$f(\\textbf{u}) = F \\textbf{u}$\n",
"\n",
"If we have a matric $G = \\begin{bmatrix}\\textbf{u}_1 & \\textbf{u}_2 & \\cdots & \\textbf{u}_q \\end{bmatrix}$, where each $\\textbf{u}_i$ is a 3-dimensional column vector, then $F \\cdot G$ results in the linear transformation of all vectors $\\textbf{u}_i$ as defined by the matrix $F$:\n",
"If we have a matric $G = \\begin{bmatrix}\\textbf{u}_1 & \\textbf{u}_2 & \\cdots & \\textbf{u}_q \\end{bmatrix}$, where each $\\textbf{u}_i$ is a 3-dimensional column vector, then $FG$ results in the linear transformation of all vectors $\\textbf{u}_i$ as defined by the matrix $F$:\n",
"\n",
"$F \\cdot G = \\begin{bmatrix}f(\\textbf{u}_1) & f(\\textbf{u}_2) & \\cdots & f(\\textbf{u}_q) \\end{bmatrix}$\n",
"$FG = \\begin{bmatrix}f(\\textbf{u}_1) & f(\\textbf{u}_2) & \\cdots & f(\\textbf{u}_q) \\end{bmatrix}$\n",
"\n",
"To summarize, the matrix on the left hand side of a dot product specifies what linear transormation to apply to the right hand side vectors. We have already shown that this can be used to perform projections and rotations, but any other linear transformation is possible. For example, here is a transformation known as a *shear mapping*:"
]
@ -2388,7 +2392,7 @@
" [1, 1.5],\n",
" [0, 1]\n",
" ])\n",
"plot_transformation(P, F_shear.dot(P), \"$P$\", \"$F_{shear} \\cdot P$\",\n",
"plot_transformation(P, F_shear.dot(P), \"$P$\", \"$F_{shear} P$\",\n",
" axis=[0, 10, 0, 7])\n",
"plt.show()"
]
@ -2412,7 +2416,7 @@
" [0, 0, 1, 1],\n",
" [0, 1, 1, 0]\n",
" ])\n",
"plot_transformation(Square, F_shear.dot(Square), \"$Square$\", \"$F_{shear} \\cdot Square$\",\n",
"plot_transformation(Square, F_shear.dot(Square), \"$Square$\", \"$F_{shear} Square$\",\n",
" axis=[0, 2.6, 0, 1.8])\n",
"plt.show()"
]
@ -2436,7 +2440,7 @@
" [1.4, 0],\n",
" [0, 1/1.4]\n",
" ])\n",
"plot_transformation(P, F_squeeze.dot(P), \"$P$\", \"$F_{squeeze} \\cdot P$\",\n",
"plot_transformation(P, F_squeeze.dot(P), \"$P$\", \"$F_{squeeze} P$\",\n",
" axis=[0, 7, 0, 5])\n",
"plt.show()"
]
@ -2456,7 +2460,7 @@
},
"outputs": [],
"source": [
"plot_transformation(Square, F_squeeze.dot(Square), \"$Square$\", \"$F_{squeeze} \\cdot Square$\",\n",
"plot_transformation(Square, F_squeeze.dot(Square), \"$Square$\", \"$F_{squeeze} Square$\",\n",
" axis=[0, 1.8, 0, 1.2])\n",
"plt.show()"
]
@ -2480,7 +2484,7 @@
" [1, 0],\n",
" [0, -1]\n",
" ])\n",
"plot_transformation(P, F_reflect.dot(P), \"$P$\", \"$F_{reflect} \\cdot P$\",\n",
"plot_transformation(P, F_reflect.dot(P), \"$P$\", \"$F_{reflect} P$\",\n",
" axis=[-2, 9, -4.5, 4.5])\n",
"plt.show()"
]

View File

@ -28,9 +28,7 @@
},
"outputs": [],
"source": [
"from __future__ import division\n",
"from __future__ import print_function\n",
"from __future__ import unicode_literals"
"from __future__ import division, print_function, unicode_literals"
]
},
{