简体   繁体   English

如何找到最接近给定值的总和的第一次出现

[英]How to find the first occurrance of sum of closest value to the given value

Have got an array like below with columns ['item','Space','rem_spc']有一个像下面这样的数组,其中包含列['item','Space','rem_spc']

array([['Pineapple', 0.5, 0.5],
       ['Mango', 0.75, 0.25],
       ['Apple', 0.375, 0.625],
       ['Melons', 0.25, 0.75],
       ['Grape', 0.125, 0.875]], dtype=object)

need to convert this array to dataframe along with new column ['nxt_item'] which should be generated for first array row alone(Here, for Pineapple) with below conditions:需要将此数组转换为 dataframe 以及新列['nxt_item'] ,该列应单独为第一个数组行生成(此处,对于菠萝),条件如下:

  1. to find the first nearest items array['Space'] whose sum equals array['rem_spc'] for pineapple.找到第一个最近的项目array['Space']其总和等于array['rem_spc']的菠萝。

Expected Output:预计 Output:

item        Space   rem_spc     nxt_item
Pineapple   0.5     0.5         {Apple, Grape}      #0.5 = 0.375 + 0.125
Mango       0.75    0.25
Apple       0.375   0.625
Melons      0.25    0.75
Grape       0.125   0.875

Thanks!谢谢!

A possible solution (another would be using binary linear programming):一种可能的解决方案(另一种是使用二进制线性规划):

from itertools import product

n = len(df) - 1
combinations = product([0, 1], repeat=n)
a = np.array(list(combinations))
df['nxt_item'] = np.nan
df.loc[0, 'nxt_item'] = (
    '{' + 
    ', '.join(list(
        df.loc[[False] + 
               a[np.argmin(np.abs(df.iloc[0, 2] - 
                                  np.sum(a * df['Space'][1:].values, axis=1))), :]
               .astype(bool).tolist(), 'item']))
    + '}')

Output: Output:

        item  Space rem_spc        nxt_item
0  Pineapple    0.5     0.5  {Apple, Grape}
1      Mango   0.75    0.25             NaN
2      Apple  0.375   0.625             NaN
3     Melons   0.25    0.75             NaN
4      Grape  0.125   0.875             NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM