【问题标题】:How to fix sklearn multiple linear regression ValueError in python (inconsistent numbers of samples: [2, 1])如何修复python中的sklearn多元线性回归ValueError(样本数量不一致:[2, 1])
【发布时间】:2019-05-27 16:28:15
【问题描述】:

我的线性回归与单个特征完美配合。自从尝试使用两个以来,我收到以下错误:ValueError:找到样本数量不一致的输入变量:[2, 1]

第一个打印语句打印以下内容: (2, 6497) (1, 6497)

然后代码在 train_test_split 阶段崩溃。

有什么想法吗?

feat_scores = {}
X = df[['alcohol','density']].values.reshape(2,-1)   
y = df['quality'].values.reshape(1,-1)

print (X.shape, y.shape)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

print (X_train.shape, y_train.shape)
print (X_test.shape, y_test.shape)

reg = LinearRegression()
reg.fit(X_train, y_train)

reg.predict(y_train)

【问题讨论】:

    标签: python scikit-learn linear-regression


    【解决方案1】:

    你错过了这一行

    X = df[['alcohol','density']].values.reshape(2,-1)   
    y = df['quality'].values.reshape(1,-1)
    

    不要将数据重新整形为 (2, 6497) (1, 6497),而必须将其设为 (6497,2) (6497,) >

    Sklearn 直接获取数据帧/系列。所以你可以给,

    X = df[['alcohol','density']]
    y = df['quality']
    

    此外,您只能使用 X 值进行预测,因此

    reg.predict(X_train)
    

    reg.predict(X_test)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-24
      • 2016-05-28
      • 2017-12-31
      • 2017-04-06
      • 2019-04-06
      • 2017-06-21
      • 2020-11-03
      • 2019-01-30
      相关资源
      最近更新 更多