【问题标题】:ValueError: Shape of passed values is (39, 1), indices imply (39, 7)ValueError:传递值的形状为 (39, 1),索引暗示 (39, 7)
【发布时间】:2020-05-21 05:13:28
【问题描述】:

嘿,我正在尝试对我的数据进行简单的逻辑回归,即收益 (y) 与市场指数 (x)。

import numpy as np

from sklearn import metrics

data = pd.read_excel ('Datafile.xlsx', index_col=0)

#split dataset into features and target variable
col_features = ['Market Beta','Value','Size','High-Yield Spread','Term Spread','Momentum','Bid-Ask Spread']
target=['Return']
x = data[col_features] #features
y = data[target] #target

#split x and y into training and testing datasets
from sklearn.model_selection import train_test_split
x_train, y_train, x_test, y_test = train_test_split (x, y, test_size = 0.25, random_state = 0)
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
y_train = np.argmax(y_train, axis=1)
logreg.fit(x_train, y_train)

y_pred = logreg.predict(x_test)

我得到的错误是 ValueError: 传递值的形状是 (39, 1),索引意味着 (39, 7)

谢谢。

【问题讨论】:

    标签: python pandas numpy scikit-learn


    【解决方案1】:

    你只是混淆了 train_test_split 结果的顺序,所以 x_test 和 y_train 被交换了。正确的顺序应该是这样的:

    x_train, x_test, y_train, y_test = train_test_split(x, y, ...
    

    【讨论】:

      猜你喜欢
      • 2018-10-15
      • 2021-12-05
      • 1970-01-01
      • 2020-05-15
      • 2019-01-27
      • 2023-03-28
      • 2020-01-12
      • 2021-10-31
      • 2021-05-12
      相关资源
      最近更新 更多