简体   繁体   English

Python矩阵内部积

[英]Python matrix inner product

I am trying to solve the below question: 我正在尝试解决以下问题:

'''
        Take in two matrices as numpy arrays, X and Y. Determine whether they have an inner product.
        If they do not, return False. If they do, return the resultant matrix as a numpy array.
        '''

with the following code: 使用以下代码:

def mat_inner_product(X,Y):

    if X.shape != Y.shape:
        return False
    else:
        return np.inner(X,Y)

I got the following error message: 我收到以下错误消息:

.F
======================================================================
FAIL: test_mat_inner_product (test_methods.TestPython1)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/src/app/test_methods.py", line 27, in test_mat_inner_product
    self.assertTrue(np.array_equal(result2, correct2))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=1)

What does it mean "False is not true"? 这是什么意思“假不是真的”? Do I have a logic error? 我有逻辑错误吗? Or should I use .dot() rather than .inner()? 还是应该使用.dot()而不是.inner()? What is the difference? 有什么区别?

One can calculate the inner product given that the last dimension of both matrices are the same . 只要两个矩阵的最后一个维相同 ,就可以计算出内积 So you should not check whether X.shape is equal to Y.shape , but only the last dimension: 因此,您不应检查X.shape是否等于Y.shape ,而只能检查最后一个尺寸:

def mat_inner_product(X,Y):
    if X.shape[-1] != Y.shape[-1]:
        return False
    else:
        return np.inner(X,Y)

Furthermore the number of dimensions - the .ndim (which is the len(X.shape) - do not have to be the same either: you can calculate the inner product of a 2d matrix with a 3d tensor. 此外,维数.ndim (即len(X.shape)也不必相同:您可以计算具有3d张量的2d矩阵的内积。

You can however omit the check and use a try - except item: 不过,您可以省略支票并try -下列项目except

def mat_inner_product(X,Y):
    try:
        return np.inner(X,Y)
    except ValueError:
        return False

Now we only have to rely on the fact that numpy has implemented the logic of the inner matrix correctly, and will raise a ValueError in case the inner product cannot be calculated. 现在,我们仅需依靠numpy正确实现了内部矩阵的逻辑这一事实,并且在无法计算内部乘积的情况下会引发ValueError

Or should I use .dot() rather than .inner() ? 还是应该使用.dot()而不是.inner() What is the difference? 有什么区别?

The difference with the dot product is that it works with the second last dimension of Y (instead of the last that is used in np.inner() ). 点积的不同之处在于它使用Y的倒数第二个维度(而不是np.inner()使用的最后一个)。 So in case you would work with numpy.dot(..) the check would be: 因此,如果您将使用numpy.dot(..)则检查应为:

def mat_dot_product(X,Y):
    if X.shape[-1] != Y.shape[-2]:
        return False
    else:
        return np.dot(X,Y)

But again, you can make use of a try - except structure here. 但是同样,您可以try - except这里的结构。

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

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