简体   繁体   中英

Expected 2D array, got 1D array instead: array=[5.6 7. ]. Reshape your data either using array.reshape(-1, 1)

when we found value through the predict but i received an error

"Expected 2D array, got 1D array instead:
array=[5.6 7. ].
Reshape your data either using array.reshape(-1, 1) if your data has a 
  single feature or array.reshape(1, -1) if it contains a single sample."

this is error plzz solve my problem it's very complicated error that's why i haven't received error solution

You just printed the error, But error itself gave the solution.

In case function accepts only 2 D array, then any numpy.ndarray can be converted to the 2 array by reshaping it by numpy.reshape(arr, (1, -1).

Now please observe the effect of reshape opperation, Here (1, -1) , first 1 represent the first dimension of array, -1 represent or just use for flattening the array, in case you have array with dimension greater than 2.

For 1-D array you can also use (1, len(arr))

import numpy
arr = = numpy.array([1, 3, 5, 6, 7, 8, 8])
numpy.reshape(arr, (1, -1))
numpy.reshaoe(arr, (1, len(arr)))

both reshaping has same result. Output

>>> array([[1, 3, 5, 6, 7, 8, 8]])  # output of numpy.reshape(arr, (1, -1))
>>> array([[1, 3, 5, 6, 7, 8, 8]])  # output of numpy.reshape(arr, (1, len(arr)))

So reshaping makes your array compatible with the function which you are using, but in case function excepts 2D and you are having 1D array, there should be some meaningful way to transform your 1-D thing to 2-D.

Means if function accepts 2D image, and you have 1D audio sequence, there really should meaning full way to transform.

If you could have also mention that, help will true help. For now we are guessing on our mind thought what you need.

So always before asking question please spent some time in framing question, so getting answer and getting issue solved is guaranteed

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