What is the difference between using matrix multiplication with np.matrix arrays, and dot()/tensor() with np.arrays?
Question:
At the moment, my code is written entirely using numpy arrays, np.array
.
Define m
as a np.array of 100 values, m.shape = (100,)
. There is also a multi-dimensional array, C.shape = (100,100)
.
The operation I would like to compute is
m^T * C * m
where m^T
should be of shape (1,100)
, m
of shape (100,1)
, and C
of shape (100,100)
.
I’m conflicted how to proceed. If I insist the data types must remain np.arrays
, then I should probably you numpy.dot()
or numpy.tensordot()
and specify the axis. That would be something like
import numpy as np
result = np.dot(C, m)
final = np.dot(m.T, result)
though m.T
is an array of the same shape as m
. Also, that’s doing two individual operations instead of one.
Otherwise, I should convert everything into np.matrix
and proceed to use matrix multiplication there. The problem with this is I must convert all my np.arrays
into np.matrix
, do the operations, and then convert back to np.array
.
What is the most efficient and intelligent thing to do?
EDIT:
Based on the answers so far, I think np.dot(m^T, np.dot(C, m))
is probably the best way forward.
Answers:
The main advantage of working with matrices is that the *
symbol performs a matrix multiplication, whereas it performs an element-wise multiplications with arrays. With arrays you need to use dot
. See:
Link
What are the differences between numpy arrays and matrices? Which one should I use?
If m
is a one dimensional array, you don’t need to transpose anything, because for 1D arrays, transpose doesn’t change anything:
In [28]: m.T.shape, m.shape
Out[28]: ((3,), (3,))
In [29]: m.dot(C)
Out[29]: array([15, 18, 21])
In [30]: C.dot(m)
Out[30]: array([ 5, 14, 23])
This is different if you add another dimension to m
:
In [31]: mm = m[:, np.newaxis]
In [32]: mm.dot(C)
--------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-32-28253c9b8898> in <module>()
----> 1 mm.dot(C)
ValueError: objects are not aligned
In [33]: (mm.T).dot(C)
Out[33]: array([[15, 18, 21]])
In [34]: C.dot(mm)
Out[34]:
array([[ 5],
[14],
[23]])
At the moment, my code is written entirely using numpy arrays, np.array
.
Define m
as a np.array of 100 values, m.shape = (100,)
. There is also a multi-dimensional array, C.shape = (100,100)
.
The operation I would like to compute is
m^T * C * m
where m^T
should be of shape (1,100)
, m
of shape (100,1)
, and C
of shape (100,100)
.
I’m conflicted how to proceed. If I insist the data types must remain np.arrays
, then I should probably you numpy.dot()
or numpy.tensordot()
and specify the axis. That would be something like
import numpy as np
result = np.dot(C, m)
final = np.dot(m.T, result)
though m.T
is an array of the same shape as m
. Also, that’s doing two individual operations instead of one.
Otherwise, I should convert everything into np.matrix
and proceed to use matrix multiplication there. The problem with this is I must convert all my np.arrays
into np.matrix
, do the operations, and then convert back to np.array
.
What is the most efficient and intelligent thing to do?
EDIT:
Based on the answers so far, I think np.dot(m^T, np.dot(C, m))
is probably the best way forward.
The main advantage of working with matrices is that the *
symbol performs a matrix multiplication, whereas it performs an element-wise multiplications with arrays. With arrays you need to use dot
. See:
Link
What are the differences between numpy arrays and matrices? Which one should I use?
If m
is a one dimensional array, you don’t need to transpose anything, because for 1D arrays, transpose doesn’t change anything:
In [28]: m.T.shape, m.shape
Out[28]: ((3,), (3,))
In [29]: m.dot(C)
Out[29]: array([15, 18, 21])
In [30]: C.dot(m)
Out[30]: array([ 5, 14, 23])
This is different if you add another dimension to m
:
In [31]: mm = m[:, np.newaxis]
In [32]: mm.dot(C)
--------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-32-28253c9b8898> in <module>()
----> 1 mm.dot(C)
ValueError: objects are not aligned
In [33]: (mm.T).dot(C)
Out[33]: array([[15, 18, 21]])
In [34]: C.dot(mm)
Out[34]:
array([[ 5],
[14],
[23]])