简体   繁体   中英

ValueError: Data must be 1-Dimensional error while creating a dataframe

I am trying to solve a classification problem with a neural network and after I get the prediction I want to create a pandas data frame with a column from the test dataset and my predictions as the second column. But I am constantly getting error. Here is my code: enter image description here

and here is my error: enter image description here

Important sidenote: Please, take some time to look into How to make good reproducible pandas examples , there are great suggestions there on how you could ask your question better.

Now for your error:

Data must be 1-dimensional

That means pandas wants a 1-dimensional array, ie of the form [0,0,1,1,...,1]. But your preds array is 2-dimensional, ie of the form [[0],[0],[1],[1],...,[1]] .

So you need to flatten the preds array here:

在此处输入图像描述

Instead of for -loops consider using list comprehensions to change your code to something like this:

predictions = [1 if p>0.5 else 0 for p in preds]
df = pd.DataFrame({'PassengerId': test['PassengerId'].values, 
                   'Survived': predictions})

Also, in the meantime look into ndarray.round method - maybe it will better fit your use case:

predictions = preds.round()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM