filmov
tv
R Tutorial : Matrix-Vector Operations

Показать описание
---
Data often needs to be transformed into new data for a number of reasons. In this lesson, we'll discuss how matrices can be used to turn vectors into other vectors via multiplication.
A matrix with n columns and m rows can only be multiplied by a vector with n elements. The resulting vector then has m elements.
Here A, a 3 by 2 matrix, is being multiplied by a 2-dimensional vector to make a 3-dimensional vector.
The ith element of a matrix-vector multiplication is the element formed by component-wise multiplication of the ith row of the given matrix by the given vector, and summing the results as shown here.
Here the row 1, -1 is multiplied by the vector 1, 2 to make the element -1 of the resulting vector here.
Matrix-vector multiplication uses the * symbol sandwiched between two % symbols. The * symbol alone produces what is called component-wise multiplication (and was discussed in a previous exercise), which is useful in practice but is not the multiplication we want in this case.
Here we have the matrix A multiplied by the vector b makes the vector -1, 4, 0 on the left. In practice, we will read the multiplication left to right, and this will be the default. If we wish to multiply A by b on the right, making bA, we will say "multiply A by b on the right."
Here's an example of a 2 by 3 matrix multiplied by a vector with three elements, making a vector with two elements.
For the demonstration's sake, it's good to see what matrix multiplication of A by b is actually doing for these small examples.
Notice than each element of A multiplied by b is simply the product of a particular row of A (which is a vector) and b. Vector-vector multiplication in this way is called the dot product.
For example, the first element of A times b is the dot product of the first row of A, coded with A[1,] and b, making 7. Same for the second element of A times b.
An example of where matrix-vector multiplication is used is the ranking of entities like sports players or teams. Here's an example of a table of outcomes in college football.
This is a relatively small example, with just five teams, yielding a matrix with 25 elements. In the exercises, you'll deal with the 12-team league, which yields a matrix with 144 (or 12 times 12) elements.
Such a table can be put into a matrix that has the pairwise interactions between all of the teams. For example, in this situation team JH (for John's Hopkins) has played four games, one each against the other four teams, hence the 4 in the 1,1 element of the matrix and a -1 in each of the remaining elements of the row and column in which it resides.
rJH is the rating of John's Hopkins, which has outscored its opponents by 103 so far, the first element of the vector on the right-hand side.
We know that who a team plays matters every bit as much as how a team plays, so we want the vector with the r_JH, r_F&M, etc. on the left to be an alteration of the vector of score differentials on the right that reflects the strength of each team given how they played against the teams they played against. The alteration is executed by matrix multiplication by the matrix (called the Massey Matrix).
Now it's your turn.
#DataCamp #RTutorial #LinearAlgebraforDataScienceinR
Комментарии