python- logistic regression, save predicted probabilities & predictions to csv

时间:2016-12-02 05:04:45

标签: python csv pandas

I am new to data analysis and python. I have mostly been following the logistic regression demonstrated in this link on titanic survivors:

http://hamelg.blogspot.ca/2015/11/python-for-data-analysis-part-28.html

I am using my own non titanic dataset though. I am at the end of the example where I want to export the results to a csv file. I made a small modification though which is getting me stuck. In addition to the prediction, i also explicitly generated the predicted probabilities which I would also like to export to the csv file.

test_probs=log_model.predict_proba(X=test_features)
print(test_probs)


# Create a submission for Kaggle
submission = pd.DataFrame({"AccountNumber":titanic_test["AccountNumber"],
"PolarPredict":test_preds,**"probabilities":test_probs** })

This is the message i get: Exception: Data must be 1-dimensional

here is the original code from the tutorial: # Make test set predictions test_preds = log_model.predict(X=test_features)

# Create a submission for Kaggle
submission = pd.DataFrame({"PassengerId":titanic_test["PassengerId"],
                       "Survived":test_preds})

# Save submission to CSV
submission.to_csv("tutorial_logreg_submission.csv", 
              index=False) 

How can I export the prediction, probabilities and the "ID" into a csv file?

1 个答案:

答案 0 :(得分:0)

you can try converting them into list

submission = pd.DataFrame({
     "AccountNumber":list(titanic_test["AccountNumber"]),
     "PolarPredict":list(test_preds),
     "probabilities":list(test_probs)
     })

submission.to_csv("tutorial_logreg_submission.csv", 
          index=False)