简体   繁体   English

使用矩阵乘法与np.matrix数组和dot()/ tensor()与np.arrays有什么区别?

[英]What is the difference between using matrix multiplication with np.matrix arrays, and dot()/tensor() with np.arrays?

At the moment, my code is written entirely using numpy arrays, np.array . 目前,我的代码完全使用numpy数组编写, np.array

Define m as a np.array of 100 values, m.shape = (100,) . m定义为100个值的m.shape = (100,)m.shape = (100,) There is also a multi-dimensional array, C.shape = (100,100) . 还有一个多维数组, 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) . 其中m^T应为形状(1,100)m为形状(100,1)C为形状(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. 如果我坚持数据类型必须保留np.arrays ,那么我应该numpy.dot()numpy.tensordot()并指定轴。 That would be something like 那会是这样的

import numpy as np
result = np.dot(C, m)
final = np.dot(m.T, result)

though mT is an array of the same shape as m . 虽然mT是一个与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. 否则,我应该将所有内容转换为np.matrix并继续使用矩阵乘法。 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 . 这个问题是我必须将我的所有np.arrays转换为np.matrix ,执行操作,然后转换回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. 基于到目前为止的答案,我认为np.dot(m^T, np.dot(C, m))可能是最好的前进方式。

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 . 对于数组,您需要使用dot See: http://wiki.scipy.org/NumPy_for_Matlab_Users What are the differences between numpy arrays and matrices? 请参阅: http//wiki.scipy.org/NumPy_for_Matlab_Users numpy数组和矩阵之间有什么区别? 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: 如果m是一维数组, 则不需要转置任何内容 ,因为对于1D数组,转置不会改变任何内容:

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 : 如果您向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]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM