Getting coefficient of each row in linear regression in Python

Multi tool use
Getting coefficient of each row in linear regression in Python
so I am reading from a CSV file and tried to use get the coefficient of every row
df = pd.read_csv(os.path.join(path))
X = df['param_a']
y= df['param_b']
X_train, X_test, y_train, y_test= train_test_split(X,y)
reg = linear_model.LinearRegression()
reg.fit(X_train, y_train)
print('Coefficients: n', reg.coef_)
this returns an error:
"Expected 2D array, got 1D array instead:narray=[-100 0 0 100 -20 250 200 -125 -250 0 20 -250 -200 125 -10].nReshape 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."
I'm trying to get the coefficient of every corresponding row to my grid.
anyone, please help? thanks
1 Answer
1
The problem relies upon the way you are defining both X
and y
.
you should try adding an extra pair of square bracers.
thus changing this:
X
y
X = df['param_a']
y= df['param_b']
to this:
X = df[['param_a']]
y= df[['param_b']]
hope this helps
I just edited my answer, take a look
– Fozoro
Jul 3 at 8:24
@Forozo, it only returns one value [[ 0.00110844]]. Is it possible to get the coefficient of each of the rows? thanks
– Mrii Rya
Jul 3 at 8:30
Coefficients don't really work that way. simply put, coefficients show you how important a column is in the calculation of your target vector y. 0.0011084 is an extremely low coefficient in your case. I would suggest you to add more features to your data or increase the size of your data.
– Fozoro
Jul 3 at 8:34
im trying to do this bigdata-madesimple.com/… where each row has a corresponding coefficient? Im also trying to do the code?
– Mrii Rya
Jul 3 at 8:38
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
it still returns the same error to me: "Expected 2D array, got 1D array instead:narray=[-125 100 -10 250 -200 10 -125 0 125 0 250 -100 -250 125 -20].nReshape 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."
– Mrii Rya
Jul 3 at 8:19