尝试将预测值回填到数据框的相应行

我正在使用我的模型进行预测,输出形式为numpy ndarray。所有预测值都经过sigmoid函数处理。现在,我想将numpy数组中的每个值回填到数据框的相应行中,并且添加一个条件:如果值大于0.5,则设为1;否则,如果值小于0.5,则设为0。

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

employers = data_churn# employers = np.array([employers])predictions = model_churn.predict(employers)predictionsemployerPredictions = real_churnemployerPredictions = 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:

employerPredictions['predictedChurn'] = np.round(predictions).astype(np.int8)#或者你可以直接将其转换为整数employerPredictions['predictedChurn'] = predictions.astype(np.int8)#或者使用np.whereemployerPredictions['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,但你可以选择其他值来表示条件不满足的情况。我使用了来自numpy的where方法。Pandas也有where方法,但功能有所不同。

Related Posts

使用LSTM生成助记符 | 如何确保我的模型使用损失函数生成有意义的句子?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

自定义Keras损失函数的奇怪Nan损失

我在尝试在Keras中实现一个自定义损失函数,但无法使…

为什么我们在K-means聚类方法中使用kmeans.fit函数?

我在一个视频中使用K-means聚类技术,但我不明白为…

如何获取Keras中ImageDataGenerator的.flow_from_directory函数扫描的类名?

我想制作一个用户友好的GUI图像分类器,用户只需指向数…

如何查看每个词的tf-idf得分

我试图了解文档中每个词的tf-idf得分。然而,它只返…

如何修复 ‘ValueError: Found input variables with inconsistent numbers of samples: [32979, 21602]’?

我在制作一个用于情感分析的逻辑回归模型时遇到了这个问题…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注