numpy.diff
api: https://docs.scipy.org/doc/numpy-1.9.0/reference/generated/numpy.diff.html
差分相减,输出是out[n] = a[n+1] - a[n]
Parameters | describe |
---|---|
a : 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 | describe |
---|---|
diff : ndarray | The n order differences. The shape of the output is the same as a except along axis where the dimension is smaller by n. |
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])
这里,np.diff(x,n=1)就是把x里面的元素后面一位减去前面一位,n=2的话就是迭代两次,在np.diff(x,n=1)得到的结果上在做一次后面减去前面一位的操作。
>>> 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]])
axis = 1 的时候是同行列相减,axis = 0时是同列行相减。
numpy.sign
api: https://docs.scipy.org/doc/numpy/reference/generated/numpy.sign.html
对数据判断是大于小于等于0
The sign function returns -1 if x < 0, 0 if x==0, 1 if x > 0 . nan is returned for nan inputs.
For complex inputs, the sign function returns sign(x.real) + 0j if x.real != 0 else sign(x.imag) + 0j .
Parameters | describe |
---|---|
x : array_like | Input values. |
out : ndarray, None, or tuple of ndarray and None, optional | A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs. |
where : array_like, optional | Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone. |
**kwargs | For other keyword-only arguments, see the ufunc docs. |
Returns | describe |
---|---|
y : ndarray | The sign of x. This is a scalar if x is a scalar. |
>>> np.sign([-5., 4.5])
array([-1., 1.])
>>> np.sign(0)
0
>>> np.sign(5-2j)
(1+0j)
numpy.argwhere
api: https://docs.scipy.org/doc/numpy/reference/generated/numpy.argwhere.html
根据关系式返回符号关系式的所有元素的索引位置
Parameters | describe |
---|---|
a : array_like | Input data. |
Returns | describe |
---|---|
index_array : ndarray | Indices of elements that are non-zero. Indices are grouped by element. |
Notes:np.argwhere(a) is the same as np.transpose(np.nonzero(a)).
The output of argwhere is not suitable for indexing arrays. For this purpose use nonzero(a) instead.
>>> x = np.arange(6).reshape(2,3)
>>> x
array([[0, 1, 2],
[3, 4, 5]])
>>> np.argwhere(x>1)
array([[0, 2],
[1, 0],
[1, 1],
[1, 2]])
Indexing
api: https://numpy.org/doc/stable/reference/arrays.indexing.html
基本切片和索引
基本操作与python对list,set与pandas的操作相似(由start:stop:step括号内的表示法构造)
警告,这里的切片数组实际上是指针,指向原数据中,而不是直接copy生成新的数组。所有对数组的切片建议使用copy()来强制生成新的数组。
>>> x = np.array([[[1],[2],[3]], [[4],[5],[6]]])
>>> x.shape
(2, 3, 1)
>>> x[1:2]
array([[[4],
[5],
[6]]])
Ellipsis
对数组进行扩展
>>> y = x[...,0]
>>> y
array([[1, 2, 3],
[4, 5, 6]])
>>> y.shape
(2, 3)
newaxis
选择元组中的每个对象用于将结果选择的尺寸扩展一个单位长度尺寸。添加的维度是newaxis 对象在选择元组中的位置。
>>> x[:,np.newaxis,:,:].shape
(2, 1, 3, 1)
>>> y[:,np.newaxis,:]
array([[[1, 2, 3]],
[[4, 5, 6]]])
>>> y[:,:,np.newaxis]
array([[[1],
[2],
[3]],
[[4],
[5],
[6]]])
警告,对于高级索引,以上内容不适用。
numpy.hstack
api: https://numpy.org/doc/stable/reference/generated/numpy.hstack.html
水平(按列)顺序堆叠数组。
这等效于沿第二个轴的串联,除了一维数组沿第一个轴的串联。重建除以的数组hsplit。
>>> a = np.array((1,2,3))
>>> b = np.array((2,3,4))
>>> np.hstack((a,b))
array([1, 2, 3, 2, 3, 4])
>>> a = np.array([[1],[2],[3]])
>>> b = np.array([[2],[3],[4]])
>>> np.hstack((a,b))
array([[1, 2],
[2, 3],
[3, 4]])