【问题标题】:How can I save my prediction values as strings in my logistic regression model in python?如何在 python 的逻辑回归模型中将预测值保存为字符串?
【发布时间】:2020-04-05 18:50:16
【问题描述】:

此代码给我的预测值为 1 0r 0。但我想将它们打印为“成功”或“失败”。有没有办法?

prediction = pd.DataFrame(predictions2, columns=['predictions']).to_csv('prediction.csv')

【问题讨论】:

    标签: python machine-learning logistic-regression prediction


    【解决方案1】:

    您需要做的就是使用字典将 0 和 1 映射到失败和成功

    prediction = pd.DataFrame(predictions2, columns=['predictions'])
    prediction['predictions'] = prediction['predictions'].astype(int)
    prediction["predictions"] = prediction.predictions.map({0:'Failure', 1:'Success'})
    prediction.to_csv('prediction.csv', index=False)
    

    您必须确保“预测”的数据类型是 int。

    【讨论】:

      【解决方案2】:

      我对 pandas 没有太多经验,但如果您只想将这些值分配给这些输出,一种方法是使用字典(在我键入时发布)。您还可以使用三元运算符。例如:

      prediction = pd.DataFrame(predictions2, columns ['predictions']).to_csv('prediction.csv')
      result = 'Success' if prediction == 1 else 'Failure'
      print(result)
      

      这可以进一步简化为:

      print('Success' if prediction == 1 else 'Failure')
      

      注意:如果输出是 int,则使用 1,如果是 string,则使用 '1'。

      【讨论】:

        【解决方案3】:

        谢谢大家。已经解决了!

        **mydict ={0:'Failure',1:'Success'}
        predictions2 = [mydict[i] for i in predictions2]
        print(predictions2)
        predictions2= pd.DataFrame(predictions2, columns=['predictions']).to_csv('prediction3.csv', index=False)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-10-23
          • 1970-01-01
          • 1970-01-01
          • 2022-11-27
          • 2021-04-30
          • 1970-01-01
          • 2023-04-02
          • 2020-11-26
          相关资源
          最近更新 更多