在练习简单线性回归模型时,我遇到了这个错误,
我认为我的数据集有问题。

Here is my data set:

Here is independent variable X:

Here is dependent variable Y:

Here is X_train

Here Is 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)

谢谢

最佳答案

您需要同时给 fit predict 方法2D数组。您的x_trainy_trainx_test当前仅为一维。控制台建议的内容应该可以工作:

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

这使用了numpy的 reshape 。过去已经回答了有关reshape的问题,例如应回答reshape(-1,1)的含义:What does -1 mean in numpy reshape?

关于python-3.x - ValueError : Expected 2D array,改为使用1D数组:,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51150153/

10-16 23:06