我已经训练了一个xgboost nulticlass分类器。我想要预测类和概率
假设我有:
import pandas as pd
import numpy as np
result = pd.DataFrame({'id': [1,2,3,4], 'Pred class': ['a', 'b', 'c', 'c']})
predictions = np.array([[0.2, 0.3, 0.5],
[0.1, 0.5, 0.4],
[0.7, 0.2, 0.1],
[0.4, 0.2, 0.6]])
我正在寻找最大概率的指数:
max_probs = np.argmax(predictions, axis=1)
我正在创建一个列表,列出每个类别中的最大概率:
res = []
for idx, (el, el2) in enumerate(zip(predictions, max_probs)):
res.append(predictions[idx, max_probs[idx] ] * 100)
然后,我将结果添加到原始数据帧:
result['probs'] = res
我有:
id Pred class probs
1 a 50.0
2 b 50.0
3 c 70.0
4 c 60.0
对于较大的数据帧,哪种方法最有效?
这里有一种使用Pandas max的方法:
result["probs"] = pd.DataFrame(predictions).max(axis=1) * 100
print(result)
# Output
id Pred class probs
0 1 a 50.0
1 2 b 50.0
2 3 c 70.0
3 4 c 60.0