尝试将预测返回到数据帧中的相应行

时间:2019-06-25 18:27:23

标签: python pandas numpy machine-learning keras

我正在以numpy ndarray的形式对模型进行预测。它们都符合S形输出。我现在想将numpy数组中的每个值重新插入到数据帧中的相应行中,并有条件地说明是否> .5 then == 1 else <.5 == 0

到目前为止,我可以读取numpy数组,但似乎无法正确地将其一一正确地添加到数据框中

employers = data_churn
# employers = np.array([employers])
predictions = model_churn.predict(employers)
predictions

employerPredictions = real_churn
employerPredictions = employerPredictions.rename(index=str, columns={"main_reason": "churned"})
employerPredictions.drop(['biztype_knowledge','biztype_field','biztype_creative','PercentEmpChg','PercentChgRevenue','PercentChgPay','amountOfResignations','nb_months_active'], axis=1, inplace=True)
if predictions.any() > .5:
    employerPredictions['predictedChurn'] = 1
    employerPredictions['ConfidenceWillChurn %'] = round((predictions[0][0] * 100), 2)
else:
    employerPredictions['predictedChurn'] = 0
    employerPredictions['ConfidenceWillNotChurn %'] = round(((1 - predictions[0][0]) * 100), 2)


employerPredictions

到目前为止,any方法仅返回第一个预测并为数据帧中的所有预测进行设置

1 个答案:

答案 0 :(得分:1)

如何将预测取整为1和0:

employerPredictions['predictedChurn'] = np.round(predictions).astype(np.int8)

#Or you just downcast it to int
employerPredictions['predictedChurn'] = predictions.astype(np.int8)

#Or use np.where
employerPredictions['predictedChurn'] = np.where(predictions>=0.5,1,0)

就ConfidenceWillChurn%或ConfidenceWillNotChurn%而言,我会尝试这样做,但我不确定这就是您要的内容。

employerPredictions['ConfidenceWillChurn %'] = np.where(predictions>=0.5,predictions*100,np.nan)

employerPredictions['ConfidenceWillNotChurn %'] = np.where(predictions<0.5,(1-predictions)*100,np.nan)

我放了np.nan,但是当条件不满足时,您可以选择另一个值。 我使用了where-method from numpy。熊猫也有一个地方方法,但是却有所不同。