【问题标题】:ERROR : While using splitting the dataset for providing to machine learning algorithms错误:使用拆分数据集以提供给机器学习算法时
【发布时间】:2017-05-16 07:32:07
【问题描述】:

这很奇怪,因为此错误仅发生在特定的 3 列中,并且与其他列一起正常工作。

错误:

Traceback (most recent call last):
  File "/version1/analyze.py", line 447, in <module>
    cv_results = model_selection.cross_val_score(model, X_train, Y_train,cv=kfold, scoring=scoring) 

 File "/usr/lib64/python2.7/site-packages/sklearn/model_selection/_validation.py", line 140, in cross_val_score
    for train, test in cv_iter)

fac = 1. / (n_samples - n_classes)

ZeroDivisionError: float division by zero

我的密码:

validation_size = 0.20
seed = 10
X_train, X_validation, Y_train, Y_validation =   model_selection.train_test_split(X, Y, test_size=validation_size, random_state=seed)
seed = 10
scoring = 'accuracy'

kfold = model_selection.KFold(n_splits=10, random_state=seed)

cv_results = model_selection.cross_val_score(model, X_train, Y_train,cv=kfold, scoring=scoring)  #error occurs here

仅当我从数据集中选择几个特定列时才会出现上述错误,它适用于其他列!

所有列都有相同的数据和类似的值。

 #code for Split-out validation dataset
   array = dataset.values

   if field == "rh":
     X = array[:,0:8]

   elif field == "rm":
     X = array[:,0:8]

   elif field == "wh":
     X = array[:,0:8]

   elif field == "wm":
     X = array[:,0:8]

   else :
    print"wrong field"

  if field == "rh":
      Y = array[:,0]     #works fine , even for 4,5,6,7 it works 

  elif field == "rm":      #gives the above error only for 1,2,3
      Y = array[:,1]      #gives the above error

  elif field == "wh":     #gives the above error
      Y = array[:,2]

  elif field == "wm":     #gives the above error
      Y = array[:,3]

  else :
      print"wrong field"

这是我的数据集:

      index,1column,2 column,3column,….,8column
         0,238,240,1103,409,1038,4,67,0
         1,41,359,995,467,1317,8,71,0
         2,102,616,1168,480,1206,7,59,0
         3,0,34,994,181,1115,4,68,0
         4,88,1419,1175,413,1060,8,71,0
         5,826,10886,1316,6885,2086,263,119,0
         6,88,472,1200,652,1047,7,64,0
         7,0,322,957,533,1062,11,73,0
         8,0,200,1170,421,1038,5,63,0
         9,103,1439,1085,1638,1151,29,66,0
        10,0,1422,1074,4832,1084,27,74,0
        11,1828,754,11030,263845,1209,10,79,0
        12,340,1644,11181,175099,4127,13,136,0
        13,71,1018,1029,2480,1276,18,66,1
        14,0,3077,1116,1696,1129,6,62,0

             “”””””
             ‘”””””

总共 105 条数据记录

但1列不会出现上述错误,即Y=1列时, 但是当我选择任何其他列 2 、 3 或 4 时,也会发生上述相同的错误。

【问题讨论】:

  • n_samples - n_classes 不能为零
  • @VMRuiz 是的,但是如何处理呢?
  • 尝试更改validation_size 或添加更多示例。如果不运行代码或不了解数据集,很难猜测
  • @VMRuiz 如果我更改 Validation_size 没有用,我已经在问题中添加了我的数据集。
  • 你有原因:1,2,3和4列中不同值的数量与validation_split(105 * 0.2)后的样本数量相同。您能否对每一列进行不同的计数来检查这一点?

标签: python machine-learning runtime-error cross-validation


【解决方案1】:

n_samples 是数据集中的行,n_classes 是标签数组中唯一的类标签。发生上述错误是因为数据集中没有标签类!

【讨论】:

    猜你喜欢
    • 2020-12-31
    • 2021-06-02
    • 2012-03-14
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 2017-05-16
    • 1970-01-01
    相关资源
    最近更新 更多