numpy np.matmul()(两个数组的矩阵乘积)
生活随笔
收集整理的這篇文章主要介紹了
numpy np.matmul()(两个数组的矩阵乘积)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
from multiarray
def matmul(a, b, out=None): # real signature unknown; restored from __doc__"""matmul(a, b, out=None)Matrix product of two arrays.兩個數組的矩陣乘積。The behavior depends on the arguments in the following way.行為取決于以下列方式呈現的參數。- If both arguments are 2-D they are multiplied like conventionalmatrices.如果兩個參數都是二維的,它們將像常規矩陣一樣相乘。- If either argument is N-D, N > 2, it is treated as a stack ofmatrices residing in the last two indexes and broadcast accordingly.如果任一自變量為N維,N> 2,則將其視為位于最后兩個索引中的矩陣堆棧,并進行相應廣播。- If the first argument is 1-D, it is promoted to a matrix byprepending a 1 to its dimensions. After matrix multiplicationthe prepended 1 is removed.如果第一個參數是1維,則通過在其尺寸前面加1來將其提升為矩陣。 矩陣相乘后,除去前面的1。- If the second argument is 1-D, it is promoted to a matrix byappending a 1 to its dimensions. After matrix multiplicationthe appended 1 is removed.如果第二個參數是1-D,則通過在其維數后附加1來將其提升為矩陣。 矩陣相乘后,將附加的1刪除。Multiplication by a scalar is not allowed, use ``*`` instead. Note thatmultiplying a stack of matrices with a vector will result in a stack ofvectors, but matmul will not recognize it as such.不允許與標量相乘,請改用``*''。 請注意,將一堆矩陣與一個向量相乘會得到一堆向量,但是matmul不會這樣識別。``matmul`` differs from ``dot`` in two important ways.``matmul''與``dot''在兩個重要方面不同。- Multiplication by scalars is not allowed.- Stacks of matrices are broadcast together as if the matriceswere elements.標量不能相乘。將矩陣堆棧一起廣播,就好像矩陣是元素一樣。.. warning::This function is preliminary and included in NumPy 1.10.0 for testingand documentation. Its semantics will not change, but the number andorder of the optional arguments will.此功能是初步功能,已包含在NumPy 1.10.0中以進行測試和記錄。 它的語義不會改變,但是可選參數的數量和順序會改變。.. versionadded:: 1.10.0Parameters----------a : array_likeFirst argument.b : array_likeSecond argument.out : ndarray, optionalOutput argument. This must have the exact kind that would be returnedif it was not used. In particular, it must have the right type, must beC-contiguous, and its dtype must be the dtype that would be returnedfor `dot(a,b)`. This is a performance feature. Therefore, if theseconditions are not met, an exception is raised, instead of attemptingto be flexible.輸出參數。 如果沒有使用,它必須具有返回的確切類型。 特別是,它必須具有正確的類型,必須是C連續的,并且它的dtype必須是為dot(a,b)返回的dtype。 這是一項性能功能。 因此,如果不滿足這些條件,則會引發異常,而不是嘗試變得靈活。Returns-------output : ndarrayReturns the dot product of `a` and `b`. If `a` and `b` are both1-D arrays then a scalar is returned; otherwise an array isreturned. If `out` is given, then it is returned.返回“ a”和“ b”的點積。 如果a和b都是一維數組,則返回標量; 否則返回一個數組。 如果給出`out`,則返回它。Raises------ValueErrorIf the last dimension of `a` is not the same size asthe second-to-last dimension of `b`.如果a的最后一個尺寸與b的倒數第二個尺寸不同。If scalar value is passed.See Also--------vdot : Complex-conjugating dot product.tensordot : Sum products over arbitrary axes.einsum : Einstein summation convention.dot : alternative matrix product with different broadcasting rules.Notes-----The matmul function implements the semantics of the `@` operator introducedin Python 3.5 following PEP465.Examples--------For 2-D arrays it is the matrix product:>>> a = [[1, 0], [0, 1]]>>> b = [[4, 1], [2, 2]]>>> np.matmul(a, b)array([[4, 1],[2, 2]])For 2-D mixed with 1-D, the result is the usual.>>> a = [[1, 0], [0, 1]]>>> b = [1, 2]>>> np.matmul(a, b)array([1, 2])>>> np.matmul(b, a)array([1, 2])Broadcasting is conventional for stacks of arrays>>> a = np.arange(2*2*4).reshape((2,2,4))>>> b = np.arange(2*2*4).reshape((2,4,2))>>> np.matmul(a,b).shape(2, 2, 2)>>> np.matmul(a,b)[0,1,1]98>>> sum(a[0,1,:] * b[0,:,1])98Vector, vector returns the scalar inner product, but neither argumentis complex-conjugated:>>> np.matmul([2j, 3j], [2j, 3j])(-13+0j)Scalar multiplication raises an error.>>> np.matmul([1,2], 3)Traceback (most recent call last):...ValueError: Scalar operands are not allowed, use '*' instead"""pass總結
以上是生活随笔為你收集整理的numpy np.matmul()(两个数组的矩阵乘积)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: numpy dot()函数(两个数组的点
- 下一篇: python socket.socket