當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python numpy.inner()用法及代碼示例


numpy.inner(arr1, arr2)計算兩個數組的內積。

參數:
arr1, arr2: array to be evaluated.

返回: Inner product of the two arrays.

代碼1:

# Python Program illustrating  
# numpy.inner() method  
  
import numpy as geek  
  
# Scalars  
product = geek.inner(5, 4)  
print("inner Product of scalar values:", product)  
  
# 1D array  
vector_a = 2 + 3j
vector_b = 4 + 5j
  
product = geek.inner(vector_a, vector_b)  
print("inner Product:", product) 

輸出:


inner Product of scalar values: 20
inner Product: (-7+22j)


代碼2:作為普通矩陣乘法

# Python Program illustrating  
# numpy.inner() 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.inner(vector_a, vector_b)  
print("inner Product:\n", product)  
  
product = geek.inner(vector_b, vector_a)  
print("\ninner Product:\n", product) 

輸出:

inner Product:
 [[18 13]
 [34 37]]

inner Product:
 [[18 34]
 [13 37]]


相關用法


注:本文由純淨天空篩選整理自Mohit Gupta_OMG 大神的英文原創作品 numpy.inner() in python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。