使用NumPy进行数字分箱

huangapple go评论60阅读模式
英文:

python binning the Numbers in numpy

问题

我想按以下方式对数组进行分箱:
[0.1, 0.2, 0.3, ... 1.0]。

我已经尝试过:

bins = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
Digitalized_lables_train = np.digitize(lables_train, bins) 

lables_train是0-9之间的数字。

但是在这个操作之后,我仍然得到1-10之间的分箱。

英文:

I want to bin an array in the following way:
[0.1, 0.2, 0.3, ... 1.0].

I already try:

bins = np.array([0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9])
Digitalized_lables_train = np.digitize(lables_train, bins ) 

lables_train are numbers from 0-9.

But after this operation i still get bins between 1-10.

答案1

得分: 1

np.digitize返回输入数组中每个值所属的箱的索引,您可以使用这些索引通过np.take收集箱的值:

bins = np.array([0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9])
lables_train = [0.783, 0.94, 0.23, 0.55, 0.785]  # 示例

ltrain_bin_values = np.take(bins, np.digitize(lables_train, bins) - 1)
print(ltrain_bin_values)

[0.7 0.9 0.2 0.5 0.7]
英文:

As np.digitize returns the indices of the bins to which each value in input array belongs you can then collect bin values by those indices with np.take:

bins = np.array([0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9])
lables_train = [0.783, 0.94, 0.23, 0.55, 0.785]  # sample

ltrain_bin_values = np.take(bins, np.digitize(lables_train, bins) - 1)
print(ltrain_bin_values)

[0.7 0.9 0.2 0.5 0.7]

答案2

得分: 0

得到了答案:

只需将我的 np.digitize(lables_train, bins ) 除以 10。

但是,对于需要将其他浮点数分箱的情况,我想知道还有哪些其他方法。

英文:

Got the answer:

just dividing my np.digitize(lables_train, bins ) with 10.

But for the case to bin in other float numbers i would like to know which other ways there could be.

huangapple
  • 本文由 发表于 2023年3月9日 16:02:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定