【问题标题】:Showing Error while using train_test_Split method to on Kaggle kernal. Help appreciated在 Kaggle 内核上使用 train_test_Split 方法时显示错误。帮助表示赞赏
【发布时间】:2019-04-18 03:26:14
【问题描述】:

任何人都可以解释什么是错误以及如何避免? 相同的代码实际上可以在我的 Jupyter 和 Spider 环境中使用。还 我不明白为什么这不适用于 kaggle 内核。

下面是我的代码。

# Prepare X and y objects for sikitlearn
X = df.iloc[:, 1:5].values
y = df.iloc[:,-1].values

# Splitting the Dataset into the training set and the test set
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.3, random_state =4)

错误详情:

        ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)

<ipython-input-49-5647b5a0596a> in <module>()
      1 # Splitting the Dataset into the training set and the test set
      2 from sklearn.model_selection import train_test_split
----> 3 X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.3, random_state =4)

/opt/conda/lib/python3.6/site-packages/sklearn/model_selection/_split.py in train_test_split(*arrays, **options)
   2100 
   2101     return list(chain.from_iterable((safe_indexing(a, train),
-> 2102                                      safe_indexing(a, test)) for a in arrays))
   2103 
   2104 

/opt/conda/lib/python3.6/site-packages/sklearn/model_selection/_split.py in <genexpr>(.0)
   2100 
   2101     return list(chain.from_iterable((safe_indexing(a, train),
-> 2102                                      safe_indexing(a, test)) for a in arrays))
   2103 
   2104 

/opt/conda/lib/python3.6/site-packages/sklearn/utils/__init__.py in safe_indexing(X, indices)
    183                                    indices.dtype.kind == 'i'):
    184             # This is often substantially faster than X[indices]
--> 185             return X.take(indices, axis=0)
    186         else:
    187             return X[indices]

TypeError: take_nd() got an unexpected keyword argument 'axis'

【问题讨论】:

  • 您好像遇到过this issue。尝试更新你的 scikit-learn、numpy 和 pandas。显示df 的数据类型。 pandas DataFrame 中是否有任何分类类型的列?
  • 我找到了解决方案,我刚刚从我的代码中删除了“.values”,它对我有用。

标签: python python-3.x scikit-learn kernel


【解决方案1】:

可能是因为你的目标变量的类型不是分类的,你必须把它改成标签。

【讨论】:

    【解决方案2】:

    这是一个尚未解决的错误。

    要使代码正常工作,您的响应变量不应是分类的。

    X_train, X_test, y_train, y_test = train_test_split(data_frame, y_was_categorical.astype(str), stratify=y_was_categorical, test_size=0.2)
    

    这是适用于我的代码

    【讨论】:

      猜你喜欢
      • 2022-11-24
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      相关资源
      最近更新 更多