【问题标题】:ValueError: Expected 2D array, got 1D array instead:ValueError:预期的 2D 数组,得到 1D 数组:
【发布时间】:2018-12-11 12:09:48
【问题描述】:

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

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)

谢谢

【问题讨论】:

    标签: python-3.x scikit-learn linear-regression


    【解决方案1】:

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

    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?

    【讨论】:

    • 你不应该重塑y_train,因为你希望它是一维数组。
    【解决方案2】:

    如果您查看LinearRegression of scikit-learn 的文档。

    fit(X, y, sample_weight=None)

    X : numpy 数组或形状为 [n_samples,n_features] 的稀疏矩阵

    预测(X)

    X : {array-like, sparse matrix}, shape = (n_samples, n_features)

    如您所见,X 有 2 个维度,而您的 x_trainx_test 显然只有一个。 按照建议,添加:

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

    在拟合和预测模型之前。

    【讨论】:

      【解决方案3】:

      使用

      y_pred = regressor.predict([[x_test]])
      

      【讨论】:

        【解决方案4】:

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

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

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

        如果您查看文档,您会看到这个

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

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

        为什么 array.reshape(-1, 1) 建议有效? -1 表示根据提供的列数选择有效的行数。请参阅图像以了解它在输入中的变化。

        【讨论】:

        • 很好的解释,我认为这应该是正确的答案。
        【解决方案5】:

        我建议在拆分成训练和测试数据集之前先重塑 X:

        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
        # Here is the trick
        x = x.reshape(-1,1)
        
        #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)
        

        【讨论】:

          【解决方案6】:

          这是我用的

          X_train = X_train.values.reshape(-1, 1)
          y_train = y_train.values.reshape(-1, 1)
          X_test = X_test.values.reshape(-1, 1)
          y_test = y_test.values.reshape(-1, 1)
          

          【讨论】:

            【解决方案7】:

            这就是解决办法

            regressor.predict([[x_test]])
            

            对于多项式回归:

            regressor_2.predict(poly_reg.fit_transform([[x_test]]))
            

            【讨论】:

              【解决方案8】:

              修改

              regressor.fit(x_train,y_train)
              y_pred = regressor.predict(x_test)
              

              regressor.fit(x_train.values.reshape(-1,1),y_train)
              y_pred = regressor.predict(x_test.values.reshape(-1,1))
              

              【讨论】:

                猜你喜欢
                • 2020-03-19
                • 2021-12-29
                • 1970-01-01
                • 2018-09-16
                • 2020-12-18
                • 2018-12-21
                • 2020-03-03
                • 2019-06-06
                相关资源
                最近更新 更多