【问题标题】:too many values to unpack (expected 2)?太多的值需要解压(预期为 2)?
【发布时间】:2019-12-28 01:22:45
【问题描述】:

我有数据框可以使用,当我测试/拆分数据时,会弹出此错误消息

要解压的值太多(预计 2 个)

我只是将我的目标列 Global 设置为 y 值,并将其余列设置为 train_test_split 的 X。不知道从哪里开始解决这个问题

X = df[['Year_of_Release', 'Critic_Score', 'Critic_Count',
       'User_Score', 'User_Count', 'Platform_PC', 'Platform_PS3',
       'Platform_PS4', 'Platform_Wii', 'Platform_X360',
       'Platform_XOne', 'Genre_Action', 'Genre_Adventure', 'Genre_Fighting',
       'Genre_Misc', 'Genre_Platform', 'Genre_Puzzle', 'Genre_Racing',
       'Genre_Role-Playing', 'Genre_Shooter', 'Genre_Simulation',
       'Genre_Sports', 'Genre_Strategy', 'Rating_E', 'Rating_E10+', 'Rating_M',
       'Rating_RP', 'Rating_T']]

y = df[['Global']]

print(X.shape)
print(y.shape)

from sklearn.model_selection import train_test_split

X_train, X_test = train_test_split(X, y, train_size=0.8, test_size=0.2, random_state=42)


X_train, X_val = train_test_split(X_train, train_size=0.8, test_size=0.2, random_state=42)

target = 'Global'
y_train = X_train[target]
y_val = X_val[target]
y_test = X_test[target]

X_train = X_train.drop(columns=target)
X_val = X_val.drop(columns=target)
X_test = X_test.drop(columns=target)

X_train.shape, y_train.shape, X_val.shape, y_val.shape, X_test.shape, y_test.shape

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-75-d3fede999d7b> in <module>()
      1 from sklearn.model_selection import train_test_split
      2 
----> 3 X_train, X_test = train_test_split(X, y, train_size=0.8, test_size=0.2, random_state=42)
      4 
      5 

ValueError: too many values to unpack (expected 2)

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    来自 sklearn 的 train_test_split 返回 4 个值 X_Train 和 y_train 以及 X_test y_test。 参考官方文档here

    您还应该指定 test sizetrain size

    【讨论】:

      【解决方案2】:
      X = df[['Year_of_Release', 'Critic_Score', 'Critic_Count',
             'User_Score', 'User_Count', 'Platform_PC', 'Platform_PS3',
             'Platform_PS4', 'Platform_Wii', 'Platform_X360',
             'Platform_XOne', 'Genre_Action', 'Genre_Adventure', 'Genre_Fighting',
             'Genre_Misc', 'Genre_Platform', 'Genre_Puzzle', 'Genre_Racing',
             'Genre_Role-Playing', 'Genre_Shooter', 'Genre_Simulation',
             'Genre_Sports', 'Genre_Strategy', 'Rating_E', 'Rating_E10+', 'Rating_M',
             'Rating_RP', 'Rating_T']]
      
      y = df[['Global']]
      
      print(X.shape)
      print(y.shape)
      
      from sklearn.model_selection import train_test_split
      
      X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        • 1970-01-01
        • 1970-01-01
        • 2020-12-09
        • 2018-02-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多