0

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

  • 1
    Please post the code. Try to distill your problem down to a minimal reproducible example, in case the code is too extensive otherwise. – 7shoe Aug 09 '22 at 06:57
  • 1
    If you feel like some answer solved your problem, you can "accept" it by clicking its checkmark. – qaziqarta Aug 10 '22 at 22:32

1 Answers1

0

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, i.e. of the form [0,0,1,1,...,1]. But your preds array is 2-dimensional, i.e. of the form [[0],[0],[1],[1],...,[1]].

So you need to flatten the preds array here:

enter image description 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()
qaziqarta
  • 1,782
  • 1
  • 4
  • 11