Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中线性回归中每行系数的求法_Python_Python 2.7_Extjs_Scikit Learn_Linear Regression - Fatal编程技术网

Python中线性回归中每行系数的求法

Python中线性回归中每行系数的求法,python,python-2.7,extjs,scikit-learn,linear-regression,Python,Python 2.7,Extjs,Scikit Learn,Linear Regression,因此,我从CSV文件中读取数据,并尝试使用获取每行的系数 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', r

因此,我从CSV文件中读取数据,并尝试使用获取每行的系数

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_)
这将返回一个错误:

"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."
我试图把每一行对应的系数放到我的网格中


谁来帮忙?谢谢

问题取决于您定义
X
y
的方式。 您应该尝试添加一对额外的方括号。 因此,改变这一点:

X = df['param_a']
y= df['param_b']
为此:

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

希望这有帮助

它仍然会向我返回相同的错误:“预期的2D数组,改为1D数组:\narray=[-125 100-10 250-200 10-125 0 125 0 250-100-250 125-20]。\n使用数组重新成形数据。如果数据有单个特征或数组,则重塑(-1,1)。如果数据包含单个样本,则重塑(1,-1)。@Forozo,它只返回一个值[[0.00110844]]。可以得到每行的系数吗?谢天谢地,效率并不是这样的。简单地说,系数显示列在目标向量y的计算中的重要性。在您的情况下,0.0011084是一个极低的系数。我建议您向数据中添加更多功能或增加数据的大小。如果每行都有相应的系数,我会尝试这样做吗?我也在试着做代码?我假设你指的是你的照片,因为你可以看到第一列是标签功能,这意味着你在列中看到的每个标签都指的是他的数据帧中的功能(在你的例子中,它是由许多行组成的
参数a
是你的功能)然后在它旁边,他放上特征的系数。如果他要对您的数据帧执行相同的表格,他会将
param_a
放在feature列中,将
0.00110844
放在EstimateCefficient列中。