Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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_Scikit Learn_Linear Regression_Sklearn Pandas - Fatal编程技术网

Python 拟合线性回归

Python 拟合线性回归,python,scikit-learn,linear-regression,sklearn-pandas,Python,Scikit Learn,Linear Regression,Sklearn Pandas,我有一个问题: regression.fit(X_train, y_train) 我得到了以下错误: ValueError: Expected 2D array, got 1D array instead: array=[ 2.9 5.1 3.2 4.5 8.2 6.8 1.3 10.5 3. 2.2 5.9 6. 3.7 3.2 9. 2. 1.1 7.1 4.9 4. ]. Reshape your data either using ar

我有一个问题:

  regression.fit(X_train, y_train)
我得到了以下错误:

ValueError: Expected 2D array, got 1D array instead:
array=[ 2.9  5.1  3.2  4.5  8.2  6.8  1.3 10.5  3.   2.2  5.9  6.   3.7  3.2
  9.   2.   1.1  7.1  4.9  4. ].
Reshape 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.

您尚未共享源代码,但这是我的2美分:

如果您使用的是硬编码阵列,而不是1D阵列:

array=[ 2.9, 5.1, 3.2, 4.5, 8.2, 6.8, 1.3, 10.5, 3., 2.2, 5.9, 6., 3.7, 3.2, 9., 2., 1.1, 7.1, 4.9, 4.]
使用二维阵列:

array=[[ 2.9, 5.1, 3.2, 4.5, 8.2, 6.8, 1.3, 10.5, 3., 2.2, 5.9, 6., 3.7, 3.2, 9., 2., 1.1, 7.1, 4.9, 4.]]
如果要将一维数组转换为二维数组,只需使用numpy函数即可。比如说,

>>> test_array = np.array([1, 2, 3, 4, 5])
>>> test_array
array([1, 2, 3, 4, 5])
>>> test_array = test_array.reshape(1, -1)
>>> test_array
array([[1, 2, 3, 4, 5]])

他们的样本量不太可能是1,他们可能需要
。重塑(-1,1)
这能回答你的问题吗?