Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python将预测结果转换为一个热点_Python - Fatal编程技术网

python将预测结果转换为一个热点

python将预测结果转换为一个热点,python,Python,我运行python神经网络预测,期望得到一个热门结果,并得到如下数据: [[0.33058667182922363, 0.3436272442340851, 0.3257860243320465], [0.32983461022377014, 0.3487854599952698, 0.4213798701763153], [0.3311253488063812, 0.3473075330257416, 0.3215670585632324], [0.38368630170822144,

我运行python神经网络预测,期望得到一个热门结果,并得到如下数据:

[[0.33058667182922363, 0.3436272442340851, 0.3257860243320465], 
[0.32983461022377014, 0.3487854599952698, 0.4213798701763153], 
[0.3311253488063812, 0.3473075330257416, 0.3215670585632324], 
[0.38368630170822144, 0.35151687264442444, 0.3247968554496765], 
[0.3332786560058594, 0.343686580657959, 0.32303473353385925]]
如何将数组转换为一个热结果,即

[[0,1,0],
[0,0,1],
[0,1,0],
[1,0,0]
[0,1,0]]

通过一个热结果,我假设您希望每个子列表的最大值为
1
,其余值为
0
(基于当前结果中的模式)。您可以使用列表理解来完成以下操作:

其中
my_list
是您的初始列表

但在上述方法中,每次迭代子列表时,您都将计算
max()
。更好的方法是这样做:

def get_hot_value(my_list):
    max_val = max(my_list)
    return [int(item == max_val) for item in my_list]

hot_list = [get_hot_value(sublist) for sublist in my_list]

编辑:如果列表中只有一个
1
(如果最大值超过1个元素),则可以修改
get\u hot\u value
函数,如下所示:

def get_hot_value(my_list):
    max_val, hot_list, is_max_found = max(my_list), [], False
    for item in my_list:
        if item == max_val and not is_max_found:
            hot_list.append(1)
        else:
            hot_list.append(0)
            is_max_found = True
    return hot_list
我建议:

n = [[0.33058667182922363, 0.3436272442340851, 0.3257860243320465],
     [0.32983461022377014, 0.3487854599952698, 0.4213798701763153],
     [0.3311253488063812, 0.3473075330257416, 0.3215670585632324],
     [0.38368630170822144, 0.35151687264442444, 0.3247968554496765],
     [0.3332786560058594, 0.343686580657959, 0.32303473353385925]]

hot_results = []

for row in n:
    hot_index = row.index(max(row))
    hot_result = [0] * len(row)
    hot_result[hot_index] = 1
    hot_results.append(hot_result)

print(hot_results)

其他解决方案都很好,解决了问题。或者,如果你有numpy

import numpy as np
n = [[0.33058667182922363, 0.3436272442340851, 0.3257860243320465],
     [0.32983461022377014, 0.3487854599952698, 0.4213798701763153],
     [0.3311253488063812, 0.3473075330257416, 0.3215670585632324],
     [0.38368630170822144, 0.35151687264442444, 0.3247968554496765],
     [0.3332786560058594, 0.343686580657959, 0.32303473353385925]]

max_indices = np.argmax(n,axis=1)

final_values = [n[i] for i in max_indices]

argmax能够找到该行中最大值的索引,然后您只需要对该行进行一次列表理解。我想应该很快吧?

很好的解决方案。但是我会考虑一个超过一个最大值的情况,这将产生不止一个<代码> 1代码>代码。我认为在这种情况下,应该不止是代码> 1 < /代码>。正如我提到的,我不知道hot result的定义,但我是根据模式猜出来的。如果必须的话。上述解决方案可能是modified@YevhenKuzmovych:我认为这是基于一个热门话题的合理假设。做了一个有用的编辑。但是
1不在热门列表中
会将复杂性增加到n^2(如果有关系的话)。@YevhenKuzmovych:是的,它会增加复杂性。使用标志更新了答案:)如果列表中只需要一个
1
,那么这是一个简单而好的方法
import numpy as np
n = [[0.33058667182922363, 0.3436272442340851, 0.3257860243320465],
     [0.32983461022377014, 0.3487854599952698, 0.4213798701763153],
     [0.3311253488063812, 0.3473075330257416, 0.3215670585632324],
     [0.38368630170822144, 0.35151687264442444, 0.3247968554496765],
     [0.3332786560058594, 0.343686580657959, 0.32303473353385925]]

max_indices = np.argmax(n,axis=1)

final_values = [n[i] for i in max_indices]