Search code examples
pythonjupyter-notebookreshapevalueerror

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


Test_train split:

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30, random_state = 10)

Test train values are: X_train = (36201, 32) X_test = (15516, 32) y_ train = (36201,) y_test = (15516,)

*

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
model_lda = LinearDiscriminantAnalysis()
model_lda.fit(y_train, X_train)

Error i am getting:

ValueError: Expected 2D array, got 1D array instead: array=[0. 2. 4. ... 2. 4. 3.].
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.


Solution

  • Try to always write a minimal, reproducible example. It helps a lot in resolving errors.

    Looking at your model_lda.fit, X and y should be inverted: model_lda.fit(X_train, y_train).

    You can check the documentation of LinearDiscriminantAnalysis here.