Python按比例移动权重

Python按比例移动权重,python,pandas,Python,Pandas,这是我的密码。假设我有: weights = [0.3,0.1,0.2,0.4] temp_list = [None,1,None,None] 我想要的是,如果该值为“无”,则将权重移动到最高权重。例如,temp_list的第一个值是None,因此我希望将权重移动到权重中的最高权重(在本例中为0.4),从而得到0.7 [0.0,0.1,0.2,0.7] <- First element is None; add to highest weight to get (0.3+0.4 = 0.

这是我的密码。假设我有:

weights = [0.3,0.1,0.2,0.4]
temp_list = [None,1,None,None]
我想要的是,如果该值为“无”,则将权重移动到最高权重。例如,temp_list的第一个值是None,因此我希望将权重移动到权重中的最高权重(在本例中为0.4),从而得到0.7

[0.0,0.1,0.2,0.7] <- First element is None; add to highest weight to get (0.3+0.4 = 0.7)
[0.0,0.1,0.2,0.7] <- Second element has value; no change
[0.0,0.1,0.0,0.9] <- Third element is None; add to highest weight to get 0.9
[0.0,1.0,0.0,0.0] <-- final output; therefore this weight is 1.0 for the second element since everywhere else is None.

我的问题是如何在列表中找到最高的权重值,并将当前的权重添加到其中?如果我试图寻找第二或第三高的重量数字呢?或者有更好的方法来解决这类问题吗?

我们可以通过纯python方法和基于numpy的方法来解决这个问题

在这两种方法中,我们都可以首先计算数组中max元素的索引。现在,我们可以遍历
temp_list
并向max元素添加权重。请注意,在达到max元素的索引之前,它将保持数组中的最大值(因为我们将向它添加更多权重)。一旦达到
max\u weights\u索引
,我们将相应的权重设置为0,并检查数组中的当前最大值。重复相同的程序

注意-我假设权重数组中没有重复项

纯Python

weights = [0.3,0.1,0.2,0.4]
temp_list = [None,1,None,None]

def get_max_index(weights):
    # Assuming no duplicates, find index with the max element
    max_weight = -1
    max_index = -1
    for i, weight in enumerate(weights):
        if weight > max_weight:
            max_weight = weight
            max_index = i
    return max_index

max_weight_index = get_max_index(weights)

for i, tmp_element in enumerate(temp_list):
    if tmp_element is None:
        if i == max_weight_index:
            current_max = weights[i]
            weights[i] = 0
            max_weight_index = get_max_index(weights)
            weights[max_weight_index] += current_max
        else:
            weights[max_weight_index] += weights[i]
            weights[i] = 0
    print(weights)
import numpy as np

weights = [0.3,0.1,0.2,0.4]
temp_list = [None,1,None,None]

weights = np.array(weights)
max_weight_index = np.argmax(weights)

for i, tmp_element in enumerate(temp_list):

    if tmp_element is None:
        if i == max_weight_index:
            current_max = weights[i]
            weights[i] = 0
            max_weight_index = np.argmax(weights)
            weights[max_weight_index] += current_max
        else:
            weights[max_weight_index] += weights[i]
            weights[i] = 0
    print(weights)
输出

[0, 0.1, 0.2, 0.7]
[0, 0.1, 0.2, 0.7]
[0, 0.1, 0, 0.8999999999999999]
[0, 0.9999999999999999, 0, 0]
[ 0.   0.1  0.2  0.7]
[ 0.   0.1  0.2  0.7]
[ 0.   0.1  0.   0.9]
[ 0.  1.  0.  0.]

有关python中浮点数精度的更多信息,请参阅

Numpy解决方案

weights = [0.3,0.1,0.2,0.4]
temp_list = [None,1,None,None]

def get_max_index(weights):
    # Assuming no duplicates, find index with the max element
    max_weight = -1
    max_index = -1
    for i, weight in enumerate(weights):
        if weight > max_weight:
            max_weight = weight
            max_index = i
    return max_index

max_weight_index = get_max_index(weights)

for i, tmp_element in enumerate(temp_list):
    if tmp_element is None:
        if i == max_weight_index:
            current_max = weights[i]
            weights[i] = 0
            max_weight_index = get_max_index(weights)
            weights[max_weight_index] += current_max
        else:
            weights[max_weight_index] += weights[i]
            weights[i] = 0
    print(weights)
import numpy as np

weights = [0.3,0.1,0.2,0.4]
temp_list = [None,1,None,None]

weights = np.array(weights)
max_weight_index = np.argmax(weights)

for i, tmp_element in enumerate(temp_list):

    if tmp_element is None:
        if i == max_weight_index:
            current_max = weights[i]
            weights[i] = 0
            max_weight_index = np.argmax(weights)
            weights[max_weight_index] += current_max
        else:
            weights[max_weight_index] += weights[i]
            weights[i] = 0
    print(weights)
输出

[0, 0.1, 0.2, 0.7]
[0, 0.1, 0.2, 0.7]
[0, 0.1, 0, 0.8999999999999999]
[0, 0.9999999999999999, 0, 0]
[ 0.   0.1  0.2  0.7]
[ 0.   0.1  0.2  0.7]
[ 0.   0.1  0.   0.9]
[ 0.  1.  0.  0.]


是否只移动与
None
对应的权重,还是移动所有权重?你能在问题中提到你是如何获得你提到的最终输出的吗?@GaneshTata是的,我只想将对应的权重转换为零。获得的最终输出是我想要实现的。我不知道如何在代码中实现这一点。“最终输出;因此第二个元素的权重为1.0,因为其他所有元素都没有。”-第二个元素在最后一步中是如何变为1.0的?在此步骤之前,第二个元素为0.1,最大元素为0.9。因此,max元素必须是除当前元素之外的所有元素中的max(在本例中为0.9)?@GaneshTata Yes,当它达到“最终输出”时;因此,该权重为1.0..',我们只有要比较的值(第二个元素和最后一个元素)。因为我们知道最后一个元素是None,所以权重(0.9)将移动到权重(0.1)并添加到权重(0.1)。所以0.1+0.9,我们得到1.0作为权重。谢谢!这就是我要找的。您提到您假设权重数组中没有重复项。如果权重完全相同(例如[0.3,0.1,0.1,0.5];请注意,两个值的权重为0.1)。这会引起任何问题吗?我想知道两个最大权重的指数是否会引起一些问题。因此我提到了这个假设。需要检查这种情况。如果要均匀地移动重量,那该如何工作?假设[None,1,1,1],拆分0.3,使其他每个权重都得到0.1;在纯python方法中,它最终将成为[0.0,0.2,0.3,0.5],您必须首先执行(0.3/len(arr)-1)以获得0.1,迭代所有数组元素(跳过0.3)并用0.1更新它们。numpy方法可能更快,您可以尝试一下,看看是否能够加快计算速度。