I am trying to do a regression analysis and then save the result in a csv file.
Two questions:
- Why set/list of results coming from regression analysis (
regr.predict
from sklearn import linear_model
) is being printed in csv in a single cell? I am trying to print every prediction in a separate row one below another (in my case I have 418 results so should be 418 rows in same column).
prediction1=regr.predict(xtest)
with open('my_output_file.csv', 'w', newline='') as f:
fieldnames=['PassengerId','Survived']
writer = csv.DictWriter(f, fieldnames)
writer.writeheader()
for result in prediction1:
writer.writerow({'PassengerId':'test','Survived':prediction1})
- How do I make python write in a certain column or even in a certain cell in csv? (For example, if I want to have a passenger ID in the first column and then have regression results in second column.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…