【NumPy】 条件选取数组中的值(ndarray[ndarray>x]、np.maximum、np.maximum、np.clip)

____tz_zs

ndarray[ndarray>x]

得到布尔值的数组,再使用布尔值选取原数组中的数值。

#!/usr/bin/python2.7
# -*- coding:utf-8 -*-
 
"""
@author:    tz_zs
"""
import numpy as np
 
n = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [1, 3, 5, 7, 9]])
print(n)
"""
[[1 2 3 4 5]
 [2 3 4 5 6]
 [1 3 5 7 9]]
"""
# 比较所有的值,选择满足要求的值
r1 = n.copy()
print(r1 > 2)
"""
[[False False  True  True  True]
 [False  True  True  True  True]
 [False  True  True  True  True]]
"""
r1[r1 > 2] = 11
print(r1)
"""
[[ 1  2 11 11 11]
 [ 2 11 11 11 11]
 [ 1 11 11 11 11]]
"""
 
# 比较某一列中的值,选择满足要求的那一行
r2 = n.copy()
b = r2[:, 4] > 6
print(b)
"""
[False False  True]
"""
r2[b] = 11
print(r2)
"""
[[ 1  2  3  4  5]
 [ 2  3  4  5  6]
 [11 11 11 11 11]]
"""
 
# 比较某一行中的值,选择满足要求的那一列
r3 = n.copy()
b = r3[1, :] > 4
print(b)
"""
[False False False  True  True]
"""
r3[:, b] = 11
print(r3)
"""
[[ 1  2  3 11 11]
 [ 2  3  4 11 11]
 [ 1  3  5 11 11]]
"""

numpy.maximum、numpy.minimum

在元素级别比较两个数组,取小的数,返回一个新的数组。如果一个元素和nan比较,返回的是这个元素,如果相比较的两个元素都是nan,返回的是第一个nan。

numpy.maximum(x1, x2, /, out=None, *, where=True, casting=‘same_kind’, order=‘K’, dtype=None, subok=True[, signature, extobj])
numpy.minimum(x1, x2, /, out=None, *, where=True, casting=‘same_kind’, order=‘K’, dtype=None, subok=True[, signature, extobj])

  • x1, x2 : 相比较的两个数组。他们的shape应该一样,或者可以 broadcast 为同一个形状
#!/usr/bin/python2.7
# -*- coding:utf-8 -*-

"""
@author:    tz_zs
"""
import numpy as np

n = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [1, 3, 5, 7, 9]])
print(n)
"""
[[1 2 3 4 5]
 [2 3 4 5 6]
 [1 3 5 7 9]]
"""

r1 = np.minimum(n, 5)
r2 = np.minimum(n, [5, 5, 5, 5, 5])
r3 = np.minimum(n, [[5], [5], [5]])
print(r1)
print(r2)
print(r3)
"""
[[1 2 3 4 5]
 [2 3 4 5 5]
 [1 3 5 5 5]]
[[1 2 3 4 5]
 [2 3 4 5 5]
 [1 3 5 5 5]]
[[1 2 3 4 5]
 [2 3 4 5 5]
 [1 3 5 5 5]]
"""

  • out : 默认为 None,可输入 ndarray。如果不为 None,则函数会将比较后的结果赋值给此参数,所以 ndarray 的长度必须与 x1,x2 相同。
#!/usr/bin/python2.7
# -*- coding:utf-8 -*-

"""
@author:    tz_zs
"""
import numpy as np

n = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [1, 3, 5, 7, 9]])
print(n)
"""
[[1 2 3 4 5]
 [2 3 4 5 6]
 [1 3 5 7 9]]
"""

o1 = np.zeros((3, 5))
o2 = np.zeros((3, 5), dtype=int)
r1 = np.minimum(n, 5, out=o1)
r2 = np.minimum(n, 5, out=o2)
r3 = np.minimum(n, 5)
print(r1)
print(r2)
print(r3)
print("#" * 10)
print(o1)
print(o2)
"""
[[ 1.  2.  3.  4.  5.]
 [ 2.  3.  4.  5.  5.]
 [ 1.  3.  5.  5.  5.]]
[[1 2 3 4 5]
 [2 3 4 5 5]
 [1 3 5 5 5]]
[[1 2 3 4 5]
 [2 3 4 5 5]
 [1 3 5 5 5]]
##########
[[ 1.  2.  3.  4.  5.]
 [ 2.  3.  4.  5.  5.]
 [ 1.  3.  5.  5.  5.]]
[[1 2 3 4 5]
 [2 3 4 5 5]
 [1 3 5 5 5]]
"""

clip

修剪数组中的值
np.clip(a, a_min, a_max, out=None)

  • a: 需要修剪的数组
  • a_min,a_max : 标量 or 数组 or None。两个参数必须有一个不为 None,参数分别表示修剪的上下界,如果为 None 表示不设界限。
#!/usr/bin/python2.7
# -*- coding:utf-8 -*-

"""
@author:    tz_zs
"""
import numpy as np

n = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [1, 3, 5, 7, 9]])
print(n)
"""
[[1 2 3 4 5]
 [2 3 4 5 6]
 [1 3 5 7 9]]
"""
r1 = np.clip(n, 3, 7)
print(r1)
"""
[[3 3 3 4 5]
 [3 3 4 5 6]
 [3 3 5 7 7]]
"""

n = np.arange(10)
r2 = np.clip(n, 3, 7)
print(r2)
"""
[3 3 3 3 4 5 6 7 7 7]
"""

参考:

Replace all elements of Python NumPy Array that are greater than some value
end

你可能感兴趣的:(#,科学计算库)