InterviewSolution
| 1. |
What Is The Difference Between Matrices And Arrays? |
|
Answer» NumPy’s basic data type is the MULTIDIMENSIONAL array. These can be one-dimensional (that is, one INDEX, like a LIST or a vector), two-dimensional (two indices, like an image), three-dimensional, or more (zero-dimensional arrays exist and are a slightly strange corner case). They support various operations, including addition, subtraction, multiplication, exponentiation, and so on - but all of these are elementwise operations. If you want matrix multiplication between two two-dimensional arrays, the function numpy.dot() does this. It also works fine for getting the matrix product of a two-dimensional array and a one-dimensional array, in either direction, or two one-dimensional arrays. If you want some kind of matrix multiplication-like operation on higher-dimensional arrays (tensor contraction), you need to think which indices you want to be contracting over. Some combination of tensordot() and rollaxis() should do what you want. However, some users find that they are doing so many matrix multiplications that always having to write dot as a prefix is too cumbersome, or they really want to KEEP row and column vectors separate. For these users, there is a matrix class. This is simply a transparent wrapper around arrays that forces arrays to be at least two-dimensional, and that overloads the multiplication and exponentiation operations. Multiplication becomes matrix multiplication, and exponentiation becomes matrix exponentiation. If you want elementwise multiplication, use numpy.multiply(). The function asmatrix() converts an array into a matrix (without ever copying any data); asarray() converts matrices to arrays.asanyarray() makes sure that the result is either a matrix or an array (but not, say, a list). Unfortunately, a few of NumPy’s many functions use asarray() when they should use asanyarray(), so from time to time you may find your matrices accidentally get converted into arrays. Just use asmatrix() on the output of these operations, and consider filing a bug. NumPy’s basic data type is the multidimensional array. These can be one-dimensional (that is, one index, like a list or a vector), two-dimensional (two indices, like an image), three-dimensional, or more (zero-dimensional arrays exist and are a slightly strange corner case). They support various operations, including addition, subtraction, multiplication, exponentiation, and so on - but all of these are elementwise operations. If you want matrix multiplication between two two-dimensional arrays, the function numpy.dot() does this. It also works fine for getting the matrix product of a two-dimensional array and a one-dimensional array, in either direction, or two one-dimensional arrays. If you want some kind of matrix multiplication-like operation on higher-dimensional arrays (tensor contraction), you need to think which indices you want to be contracting over. Some combination of tensordot() and rollaxis() should do what you want. However, some users find that they are doing so many matrix multiplications that always having to write dot as a prefix is too cumbersome, or they really want to keep row and column vectors separate. For these users, there is a matrix class. This is simply a transparent wrapper around arrays that forces arrays to be at least two-dimensional, and that overloads the multiplication and exponentiation operations. Multiplication becomes matrix multiplication, and exponentiation becomes matrix exponentiation. If you want elementwise multiplication, use numpy.multiply(). The function asmatrix() converts an array into a matrix (without ever copying any data); asarray() converts matrices to arrays.asanyarray() makes sure that the result is either a matrix or an array (but not, say, a list). Unfortunately, a few of NumPy’s many functions use asarray() when they should use asanyarray(), so from time to time you may find your matrices accidentally get converted into arrays. Just use asmatrix() on the output of these operations, and consider filing a bug. |
|