Numpy 笔记
numpy.ndarray.ndim
Number of array dimensions.
数组维度的个数
import numpy as np
x = np.array([1, 2, 3])
y = np.array([[1, 2, 3], [4, 5, 6]])
z = np.zeros((2, 3, 4))
>>> x.ndim
1
>>> y.ndim
2
>>> z.ndim
3
numpy.ndarray.shape
Tuple of array dimensions.
数组的维度,n行m列对应的shape
为(n, m),shape
的长度就是ndim
>>> x
array([1, 2, 3])
>>> x.shape
(3,)
>>> y
array([[1, 2, 3],
[4, 5, 6]])
>>> y.shape
(2, 3)
>>> z
array([[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]]])
>>> z.shape
(2, 3, 4)
numpy.tile
tile(A, reps)
Construct an array by repeating A the number of times given by reps.
If `reps` has length ``d``, the result will have dimension of
``max(d, A.ndim)``.
If ``A.ndim < d``, `A` is promoted to be d-dimensional by prepending new
axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication,
or shape (1, 1, 3) for 3-D replication. If this is not the desired
behavior, promote `A` to d-dimensions manually before calling this
function.
If ``A.ndim > d``, `reps` is promoted to `A`.ndim by pre-pending 1's to it.
Thus for an `A` of shape (2, 3, 4, 5), a `reps` of (2, 2) is treated as
(1, 1, 2, 2).
- 比较数组维度d和元组维度reps,如果d<reps,在需要时对数组补中括号 [] 来增加维度。
- 元组数字从右到左,数组维度从最深维度到最低维度。假设元组为(a,b,c,d,e,f),则数组最深维度重复f次,然后次深维度重复e次,接着次次深维度重复d次,再然后次次次深维度重复c次…… 以此类推,直到对最低维度重复a次,结束,得到结果。
参考python numpy-tile函数
a = np.array([0, 1, 2])
b = np.array([[1, 2], [3, 4]])
c = np.array([1, 2, 3, 4])
>>> a
array([0, 1, 2])
>>> np.tile(a, 2)
array([0, 1, 2, 0, 1, 2])
>>> np.tile(a, (2, 2))
array([[0, 1, 2, 0, 1, 2],
[0, 1, 2, 0, 1, 2]])
>>> np.tile(a, (2, 1, 2))
array([[[0, 1, 2, 0, 1, 2]],
[[0, 1, 2, 0, 1, 2]]])
>>> b
array([[1, 2],
[3, 4]])
>>> np.tile(b, 2)
array([[1, 2, 1, 2],
[3, 4, 3, 4]])
>>> np.tile(b, (2, 1))
array([[1, 2],
[3, 4],
[1, 2],
[3, 4]])
>>> c
array([1, 2, 3, 4])
>>> np.tile(c,(4,1))
array([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])
numpy.argsort
argsort(a, axis=-1, kind='quicksort', order=None)
Returns the indices that would sort an array.
Perform an indirect sort along the given axis using the algorithm specified
by the `kind` keyword. It returns an array of indices of the same shape as
`a` that index data along the given axis in sorted order.
Parameters
----------
a : array_like
Array to sort.
axis : int or None, optional
Axis along which to sort. The default is -1 (the last axis). If None,
the flattened array is used.
kind : {'quicksort', 'mergesort', 'heapsort'}, optional
Sorting algorithm.
order : str or list of str, optional
When `a` is an array with fields defined, this argument specifies
which fields to compare first, second, etc. A single field can
be specified as a string, and not all fields need be specified,
but unspecified fields will still be used, in the order in which
they come up in the dtype, to break ties.
返回数组的索引,升序排列。
a = np.array([3, 1, 2])
b = np.array([[0, 3], [2, 2]])
c = np.array([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')])
>>> a
array([3, 1, 2])
>>> np.argsort(a)
array([1, 2, 0])
>>> b
array([[0, 3],
[2, 2]])
>>> np.argsort(b, axis=0)
array([[0, 1],
[1, 0]])
>>> np.argsort(b, axis=1)
array([[0, 1],
[0, 1]])
>>> c
array([(1, 0), (0, 1)],
dtype=[('x', '<i4'), ('y', '<i4')])
>>> np.argsort(c, order=('x','y'))
array([1, 0])
>>> np.argsort(c, order=('y','x'))
array([0, 1])
降序排列
>>> a
array([3, 1, 2])
>>> np.argsort(-a)
array([0, 2, 1])