创造一个类别为ndarray的矩阵,ndarray为numpy.array的数据类型。
-在list中索引不连续的元素import numpy as np a = [1,2,3,4,5,6,7,8] a_ndarray = np.array(a) b = a_ndarray[[0,2,5]]
(以上代码为引用千行百行)
-np.bincountimport numpy as np #获取平铺后每个索引位置值在原始数列中出现的次数 counts = np.bincount(nums) #返回众数 返回最大值在数列中的索引位置 np.argmax(counts) 即 [0,4,5,8,8] ——> bincount 返回 长度为8的列表 [1,0,0,0,1,1,0,0,2] 也就是把0-8平铺到列表里,然后对0-8的每个数字计数
(引用于jamiehmy)
-numpy.appendnp.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])
array([1, 2, 3, ..., 7, 8, 9])
np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0)
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0)
Traceback (most recent call last):
...
ValueError: all the input arrays must have same number of dimensions, but
the array at index 0 has 2 dimension(s) and the array at index 1 has 1
dimension(s)
-np.sum
np.sum([0.5, 1.5]) 2.0 np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32) 1 np.sum([[0, 1], [0, 5]]) 6 np.sum([[0, 1], [0, 5]], axis=0) array([0, 6]) np.sum([[0, 1], [0, 5]], axis=1) array([1, 5]) np.sum([[0, 1], [np.nan, 5]], where=[False, True], axis=1) array([1., 5.]-numpy.array_split
-numpy.argsort (有arg的输出基本都是indices)numpy.array_split(ary, indices_or_sections, axis=0) x = np.arange(8.0) np.array_split(x, 3) [array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7.])] x = np.arange(9) np.array_split(x, 4) [array([0, 1, 2]), array([3, 4]), array([5, 6]), array([7, 8])]
One dimensional array:
x = np.array([3, 1, 2])
np.argsort(x)
array([1, 2, 0])
Two-dimensional array:
x = np.array([[0, 3], [2, 2]])
x
array([[0, 3],
[2, 2]])
ind = np.argsort(x, axis=0) # sorts along first axis (down)
ind
array([[0, 1],
[1, 0]])
np.take_along_axis(x, ind, axis=0) # same as np.sort(x, axis=0)
array([[0, 2],
[2, 3]])
ind = np.argsort(x, axis=1) # sorts along last axis (across)
ind
array([[0, 1],
[0, 1]])
np.take_along_axis(x, ind, axis=1) # same as np.sort(x, axis=1)
array([[0, 3],
[2, 2]])
-numpy.argmax
a = np.arange(6).reshape(2,3) + 10
a
array([[10, 11, 12],
[13, 14, 15]])
np.argmax(a)
5
np.argmax(a, axis=0)
array([1, 1, 1])
np.argmax(a, axis=1)
array([2, 2])
Indexes of the maximal elements of a N-dimensional array:
ind = np.unravel_index(np.argmax(a, axis=None), a.shape)
ind
(1, 2)
a[ind]
15
b = np.arange(6)
b[1] = 5
b
array([0, 5, 2, 3, 4, 5])
np.argmax(b) # only the first occurrence is returned.
1
-np.delete()
- numpy.delete(arr, obj, axis=None)
- arr: Input array
- obj: Row or column number to delete
- axis: Axis to delete
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])
numpy.hstack
a = np.array((1,2,3))
b = np.array((4,5,6))
np.hstack((a,b))
array([1, 2, 3, 4, 5, 6])
a = np.array([[1],[2],[3]])
b = np.array([[4],[5],[6]])
np.hstack((a,b))
array([[1, 4],
[2, 5],
[3, 6]])
- arr: Input array
- obj: Row or column number to delete
- axis: Axis to delete
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])
a = np.array((1,2,3))
b = np.array((4,5,6))
np.hstack((a,b))
array([1, 2, 3, 4, 5, 6])
a = np.array([[1],[2],[3]])
b = np.array([[4],[5],[6]])
np.hstack((a,b))
array([[1, 4],
[2, 5],
[3, 6]])
(以上引用于numpy — NumPy v1.21 Manual)



