1 numpy.insert
numpy.insert可以有三个参数(arr,obj,values),也可以有4个参数(arr,obj,values,axis):
第一个参数arr是一个数组,可以是一维的也可以是多维的,在arr的基础上 插入元素
第二个参数obj是元素插入的位置
第三个参数values是需要插入的数值
第四个参数axis是指示在哪一个轴上对应的插入位置进行插入
以下几点说明:
1)axis=0:从行方向插入
axis=1:从列方向插入
2)如果是多维数据,假如a=[N,C,H,W],N为行方向,C为列方向,每次只能插入一列或者一行,无法插入多行或者多列的数组
3)obj是元素插入位置,可以是多个位置插入,待插入值和values中的值对应
官方相关example
Examples
--------
>>> a = np.array([[1, 1], [2, 2], [3, 3]])
>>> a
array([[1, 1],
[2, 2],
[3, 3]])
>>> np.insert(a, 1, 5)
array([1, 5, 1, 2, 2, 3, 3])
>>> np.insert(a, 1, 5, axis=1)
array([[1, 5, 1],
[2, 5, 2],
[3, 5, 3]])
Difference between sequence and scalars:
>>> np.insert(a, [1], [[1],[2],[3]], axis=1)
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
>>> np.array_equal(np.insert(a, 1, [1, 2, 3], axis=1),
... np.insert(a, [1], [[1],[2],[3]], axis=1))
True
>>> b = a.flatten()
>>> b
array([1, 1, 2, 2, 3, 3])
>>> np.insert(b, [2, 2], [5, 6])
array([1, 1, 5, 6, 2, 2, 3, 3])
>>> np.insert(b, slice(2, 4), [5, 6])
array([1, 1, 5, 2, 6, 2, 3, 3])
>>> np.insert(b, [2, 2], [7.13, False]) # type casting
array([1, 1, 7, 0, 2, 2, 3, 3])
>>> x = np.arange(8).reshape(2, 4)
>>> idx = (1, 3)
>>> np.insert(x, idx, 999, axis=1)
array([[ 0, 999, 1, 2, 999, 3],
[ 4, 999, 5, 6, 999, 7]])
2 numpy.append
numpu.append(arr,values,axis=None)
功能:将values插入到目标arr的最后。
注意:When axis
is specified, values
must have the correct shape
Parameters
----------
arr : array_like
Values are appended to a copy of this array.
values : array_like
These values are appended to a copy of arr
. It must be of the
correct shape (the same shape as arr
, excluding axis
). If
axis
is not specified, values
can be any shape and will be
flattened before use.
axis : int, optional
The axis along which values
are appended. If axis
is not
given, both arr
and values
are flattened before use.
axis=0:从行方向插入
axis=1:从列方向插入
3 numpy.delete
delete(arr, obj, axis=None)
功能:返回一个删除指定轴后的新的数组
Parameters
----------
arr : array_like
Input array.
obj : slice, int or array of ints
Indicate which sub-arrays to remove.
axis : int, optional
The axis along which to delete the subarray defined by obj
.
If axis
is None, obj
is applied to the flattened array.
Examples
--------
>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> arr
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> np.delete(arr, 1, 0)
array([[ 1, 2, 3, 4],
[ 9, 10, 11, 12]])
>>> np.delete(arr, np.s_[::2], 1)
array([[ 2, 4],
[ 6, 8],
[10, 12]])
>>> np.delete(arr, [1,3,5], None)
array([ 1, 3, 5, 7, 8, 9, 10, 11, 12])
4.np.where
功能:Help on built-in function where in module numpy.core.multiarray
使用:np.where(condition, [x, y])
Parameters
----------
condition : array_like, bool
When True, yield x
, otherwise yield y
.
x, y : array_like, optional
Values from which to choose. x
, y
and condition
need to be
broadcastable to some shape.
Returns
-------
out : ndarray or tuple of ndarrays
If both `x` and `y` are specified, the output array contains
elements of `x` where `condition` is True, and elements from
`y` elsewhere.
If only `condition` is given, return the tuple
``condition.nonzero()``, the indices where `condition` is True.
Examples
--------
>>> np.where([[True, False], [True, True]],
... [[1, 2], [3, 4]],
... [[9, 8], [7, 6]])
array([[1, 8],
[3, 4]])
>>> np.where([[0, 1], [1, 0]])
(array([0, 1]), array([1, 0]))
>>> x = np.arange(9.).reshape(3, 3)
>>> np.where( x > 5 )
(array([2, 2, 2]), array([0, 1, 2]))
>>> x[np.where( x > 3.0 )] # Note: result is 1D.
array([ 4., 5., 6., 7., 8.])
>>> np.where(x < 5, x, -1) # Note: broadcasting.
array([[ 0., 1., 2.],
[ 3., 4., -1.],
[-1., -1., -1.]])
1) a = array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
np.where(a>6)
(array([4, 5, 6, 7, 8, 9], dtype=int64),)
return:返回坐标
2)
np.where(a>6,a,10*a)
array([ 0, 20, 40, 60, 8, 10, 12, 14, 16, 18])
return:返回a(a>6) + 10*a(a<=6)
5 np.prod
np.prod()函数用来计算所有元素的乘积,对于有多个维度的数组可以指定轴,如axis=1指定计算每一行的乘积
官方定义
prod(a, axis=None, dtype=None, out=None, keepdims=, initial=)
Return the product of array elements over a given axis.
Examples
--------
The product of an empty array is the neutral element 1:
>>> np.prod([])
1.0
>>> np.prod([[1.,2.],[3.,4.]])
24.0
>>> np.prod([[1.,2.],[3.,4.]], axis=1)
array([ 2., 12.])
>>> x = np.array([1, 2, 3], dtype=np.uint8)
>>> np.prod(x).dtype == np.uint
True
>>> np.prod([1, 2], initial=5)
10