简体   繁体   中英

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

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 = df['param_a']
y= df['param_b']

to this:

X = df[['param_a']]
y= df[['param_b']]

hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM