0

I don't understand where i'm getting wrong. Both of the arrays are of same shape and has same no. of elements. i'm getting - "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."

I'll provide my code below -

y_pred:
    array([1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1,
           1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1,
           0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1,
           0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1,
           0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1,
           1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0,
           1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1,
           1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0,
           1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 1], dtype=int64)
   

 y_pred.shape - (200,)
    
   
 y_test :array([1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1,
           1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1,
           0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1,
           0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0,
           0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1,
           1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0,
           0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1,
           1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0,
           1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 1], dtype=int64)
    

y_test.shape -  (200,)

Now when i tried to evaluate this (dtc.score(y_test,y_pred_dtr)) , I'm getting this error -

ValueError: Expected 2D array, got 1D array instead:
    array=[1. 0. 0. 0. 0. 0. 1. 0. 0. 1. 1. 1. 0. 0. 1. 0. 1. 0. 0. 1. 1. 1. 1. 1.
     0. 1. 0. 0. 0. 1. 0. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 1. 0. 0. 0. 1.
     1. 0. 1. 1. 1. 0. 1. 0. 1. 0. 0. 0. 1. 1. 1. 0. 1. 1. 0. 1. 0. 1. 1. 0.
     0. 0. 0. 1. 1. 1. 1. 1. 0. 0. 1. 1. 1. 0. 1. 0. 0. 0. 1. 0. 0. 1. 1. 0.
     0. 0. 0. 1. 0. 0. 1. 0. 0. 1. 1. 0. 0. 1. 1. 1. 1. 1. 1. 0. 0. 0. 0. 1.
     1. 1. 0. 0. 0. 1. 0. 1. 1. 1. 0. 0. 0. 1. 1. 0. 1. 1. 1. 0. 0. 0. 1. 1.
     0. 1. 0. 0. 0. 0. 1. 0. 0. 1. 1. 0. 0. 0. 0. 1. 1. 0. 1. 0. 1. 0. 1. 1.
     0. 0. 1. 0. 1. 1. 1. 0. 1. 1. 1. 1. 0. 1. 0. 1. 1. 1. 1. 0. 1. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 1.].
    

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. 
6
  • When your are doing dtc.score(y_test, y_pred_dtr). When did you obtained y_pred_dtr? I only see y_pred. Also, I can see that you don't obtain a normal array, values are no separated by commas. I don't know if its an error of copying the output Commented Jan 11, 2021 at 8:15
  • @AlexSerraMarrugat I did : y_pred = dtc.predict(x_test). this is how i got value of y_pred. Commented Jan 11, 2021 at 8:22
  • Yes, but in your score you write: y_pred_dtr. This a variable you haven't defined yet. Can you check it? Commented Jan 11, 2021 at 8:28
  • @AlexSerraMarrugat i wrote that by mistake in the comment , i calculated the y_pred_dtr Commented Jan 11, 2021 at 12:11
  • How many features you have? Commented Jan 11, 2021 at 12:41

1 Answer 1

1
here is the thing,
a=np.array([1,2,3]) is 1D array
now in y_pred and y_test it should be 
array=np.array([sample1array,sample2array,....])
so it should be,
a=np.array([[1],[2],[3]])
so try reshape(-1,1) so
np.array([1,2,3]) will be converted 'n' rows '1' column array (2D) = np.array([[1],[2],[3]]).
try this.

EDIT: because it may be possible that your output length is greater than 1 then? ,like y=[[1,0],[0,0],[1,0]]. this is the only way to do that. thanks.

Sign up to request clarification or add additional context in comments.

2 Comments

I understood that but i have to reshape both the arrays ? also why do we need 2D arrays for evaluating the score of model ?
sorry but no, both the array are of same shape , i have mentioned that in the question . Both the arrays are 1D and have 200 values in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.