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 差值


差值

离散差值表示减去两个连续的元素。

例如,对于 [1, 2, 3, 4],离散差值将是 [2-1, 3-2, 4-3] = [1, 1, 1]

要找到离散差值,请使用 diff() 函数。

示例

计算以下数组的离散差值

import numpy as np

arr = np.array([10, 15, 25, 5])

newarr = np.diff(arr)

print(newarr)
自己试试 »

返回值: [5 10 -20] 因为 15-10=5,25-15=10,以及 5-25=-20

我们可以通过给出参数 n 来重复执行此操作。

例如,对于 [1, 2, 3, 4],n=2 的离散差值将是 [2-1, 3-2, 4-3] = [1, 1, 1],然后,由于 n=2,我们将再次执行此操作,使用新的结果:[1-1, 1-1] = [0, 0]

示例

计算以下数组的离散差值两次

import numpy as np

arr = np.array([10, 15, 25, 5])

newarr = np.diff(arr, n=2)

print(newarr)
自己试试 »

返回值: [5 -30] 因为:15-10=5,25-15=10,以及 5-25=-20 并且 10-5=5 以及 -20-10=-30



×

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.