排序
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
y = np.sort(x)
print(y)
y = np.sort(x, axis=0)
print(y)
y = np.sort(x, axis=1)
print(y)
[[2.32 7.54 9.78 1.73 6.22]
[6.93 5.17 9.28 9.76 8.25]
[0.01 4.23 0.19 1.73 9.27]
[7.99 4.97 0.88 7.32 4.29]
[9.05 0.07 8.95 7.9 6.99]]
[[1.73 2.32 6.22 7.54 9.78]
[5.17 6.93 8.25 9.28 9.76]
[0.01 0.19 1.73 4.23 9.27]
[0.88 4.29 4.97 7.32 7.99]
[0.07 6.99 7.9 8.95 9.05]]
[[0.01 0.07 0.19 1.73 4.29]
[2.32 4.23 0.88 1.73 6.22]
[6.93 4.97 8.95 7.32 6.99]
[7.99 5.17 9.28 7.9 8.25]
[9.05 7.54 9.78 9.76 9.27]]
[[1.73 2.32 6.22 7.54 9.78]
[5.17 6.93 8.25 9.28 9.76]
[0.01 0.19 1.73 4.23 9.27]
[0.88 4.29 4.97 7.32 7.99]
[0.07 6.99 7.9 8.95 9.05]]
import numpy as np
dt = np.dtype([('name', 'S10'), ('age', np.int64)])
a = np.array([("Mike", 21), ("Nancy", 25), ("Bob", 17), ("Jane", 27)], dtype=dt)
b = np.sort(a, order='name')
print(b)
b = np.sort(a, order='age')
print(b)
[(b'Bob', 17) (b'Jane', 27) (b'Mike', 21) (b'Nancy', 25)]
[(b'Bob', 17) (b'Mike', 21) (b'Nancy', 25) (b'Jane', 27)]
import numpy as np
np.random.seed(20200612)
x = np.random.randint(0, 10, 10)
print(x)
y = np.argsort(x)
print(y)
print(x[y])
y = np.argsort(-x)
print(y)
print(x[y])
[6 1 8 5 5 4 1 2 9 1]
[1 6 9 7 5 3 4 0 2 8]
[1 1 1 2 4 5 5 6 8 9]
[8 2 0 3 4 5 7 1 6 9]
[9 8 6 5 5 4 2 1 1 1]
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
y = np.argsort(x)
print(y)
y = np.argsort(x, axis=0)
print(y)
y = np.argsort(x, axis=1)
print(y)
y = np.array([np.take(x[i], np.argsort(x[i])) for i in range(5)])
print(y)
[[2.32 7.54 9.78 1.73 6.22]
[6.93 5.17 9.28 9.76 8.25]
[0.01 4.23 0.19 1.73 9.27]
[7.99 4.97 0.88 7.32 4.29]
[9.05 0.07 8.95 7.9 6.99]]
[[3 0 4 1 2]
[1 0 4 2 3]
[0 2 3 1 4]
[2 4 1 3 0]
[1 4 3 2 0]]
[[2 4 2 0 3]
[0 2 3 2 0]
[1 3 4 3 4]
[3 1 1 4 1]
[4 0 0 1 2]]
[[3 0 4 1 2]
[1 0 4 2 3]
[0 2 3 1 4]
[2 4 1 3 0]
[1 4 3 2 0]]
[[1.73 2.32 6.22 7.54 9.78]
[5.17 6.93 8.25 9.28 9.76]
[0.01 0.19 1.73 4.23 9.27]
[0.88 4.29 4.97 7.32 7.99]
[0.07 6.99 7.9 8.95 9.05]]
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
index = np.lexsort([x[:, 0]])
print(index)
y = x[index]
print(y)
index = np.lexsort([-1 * x[:, 0]])
print(index)
y = x[index]
print(y)
[[2.32 7.54 9.78 1.73 6.22]
[6.93 5.17 9.28 9.76 8.25]
[0.01 4.23 0.19 1.73 9.27]
[7.99 4.97 0.88 7.32 4.29]
[9.05 0.07 8.95 7.9 6.99]]
[2 0 1 3 4]
[[0.01 4.23 0.19 1.73 9.27]
[2.32 7.54 9.78 1.73 6.22]
[6.93 5.17 9.28 9.76 8.25]
[7.99 4.97 0.88 7.32 4.29]
[9.05 0.07 8.95 7.9 6.99]]
[4 3 1 0 2]
[[9.05 0.07 8.95 7.9 6.99]
[7.99 4.97 0.88 7.32 4.29]
[6.93 5.17 9.28 9.76 8.25]
[2.32 7.54 9.78 1.73 6.22]
[0.01 4.23 0.19 1.73 9.27]]
x = np.array([1, 5, 1, 4, 3, 4, 4])
y = np.array([9, 4, 0, 4, 0, 2, 1])
a = np.lexsort([x])
b = np.lexsort([y])
print(a)
print(x[a])
print(b)
print(y[b])
z = np.lexsort([y, x])
print(z)
print(x[z])
z = np.lexsort([x, y])
print(z)
print(y[z])
[0 2 4 3 5 6 1]
[1 1 3 4 4 4 5]
[2 4 6 5 1 3 0]
[0 0 1 2 4 4 9]
[2 0 4 6 5 3 1]
[1 1 3 4 4 4 5]
[2 4 6 5 3 1 0]
[0 0 1 2 4 4 9]
import numpy as np
np.random.seed(100)
x = np.random.randint(1, 30, [8, 3])
print(x)
y = np.sort(x, axis=0)
print(y)
z = np.partition(x, kth=2, axis=0)
print(z)
[[ 9 25 4]
[ 8 24 16]
[17 11 21]
[ 3 22 3]
[ 3 15 3]
[18 17 25]
[16 5 12]
[29 27 17]]
[[ 3 5 3]
[ 3 11 3]
[ 8 15 4]
[ 9 17 12]
[16 22 16]
[17 24 17]
[18 25 21]
[29 27 25]]
[[ 3 5 3]
[ 3 11 3]
[ 8 15 4]
[ 9 22 21]
[17 24 16]
[18 17 25]
[16 25 12]
[29 27 17]]
import numpy as np
np.random.seed(100)
x = np.random.randint(1, 30, [8, 3])
print(x)
z = np.partition(x, kth=2, axis=0)
print(z)
print(z[2])
[[ 9 25 4]
[ 8 24 16]
[17 11 21]
[ 3 22 3]
[ 3 15 3]
[18 17 25]
[16 5 12]
[29 27 17]]
[[ 3 5 3]
[ 3 11 3]
[ 8 15 4]
[ 9 22 21]
[17 24 16]
[18 17 25]
[16 25 12]
[29 27 17]]
[ 8 15 4]
import numpy as np
np.random.seed(100)
x = np.random.randint(1, 30, [8, 3])
print(x)
z = np.partition(x, kth=-3, axis=0)
print(z)
print(z[-3])
[[ 9 25 4]
[ 8 24 16]
[17 11 21]
[ 3 22 3]
[ 3 15 3]
[18 17 25]
[16 5 12]
[29 27 17]]
[[ 8 5 3]
[ 3 11 3]
[ 3 15 4]
[ 9 17 12]
[16 22 16]
[17 24 17]
[18 25 21]
[29 27 25]]
[17 24 17]
import numpy as np
np.random.seed(100)
x = np.random.randint(1, 30, [8, 3])
print(x)
y = np.argsort(x, axis=0)
print(y)
z = np.argpartition(x, kth=2, axis=0)
print(z)
[[ 9 25 4]
[ 8 24 16]
[17 11 21]
[ 3 22 3]
[ 3 15 3]
[18 17 25]
[16 5 12]
[29 27 17]]
[[3 6 3]
[4 2 4]
[1 4 0]
[0 5 6]
[6 3 1]
[2 1 7]
[5 0 2]
[7 7 5]]
[[3 6 3]
[4 2 4]
[1 4 0]
[0 3 2]
[2 1 1]
[5 5 5]
[6 0 6]
[7 7 7]]
import numpy as np
np.random.seed(100)
x = np.random.randint(1, 30, [8, 3])
print(x)
z = np.argpartition(x, kth=2, axis=0)
print(z[2])
[[ 9 25 4]
[ 8 24 16]
[17 11 21]
[ 3 22 3]
[ 3 15 3]
[18 17 25]
[16 5 12]
[29 27 17]]
[1 4 0]
import numpy as np
np.random.seed(100)
x = np.random.randint(1, 30, [8, 3])
print(x)
z = np.argpartition(x, kth=-3, axis=0)
print(z[-3])
[[ 9 25 4]
[ 8 24 16]
[17 11 21]
[ 3 22 3]
[ 3 15 3]
[18 17 25]
[16 5 12]
[29 27 17]]
[2 1 7]
搜索
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
y = np.argmax(x)
print(y)
y = np.argmax(x, axis=0)
print(y)
y = np.argmax(x, axis=1)
print(y)
[[2.32 7.54 9.78 1.73 6.22]
[6.93 5.17 9.28 9.76 8.25]
[0.01 4.23 0.19 1.73 9.27]
[7.99 4.97 0.88 7.32 4.29]
[9.05 0.07 8.95 7.9 6.99]]
2
[4 0 0 1 2]
[2 3 4 0 0]
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
y = np.argmin(x)
print(y)
y = np.argmin(x, axis=0)
print(y)
y = np.argmin(x, axis=1)
print(y)
[[2.32 7.54 9.78 1.73 6.22]
[6.93 5.17 9.28 9.76 8.25]
[0.01 4.23 0.19 1.73 9.27]
[7.99 4.97 0.88 7.32 4.29]
[9.05 0.07 8.95 7.9 6.99]]
10
[2 4 2 0 3]
[3 1 0 2 1]
import numpy as np
x = np.array([0, 2, 3])
print(x)
print(x.shape)
print(x.ndim)
y = np.nonzero(x)
print(y)
print(np.array(y))
print(np.array(y).shape)
print(np.array(y).ndim)
print(np.transpose(y))
print(x[np.nonzero(x)])
[0 2 3]
(3,)
1
(array([1, 2]),)
[[1 2]]
(1, 2)
2
[[1]
[2]]
[2 3]
import numpy as np
x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
print(x)
print(x.shape)
print(x.ndim)
y = np.nonzero(x)
print(y)
print(np.array(y))
print(np.array(y).shape)
print(np.array(y).ndim)
y = x[np.nonzero(x)]
print(y)
y = np.transpose(np.nonzero(x))
print(y)
[[3 0 0]
[0 4 0]
[5 6 0]]
(3, 3)
2
(array([0, 1, 2, 2]), array([0, 1, 0, 1]))
[[0 1 2 2]
[0 1 0 1]]
(2, 4)
2
[3 4 5 6]
[[0 0]
[1 1]
[2 0]
[2 1]]
import numpy as np
x = np.array([[[0, 1], [1, 0]], [[0, 1], [1, 0]], [[0, 0], [1, 0]]])
print(x)
print(np.shape(x))
print(x.ndim)
y = np.nonzero(x)
print(np.array(y))
print(np.array(y).shape)
print(np.array(y).ndim)
print(y)
print(x[np.nonzero(x)])
[[[0 1]
[1 0]]
[[0 1]
[1 0]]
[[0 0]
[1 0]]]
(3, 2, 2)
3
[[0 0 1 1 2]
[0 1 0 1 1]
[1 0 1 0 0]]
(3, 5)
2
(array([0, 0, 1, 1, 2]), array([0, 1, 0, 1, 1]), array([1, 0, 1, 0, 0]))
[1 1 1 1 1]
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(x)
y = x > 3
print(y)
y = np.nonzero(x > 3)
print(y)
y = x[np.nonzero(x > 3)]
print(y)
y = x[x > 3]
print(y)
[[1 2 3]
[4 5 6]
[7 8 9]]
[[False False False]
[ True True True]
[ True True True]]
(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
[4 5 6 7 8 9]
[4 5 6 7 8 9]
import numpy as np
x = np.arange(10)
print(x)
y = np.where(x < 5, x, 10 * x)
print(y)
x = np.array([[0, 1, 2],
[0, 2, 4],
[0, 3, 6]])
y = np.where(x < 4, x, -1)
print(y)
[0 1 2 3 4 5 6 7 8 9]
[ 0 1 2 3 4 50 60 70 80 90]
[[ 0 1 2]
[ 0 2 -1]
[ 0 3 -1]]
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.where(x > 5)
print(y)
print(x[y])
y = np.nonzero(x > 5)
print(y)
print(x[y])
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.where(x > 25)
print(y)
print(x[y])
y = np.nonzero(x > 25)
print(y)
print(x[y])
(array([5, 6, 7]),)
[6 7 8]
(array([5, 6, 7]),)
[6 7 8]
(array([3, 3, 3, 3, 3, 4, 4, 4, 4, 4]), array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4]))
[26 27 28 29 30 31 32 33 34 35]
(array([3, 3, 3, 3, 3, 4, 4, 4, 4, 4]), array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4]))
[26 27 28 29 30 31 32 33 34 35]
import numpy as np
x = np.array([0, 1, 5, 9, 11, 18, 26, 33])
y = np.searchsorted(x, 15)
print(y)
y = np.searchsorted(x, 15, side='right')
print(y)
y = np.searchsorted(x, -1)
print(y)
y = np.searchsorted(x, -1, side='right')
print(y)
y = np.searchsorted(x, 35)
print(y)
y = np.searchsorted(x, 35, side='right')
print(y)
y = np.searchsorted(x, 11)
print(y)
y = np.searchsorted(x, 11, side='right')
print(y)
y = np.searchsorted(x, 0)
print(y)
y = np.searchsorted(x, 0, side='right')
print(y)
y = np.searchsorted(x, 33)
print(y)
y = np.searchsorted(x, 33, side='right')
print(y)
5
5
0
0
8
8
4
5
0
1
7
8
import numpy as np
x = np.array([0, 1, 5, 9, 11, 18, 26, 33])
y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35])
print(y)
y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35], side='right')
print(y)
[0 0 4 5 7 8]
[0 1 5 5 8 8]
import numpy as np
x = np.array([0, 1, 5, 9, 11, 18, 26, 33])
np.random.shuffle(x)
print(x)
x_sort = np.argsort(x)
print(x_sort)
y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35], sorter=x_sort)
print(y)
y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35], side='right', sorter=x_sort)
print(y)
[ 1 0 26 9 33 11 18 5]
[1 0 7 3 5 6 2 4]
[0 0 4 5 7 8]
[0 1 5 5 8 8]
计数
import numpy as np
x = np.count_nonzero(np.eye(4))
print(x)
x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]])
print(x)
x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=0)
print(x)
x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=1)
print(x)
4
5
[1 1 1 1 1]
[2 3]
集合操作
import numpy as np
x = np.unique([1, 1, 3, 2, 3, 3])
print(x)
x = sorted(set([1, 1, 3, 2, 3, 3]))
print(x)
x = np.array([[1, 1], [2, 3]])
u = np.unique(x)
print(u)
x = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])
y = np.unique(x, axis=0)
print(y)
x = np.array(['a', 'b', 'b', 'c', 'a'])
u, index = np.unique(x, return_index=True)
print(u)
print(index)
print(x[index])
x = np.array([1, 2, 6, 4, 2, 3, 2])
u, index = np.unique(x, return_inverse=True)
print(u)
print(index)
print(u[index])
u, count = np.unique(x, return_counts=True)
print(u)
print(count)
[1 2 3]
[1, 2, 3]
[1 2 3]
[[1 0 0]
[2 3 4]]
['a' 'b' 'c']
[0 1 3]
['a' 'b' 'c']
[1 2 3 4 6]
[0 1 4 3 1 2 1]
[1 2 6 4 2 3 2]
[1 2 3 4 6]
[1 3 1 1 1]
import numpy as np
test = np.array([0, 1, 2, 5, 0])
states = [0, 2]
mask = np.in1d(test, states)
print(mask)
print(test[mask])
mask = np.in1d(test, states, invert=True)
print(mask)
print(test[mask])
[ True False True False True]
[0 2 0]
[False True False True False]
[1 5]
import numpy as np
from functools import reduce
x = np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
print(x)
x = np.array([1, 1, 2, 3, 4])
y = np.array([2, 1, 4, 6])
xy, x_ind, y_ind = np.intersect1d(x, y, return_indices=True)
print(x_ind)
print(y_ind)
print(xy)
print(x[x_ind])
print(y[y_ind])
x = reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
print(x)
[1 3]
[0 2 4]
[1 0 2]
[1 2 4]
[1 2 4]
[1 2 4]
[3]
import numpy as np
from functools import reduce
x = np.union1d([-1, 0, 1], [-2, 0, 2])
print(x)
x = reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
print(x)
'''
functools.reduce(function, iterable[, initializer])
将两个参数的 function 从左至右积累地应用到 iterable 的条目,
以便将该可迭代对象缩减为单一的值。
例如,reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 是计算 ((((1+2)+3)+4)+5) 的值。
左边的参数 x 是积累值而右边的参数 y 则是来自 iterable 的更新值。
如果存在可选项 initializer,它会被放在参与计算的可迭代对象的条目之前,
并在可迭代对象为空时作为默认值。 如果没有给出 initializer 并且 iterable 仅包含一个条目,则将返回第一项。
大致相当于:
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
value = next(it)
else:
value = initializer
for element in it:
value = function(value, element)
return value
'''
[-2 -1 0 1 2]
[1 2 3 4 6]
'\nfunctools.reduce(function, iterable[, initializer])\n将两个参数的 function 从左至右积累地应用到 iterable 的条目,\n以便将该可迭代对象缩减为单一的值。 \n例如,reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 是计算 ((((1+2)+3)+4)+5) 的值。 \n左边的参数 x 是积累值而右边的参数 y 则是来自 iterable 的更新值。 \n如果存在可选项 initializer,它会被放在参与计算的可迭代对象的条目之前,\n并在可迭代对象为空时作为默认值。 如果没有给出 initializer 并且 iterable 仅包含一个条目,则将返回第一项。\n\n大致相当于:\ndef reduce(function, iterable, initializer=None):\n it = iter(iterable)\n if initializer is None:\n value = next(it)\n else:\n value = initializer\n for element in it:\n value = function(value, element)\n return value\n'
import numpy as np
a = np.array([1, 2, 3, 2, 4, 1])
b = np.array([3, 4, 5, 6])
x = np.setdiff1d(a, b)
print(x)
[1 2]
import numpy as np
a = np.array([1, 2, 3, 2, 4, 1])
b = np.array([3, 4, 5, 6])
x = np.setxor1d(a, b)
print(x)
[1 2 5 6]
练习
Z = np.random.randint(0,10,(3,3))
print (Z)
print (Z[Z[:,2].argsort()])
[[0 3 9]
[4 3 6]
[3 2 6]]
[[4 3 6]
[3 2 6]
[0 3 9]]
import numpy as np
x=np.array([[1,2,3,4],[1,4,3,3]])
print(x)
y=np.lexsort(x,axis=0)
display(y)
display(x[:,y])
[[1 2 3 4]
[1 4 3 3]]
array([0, 2, 3, 1])
array([[1, 3, 4, 2],
[1, 3, 3, 4]])
import numpy as np
np.random.seed(100)
x = np.random.randint(1, 30, [8, 3])
print(x)
z = np.argpartition(x, kth=2, axis=0)
y=np.array([[ x[z[i,j],j]for j in range(3)]for i in range(2)])
print(y)
np.partition(x, kth=2, axis=0)[:2]
[[ 9 25 4]
[ 8 24 16]
[17 11 21]
[ 3 22 3]
[ 3 15 3]
[18 17 25]
[16 5 12]
[29 27 17]]
[[ 3 5 3]
[ 3 11 3]]
array([[ 3, 5, 3],
[ 3, 11, 3]])
import numpy as np
arr = np.arange(10)
index = np.where(arr % 2 == 1)
print(arr[index])
x = arr[arr % 2 == 1]
print(x)
[1 3 5 7 9]
[1 3 5 7 9]
import numpy as np
arr = np.arange(10)
index = np.where(arr % 2 == 0)
arr[index] = 0
print(arr)
[0 1 0 3 0 5 0 7 0 9]
import numpy as np
arr = np.arange(10)
x = np.where(arr % 2 == 0, 0, arr)
print(x)
print(arr)
x = np.copy(arr)
x[x % 2 == 0] = 0
print(x)
print(arr)
[0 1 0 3 0 5 0 7 0 9]
[0 1 2 3 4 5 6 7 8 9]
[0 1 0 3 0 5 0 7 0 9]
[0 1 2 3 4 5 6 7 8 9]
import numpy as np
np.random.seed(100)
a = np.random.uniform(1, 50, 20)
print(a)
b = np.argsort(a)
print(b)
print(b[-5:])
b = np.sort(a)
b = np.where(a >= b[-5])
print(b)
b = np.argpartition(a, kth=-5)
print(b[-5:])
[27.62684215 14.64009987 21.80136195 42.39403048 1.23122395 6.95688692
33.86670515 41.466785 7.69862289 29.17957314 44.67477576 11.25090398
10.08108276 6.31046763 11.76517714 48.95256545 40.77247431 9.42510962
40.99501269 14.42961361]
[ 4 13 5 8 17 12 11 14 19 1 2 0 9 6 16 18 7 3 10 15]
[18 7 3 10 15]
(array([ 3, 7, 10, 15, 18]),)
[18 7 3 10 15]
import numpy as np
a = np.array([1, 2, 3, np.nan, 5, 6, 7, np.nan])
b = np.isnan(a)
c = np.where(np.logical_not(b))
print(a[c])
[1. 2. 3. 5. 6. 7.]
a=np.random.uniform(1, 50, 10)
print(a)
count = 0
for i in a[np.where(a > 7)]:
count+=1
print(count)
[22.153505 47.06146116 41.06481956 17.46948556 9.59511223 19.26877027
1.27873686 13.36889132 39.98746292 1.74749359]
8
import numpy as np
a = np.array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])
b = np.array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])
x = np.intersect1d(a, b)
print(x)
[2 4]
import numpy as np
a = np.array([1, 2, 3, 4, 5])
b = np.array([5, 6, 7, 8, 9])
x = np.setdiff1d(a, b)
print(x)
[1 2 3 4]