Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

NumPy 乘积


乘积

要查找数组中元素的乘积,请使用 prod() 函数。

示例

查找此数组的元素乘积

import numpy as np

arr = np.array([1, 2, 3, 4])

x = np.prod(arr)

print(x)
自己试一试 »

**返回值:** 24,因为 1*2*3*4 = 24

示例

查找两个数组的元素乘积

import numpy as np

arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])

x = np.prod([arr1, arr2])

print(x)
自己试一试 »

**返回值:** 40320,因为 1*2*3*4*5*6*7*8 = 40320


沿轴的乘积

如果指定 axis=1,NumPy 将返回每个数组的乘积。

示例

在以下数组中沿第 1 个轴执行求和

import numpy as np

arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])

newarr = np.prod([arr1, arr2], axis=1)

print(newarr)
自己试一试 »

**返回值:** [24 1680]


累积乘积

累积乘积表示部分地进行乘积运算。

例如,[1, 2, 3, 4] 的部分乘积为 [1, 1*2, 1*2*3, 1*2*3*4] = [1, 2, 6, 24]

使用 cumprod() 函数执行部分求和。

示例

对以下数组的所有元素执行累积乘积

import numpy as np

arr = np.array([5, 6, 7, 8])

newarr = np.cumprod(arr)

print(newarr)
自己试一试 »

**返回值:** [5 30 210 1680]



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.