numpy.dot(vector_a, vector_b, out = None)
返回向量a和b的點積。它可以處理2D數組,但將其視為矩陣,並將執行矩陣乘法。對於N維,它是a的最後一個軸與b的second-to-last的總和:
dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
參數-
- vector_a : [數組]如果a是複數,則將其複共軛用於點積的計算。
- vector_b : [數組]如果b是複數,則將其複共軛用於點積的計算。
- out : [array,optional]輸出參數必須是C-contiguous,並且其dtype必須是為dot(a,b)返回的dtype。
返回-
向量a和b的點積。如果vector_a和vector_b為一維,則返回標量
代碼1-
# Python Program illustrating
# numpy.dot() method
import numpy as geek
# Scalars
product = geek.dot(5, 4)
print("Dot Product of scalar values : ", product)
# 1D array
vector_a = 2 + 3j
vector_b = 4 + 5j
product = geek.dot(vector_a, vector_b)
print("Dot Product : ", product)
輸出-
Dot Product of scalar values : 20 Dot Product : (-7+22j)
Code1如何工作?
vector_a = 2 + 3j
vector_b = 4 + 5j
現在點產品
= 2(4 + 5j)+ 3j(4-5j)
= 8 + 10j + 12j-15
= -7 + 22j
代碼2-
# Python Program illustrating
# numpy.dot() method
import numpy as geek
# 1D array
vector_a = geek.array([[1, 4], [5, 6]])
vector_b = geek.array([[2, 4], [5, 2]])
product = geek.dot(vector_a, vector_b)
print("Dot Product : \n", product)
product = geek.dot(vector_b, vector_a)
print("\nDot Product : \n", product)
"""
Code 2 : as normal matrix multiplication
"""
輸出-
Dot Product : [[22 12] [40 32]] Dot Product : [[22 32] [15 32]]
相關用法
注:本文由純淨天空篩選整理自 numpy.dot() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。