Python Numpy np.digitize()方法
在np.digitize()方法的帮助下,我们可以通过使用np.digitize()方法得到每个值属于一个数组的bin的索引。
语法: np.digitize(Array, Bin, Right)
返回 : 返回一个索引的数组。
例子#1 :
在这个例子中,我们可以看到,通过使用np.digitize()方法,我们能够得到属于数组的每个值的bin的索引数组,通过使用这个方法。
# import numpy import numpy as np a = np.array([1.2, 2.4, 3.6, 4.8]) bins = np.array([1.0, 1.3, 2.5, 4.0, 10.0]) # using np.digitize() method gfg = np.digitize(a, bins) print(gfg)
输出 :
[1 2 3 4]
例子#2 :
# import numpy import numpy as np a = np.array([[10.2, 21.4, 3.6, 14.8], [1.0, 5.0, 10.0, 15.0]]) bins = np.array([1.0, 1.3, 2.5, 4.0, 10.0]) # using np.digitize() method gfg = np.digitize(a, bins) print(gfg)
输出 :
[[5 5 3 5]
[1 4 5 5]]
版权声明:本页面内容旨在传播知识,为用户自行发布,若有侵权等问题请及时与本网联系,我们将第一时间处理。E-mail:284563525@qq.com