I keep getting the error :"ValueError: Expected 2D array, got 1D array instead:" for a linear regression process
Asked Answered
A

2

2

I have 2 arrays which are true_stress and true_strain. I want to do a linear regression to their log10 versions but I keep getting the said error.

from sklearn.linear_model import LinearRegression
log_tStress = np.log10(true_stress)
log_tStrain = np.log10(true_strain)

regressor = LinearRegression()
regressor.fit(log_tStrain, log_tStress)
predict = regressor.predict(log_tStrain)

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

Alleyne answered 6/8, 2018 at 10:48 Comment(2)
What's the shape of your true_stress and true_strain arrays?Patrizio
Possible duplicate of Sklearn Linear Regression - "IndexError: tuple index out of range"Philana
E
1

Well it kinda is just what it says.You are feeding a 1D array where you need a 2D one.

Numpy.log10 gives you a array where each values is the log of whatever you feed it (if it's negative it returns Nan check for that) with the same shape of whatever you feed it.

You arent verry clear on what you want to predict so i'll just assume you want to do linear regresion on a matrix like this [log_tStress, log_tStrain] so you can predict log_tStress over log_tStrain (or vice versa)

I cant help you much given i have 0 idea what log_tStress is or how it looks like . I can assume log_tStrain is your training data. If you want to predict the training data alone you'll have to give it another dimension to it's matrix (something relevant to whatever the training data is) but if you have nothing on that index might work too

.Fit requires you these params:

X - the training data [nr_samples, nr_features]

y - the array shape of said targets

Here I'll link you the documentation so you can look: http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html

Evars answered 6/8, 2018 at 11:32 Comment(2)
To expand on the answer. If you only have 1 feature, you can convert your log_tStress to a 2D array like so: log_tStress[:,None]Graecoroman
Thank you very much for your answer. Both vectors were row vectors. When I converted one to a column vector It worked.Alleyne
F
0

See the error trace-back and see on which array you are getting error and than reshape it using array.reshape[first dimension value,second dimension value] If you paste your error trace back I will be more specific.

Floppy answered 6/8, 2018 at 12:40 Comment(1)
Yea, It worked when I reshaped one vector to a column vector as; log_tStrain = log_tStrain.reshape(len(log_tStrain), 1) Thank you very much.Alleyne

© 2022 - 2024 — McMap. All rights reserved.