dan*_*han 6 linear-regression python-3.x scikit-learn
在练习简单线性回归模型时,我遇到了这个错误,我认为我的数据集有问题。
这是错误体:
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.
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
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)
Run Code Online (Sandbox Code Playgroud)
谢谢
Nee*_*ell 29
很多时候在做线性回归问题时,人们喜欢想象这个图
在输入上,我们有一个 X = [1,2,3,4,5]
然而,许多回归问题具有多维输入。考虑一下房价的预测。决定房价的不是一项属性。它具有多种功能(例如:房间数量、位置等)
它告诉我们行由样本组成,列由特征组成。
但是,请考虑当我们将一项特征作为输入时会发生什么。然后我们需要一个 nx 1 维输入,其中 n 是样本数,第 1 列代表我们唯一的特征。
为什么这个array.reshape(-1, 1)建议有效?-1 表示根据提供的列数选择有效的行数。请参阅图像以了解它在输入中如何变化。

小智 7
如果您查看LinearRegressionscikit-learn 的文档。
拟合(X,Y,sample_weight=None)
X : 形状 [n_samples,n_features] 的 numpy 数组或稀疏矩阵
预测(X)
X : {array-like, sparse matrix}, shape = (n_samples, n_features)
正如你可以看到X有2个维度,那里的,你的x_train和x_test明确有一个。按照建议,添加:
x_train = x_train.reshape(-1, 1)
x_test = x_test.reshape(-1, 1)
Run Code Online (Sandbox Code Playgroud)
在拟合和预测模型之前。
您需要同时给fit和predict方法提供2D数组。你x_train,y_train并且x_test目前仅一维。控制台建议的内容应该可以工作:
x_train= x_train.reshape(-1, 1)
y_train= y_train.reshape(-1, 1)
x_test = x_test.reshape(-1, 1)
Run Code Online (Sandbox Code Playgroud)
这使用numpy的reshape。reshape过去已经回答过有关问题,例如应该回答什么reshape(-1,1)意思:-1在numpy重塑中是什么意思?
| 归档时间: |
|
| 查看次数: |
14778 次 |
| 最近记录: |