英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论