ValueError:预期的二维数组,得到的是一维数组:

新手上路,请多包涵

在练习简单线性回归模型时出现此错误,我认为我的数据集有问题。

这是我的数据集:

这是自变量 X:

这是因变量 Y:

这是 X_train

这是 Y_train

这是错误正文:

 ValueError: Expected 2D array, got 1D array instead:
array=[ 7.   8.4 10.1  6.5  6.9  7.9  5.8  7.4  9.3 10.3  7.3  8.1].
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.

这是我的代码:

 import pandas as pd
import matplotlib as pt

#import data set

dataset = pd.read_csv('Sample-data-sets-for-linear-regression1.csv')
x = dataset.iloc[:, 1].values
y = dataset.iloc[:, 2].values

#Spliting the dataset into Training set and Test Set
from sklearn.cross_validation import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size= 0.2, random_state=0)

#linnear Regression

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()
regressor.fit(x_train,y_train)

y_pred = regressor.predict(x_test)

谢谢

原文由 danyialKhan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 461
2 个回答

您需要为 fitpredict 方法提供二维数组。你的 x_trainx_test 目前只有一维。控制台建议的内容应该有效:

 x_train= x_train.reshape(-1, 1)
x_test = x_test.reshape(-1, 1)

这使用 numpy 的 reshape 来转换你的数组。例如, x = [1, 2, 3] 将被转换为矩阵 x' = [[1], [2], [3]] (-1给出矩阵的x维度,从数组的长度和剩余维度推断,1是y维度- 给我们一个 anx 1 矩阵,其中 n 是输入长度)。

关于 reshape 的问题在过去已经得到回答,例如应该回答什么 reshape(-1,1) 完全意味着: -1 在 numpy reshape 中意味着什么? (下面的其他一些答案也很好地解释了这一点)

原文由 CodeMonkey123 发布,翻译遵循 CC BY-SA 4.0 许可协议

很多时候在做线性回归问题的时候,人们喜欢想象这个图

一变量输入线性回归

在输入中,我们有一个 X = [1,2,3,4,5]

然而,许多回归问题都有多维输入。考虑房价的预测。这不是决定房价的一个属性。它具有多项功能(例如:房间数量、位置等)

如果你查看文档,你会看到这个 文档截图

它告诉我们行由样本组成,而列由特征组成。

输入说明

但是,考虑一下当他将一个特征作为我们的输入时会发生什么。然后我们需要一个 nx 1 维输入,其中 n 是样本数,第 1 列代表我们唯一的特征。

为什么 array.reshape(-1, 1) 建议有效? -1 表示根据提供的列数选择行数。查看图像以了解它在输入中的变化方式。 使用 array.reshape 进行转换

原文由 Neel Sandell 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进