I keep getting the error :“ValueError: Expected 2D array, got 1D array instead:” for a linear regression process

蹲街弑〆低调 提交于 2021-02-10 06:29:32

问题


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:


回答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




回答2:


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.



来源:https://stackoverflow.com/questions/51705882/i-keep-getting-the-error-valueerror-expected-2d-array-got-1d-array-instead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!