ValueError: Expected 2D array, got 1D array instead:

You need to give both the fit and predict methods 2D arrays. Your x_train and x_test are currently only 1 dimensional. What is suggested by the console should work:

x_train= x_train.reshape(-1, 1)
x_test = x_test.reshape(-1, 1)

This uses numpy’s reshape to transform your array. For example, x = [1, 2, 3] wopuld be transformed to a matrix x' = [[1], [2], [3]] (-1 gives the x dimension of the matrix, inferred from the length of the array and remaining dimensions, 1 is the y dimension – giving us a n x 1 matrix where n is the input length).

Questions about reshape have been answered in the past, this for example should answer what reshape(-1,1) fully means: What does -1 mean in numpy reshape? (also some of the other below answers explain this very well too)

Leave a Comment