Solving Python script Error – Expected 2D array, got 1D array instead

Expected 2D array, got 1D array instead is a common error which occurs when you are not converting your data correctly or not using the predict method correctly.

In this post we will try to solve your error and see why it occurs in the first place, we will present multiple solutions so you can find the one which suits your case.

Exploring the Error : Expected 2D array, got 1D array instead

This error is a very common error which occurs when you are not converting your data correctly or not using the predict method correctly.

The error message you have should look like this, make sure its the exact match in order to avoid further confusion.

                                                                       #
Expected 2D array, got 1D array instead
                                                                       #

Bellow we will describe how the error can be solved. With multiple possible solutions.

Solution 1 : Correctly use the predict method

The first solution is to use the predict method with the values you want to process. The only condition here to apply it on data that has the same dimension as the training data that was provided.

As a result, you should use

                                                                       #
.predict([[0.42,0.61]])
                                                                       #

Instead of using

                                                                       #
.predict([0.42,0.61])
                                                                       #

The solution above will work for most people, you can find another quick fix bellow.

Solution 2 : Convert into a 1D array and reshape the data when using panda.Series

The second solution is for developers using panda.Series.

The solution is to convert the data into a 1D array and reshape the data.

                                                                       #
df = pd.DataFrame(list(BiogasPlant.objects.all()))
test = df.iloc[-1:]       # sliced it here
                                                                       #

Now, it is a 1 dimentional array

                                                                       #
test2d = test.values.reshape(1,-1)
                                                                       #

You can read more about this in this documentation.

                                                                       #
http://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.Series.values.html
                                                                       #

This solution should be enough to get rid of the error for good.

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

The article is over guys, I hope my effort did not go to waste and I helped some of you solve this error, keep learning Python and keep coding, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/