Selisih [Difference]
Selisih diskrit berarti mengurangkan dua elemen array secara berurutan.
Misalnya: untuk array [1, 2, 3, 4], selisih diskritnya menjadi [2-1, 3-2, 4-3] = [1, 1, 1]
Untuk mencari selisih diskrit, gunakan fungsi diff[].
Contoh:
Hitung selisih diskrit dari array berikut
import numpy as np arr = np.array[[10, 15, 25, 5]] newarr = np.diff[arr] print[newarr]
Mengembalikan : [5 10 -20]
karena 15-10=5, 25-15=10, dan 5-25=-20
Operasi ini dapat dilakukan berulang kali dengan memberikan parameter n.
Misalnya: pada array [1, 2, 3, 4], selisih diskrit dimana n = 2 akan menjadi [2-1, 3-2, 4-3] = [1, 1, 1], karena n = 2, maka kita akan melakukan operasi ini sekali lagi, dengan hasil baru: [1-1, 1-1] = [0, 0]
Contoh:
Hitung selisih diskrit dari array berikut sebanyak dua kali
import numpy as np arr = np.array[[10, 15, 25, 5]] newarr = np.diff[arr, n=2] print[newarr]
Mengembalikan : [5 -30]
karena : 15-10=5, 25-15=10, dan 5-25=-20 AND 10-5=5 dan -20-10=-30
Selisih [Difference]
Selisih diskrit berarti mengurangkan dua elemen array secara berurutan.
Misalnya: untuk array [1, 2, 3, 4], selisih diskritnya menjadi [2-1, 3-2, 4-3] = [1, 1, 1]
Untuk mencari selisih diskrit, gunakan fungsi diff[].
Contoh:
Hitung selisih diskrit dari array berikut
import numpy as np arr = np.array[[10, 15, 25, 5]] newarr = np.diff[arr] print[newarr]
Mengembalikan : [5 10 -20]
karena 15-10=5, 25-15=10, dan 5-25=-20
Operasi ini dapat dilakukan berulang kali dengan memberikan parameter n.
Misalnya: pada array [1, 2, 3, 4], selisih diskrit dimana n = 2 akan menjadi [2-1, 3-2, 4-3] = [1, 1, 1], karena n = 2, maka kita akan melakukan operasi ini sekali lagi, dengan hasil baru: [1-1, 1-1] = [0, 0]
Contoh:
Hitung selisih diskrit dari array berikut sebanyak dua kali
import numpy as np arr = np.array[[10, 15, 25, 5]] newarr = np.diff[arr, n=2] print[newarr]
Mengembalikan : [5 -30]
karena : 15-10=5, 25-15=10, dan 5-25=-20 AND 10-5=5 dan -20-10=-30
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
numpy.diff[arr[, n[, axis]]] function is used when we calculate the n-th order discrete difference along the given axis. The first order difference is given by out[i] = arr[i+1] – arr[i] along the given axis. If we have to calculate higher differences, we are using diff recursively.
Syntax: numpy.diff[]
Parameters:
arr : [array_like] Input array.
n : [int, optional] The number of times values are differenced.
axis : [int, optional] The axis along which the difference is taken, default is the last axis.
Returns : [ndarray]The n-th discrete difference. The output is the same as a except along axis where the dimension is smaller by n.
Code #1 :
Python3
import
numpy as geek
arr
=
geek.array[[
1
,
3
,
4
,
7
,
9
]]
print
[
"Input array : "
, arr]
print
[
"First order difference : "
, geek.diff[arr]]
print
[
"Second order difference : "
, geek.diff[arr, n
=
2
]]
print
[
"Third order difference : "
, geek.diff[arr, n
=
3
]]
Output:
Input array : [1 3 4 7 9] First order difference : [2 1 3 2] Second order difference : [-1 2 -1] Third order difference : [ 3 -3]
Code #2 :
Python3
import
numpy as geek
arr
=
geek.array[[[
1
,
2
,
3
,
5
], [
4
,
6
,
7
,
9
]]]
print
[
"Input array : "
, arr]
print
[
"Difference when axis is 0 : "
, geek.diff[arr, axis
=
0
]]
print
[
"Difference when axis is 1 : "
, geek.diff[arr, axis
=
1
]]
Output:
Input array : [[1 2 3 5] [4 6 7 9]] Difference with axis 0 : [[3 4 4 4]] Difference with axis 1 : [[1 1 2] [2 1 2]]
Calculate the n-th discrete difference along the given axis.
The first difference is given by out[i] = a[i+1] - a[i]
along the given axis, higher differences
are calculated by using diff
recursively.
Input array
nint, optionalThe number of times values are differenced. If zero, the input is returned as-is.
axisint, optionalThe axis along which the difference is taken, default is the last axis.
prepend, appendarray_like, optionalValues to prepend or append to a along axis prior to performing the difference. Scalar values are expanded to arrays with length 1 in the direction of axis and the shape of the input array in along all other axes. Otherwise the dimension and shape must match a except along axis.
New in version 1.16.0.
Returns diffndarrayThe n-th differences. The shape of the output is the same as a except along axis where the dimension is smaller by n. The type of the output is the same as the type of the
difference between any two elements of a. This is the same as the type of a in most cases. A notable exception is datetime64
, which results in a timedelta64
output array.
Notes
Type is preserved for boolean arrays, so the result will contain False when consecutive elements are the same and True when they differ.
For unsigned integer arrays, the results will also be unsigned. This should not be surprising, as the result is consistent with calculating the difference directly:
>>> u8_arr = np.array[[1, 0], dtype=np.uint8] >>> np.diff[u8_arr] array[[255], dtype=uint8] >>> u8_arr[1,...] - u8_arr[0,...] 255
If this is not desirable, then the array should be cast to a larger integer type first:
>>> i16_arr = u8_arr.astype[np.int16] >>> np.diff[i16_arr] array[[-1], dtype=int16]
Examples
>>> x = np.array[[1, 2, 4, 7, 0]] >>> np.diff[x] array[[ 1, 2, 3, -7]] >>> np.diff[x, n=2] array[[ 1, 1, -10]]
>>> x = np.array[[[1, 3, 6, 10], [0, 5, 6, 8]]] >>> np.diff[x] array[[[2, 3, 4], [5, 1, 2]]] >>> np.diff[x, axis=0] array[[[-1, 2, 0, -2]]]
>>> x = np.arange['1066-10-13', '1066-10-16', dtype=np.datetime64] >>> np.diff[x] array[[1, 1], dtype='timedelta64[D]']