78

I attempt to solve this problem 6 in this notebook. The question is to train a simple model on this data using 50, 100, 1000 and 5000 training samples by using the LogisticRegression model from sklearn.linear_model.

lr = LogisticRegression()
lr.fit(train_dataset,train_labels)

This is the code i trying to do and it give me the error.

ValueError: Found array with dim 3. Estimator expected <= 2.

Any idea?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
edwin
  • 1,152
  • 1
  • 13
  • 27

5 Answers5

125

scikit-learn expects 2d num arrays for the training dataset for a fit function. The dataset you are passing in is a 3d array you need to reshape the array into a 2d.

nsamples, nx, ny = train_dataset.shape
d2_train_dataset = train_dataset.reshape((nsamples,nx*ny))
gsamaras
  • 71,951
  • 46
  • 188
  • 305
Kristian K.
  • 1,437
  • 1
  • 10
  • 9
  • 32
    Would you mind explaining how ndarray.reshape can magically transform 3D data to 2D without losing the information represented by the original vectors? – scipilot Aug 27 '17 at 02:01
  • 8
    First dimension is maintained and the other two dimensions are flattened (so 28x28 becomes 784). The fit algorithm will next consider the first 784 features part of sample number one and the next 784 features part of sample two and so on. – Andrés Marafioti Sep 07 '17 at 13:49
  • 2
    I have my X data separate from y labels. How can I flatten the X train dataset using this answer? Y labels are a 5k array. X_train is a 5k x 1024 x 1024 – BluePython Nov 11 '17 at 20:58
  • 1
    You are correct, but how did you get that information? The [documentation](http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression.fit) clearly says `Parameters: X : {array-like, sparse matrix}, shape (n_samples, n_features)` – kyriakosSt Apr 26 '18 at 15:45
  • 2
    So if I reshaped my data this way, fitted it and predicted my y. The shape of my y is now (nsamples, 1). But what I need y to be is (nx,ny) because I need a label for each pixel. What should I do in this case? – Nathan Apr 30 '20 at 05:31
  • This is wrong answer. nrgb is missing as the 4th parameter. – Irfan Shaikh Aug 09 '23 at 21:59
12

In LSTM, GRU, and TCN layers, the return_sequence in last layer before Dence Layer must set False . It is one of conditions that you encounter to this error message .

Attila
  • 139
  • 1
  • 4
4

If anyone is stumbling onto this question from using LSTM or any RNN for two or more time series, this might be a solution.

However, to those who want error between two different values predicted, if for example you're trying to predict two completely different time series, then you can do the following:

from sklearn import mean_squared_error 
# Any sklearn function that takes 2D data only
# 3D data
real = np.array([
    [
        [1,60],
        [2,70],
        [3,80]
    ],
    [
        [2,70],
        [3,80],
        [4,90]
    ]
]) 

pred = np.array([
    [
        [1.1,62.1],
        [2.1,72.1],
        [3.1,82.1]
    ],
    [
        [2.1,72.1],
        [3.1,82.1],
        [4.1,92.1]
    ]
])

# Error/Some Metric on Feature 1:
print(mean_squared_error(real[:,:,0], pred[:,:,0]) # 0.1000

# Error/Some Metric on Feature 2:
print(mean_squared_error(real[:,:,1], pred[:,:,1]) # 2.0000

Additional Info from the numpy indexing

Xavier
  • 423
  • 4
  • 11
3

I had a similar Error by solving an image classification problem. We have a 3D matrix: the first dimension is the total number of images, can be replaced by "-1", the second dimension is the product of the height and the width of the picture, the third dimension is equal to three, since the RGB image has three channels (red, green blue). If we don't want to lose information about the color of the image, then we use x_train.reshape(-1, nxny3). If the color can be neglected and thereby reduce the size of the matrix: x_train.reshape(-1, nxny1)

2

You probably have the last "lstm" layer in your model using "return_sequences=True". Change this to false to not return the output for further lstm models.