单例数组数组(<function train at 0x7f3a311320d0>, dtype=object)不能被视为有效集合 [英] Singleton array array(&lt;function train at 0x7f3a311320d0&gt;, dtype=object) cannot be considered a valid collection

查看:23
本文介绍了单例数组数组(<function train at 0x7f3a311320d0>, dtype=object)不能被视为有效集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道如何解决.任何帮助非常感谢.我看到了矢量化:不是有效的集合,但不确定我是否理解这一点>

Not sure how to fix . Any help much appreciate. I saw thi Vectorization: Not a valid collection but not sure if i understood this

train = df1.iloc[:,[4,6]]
target =df1.iloc[:,[0]]

def train(classifier, X, y):
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=33)
    classifier.fit(X_train, y_train)
    print ("Accuracy: %s" % classifier.score(X_test, y_test))
    return classifier

trial1 = Pipeline([
         ('vectorizer', TfidfVectorizer()),
         ('classifier', MultinomialNB()),])

train(trial1, train, target)

错误如下:

    ----> 6 train(trial1, train, target)

    <ipython-input-140-ac0e8d32795e> in train(classifier, X, y)
          1 def train(classifier, X, y):
    ----> 2     X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=33)
          3 
          4     classifier.fit(X_train, y_train)
          5     print ("Accuracy: %s" % classifier.score(X_test, y_test))

    /home/manisha/anaconda3/lib/python3.5/site-packages/sklearn/model_selection/_split.py in train_test_split(*arrays, **options)
       1687         test_size = 0.25
       1688 
    -> 1689     arrays = indexable(*arrays)
       1690 
       1691     if stratify is not None:

    /home/manisha/anaconda3/lib/python3.5/site-packages/sklearn/utils/validation.py in indexable(*iterables)
        204         else:
        205             result.append(np.array(X))
    --> 206     check_consistent_length(*result)
        207     return result
        208 

    /home/manisha/anaconda3/lib/python3.5/site-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
        175     """
        176 
    --> 177     lengths = [_num_samples(X) for X in arrays if X is not None]
        178     uniques = np.unique(lengths)
        179     if len(uniques) > 1:

    /home/manisha/anaconda3/lib/python3.5/site-packages/sklearn/utils/validation.py in <listcomp>(.0)
        175     """
        176 
    --> 177     lengths = [_num_samples(X) for X in arrays if X is not None]
        178     uniques = np.unique(lengths)
        179     if len(uniques) > 1:

    /home/manisha/anaconda3/lib/python3.5/site-packages/sklearn/utils/validation.py in _num_samples(x)
        124         if len(x.shape) == 0:
        125             raise TypeError("Singleton array %r cannot be considered"
    --> 126                             " a valid collection." % x)
        127         return x.shape[0]
        128     else:

    TypeError: Singleton array array(<function train at 0x7f3a311320d0>, dtype=object) cannot be considered a valid collection.

 ____

不知道如何解决.任何帮助非常感谢.我看到了矢量化:不是有效的集合,但不确定我是否理解这一点>

Not sure how to fix . Any help much appreciate. I saw thi Vectorization: Not a valid collection but not sure if i understood this

推荐答案

出现这个错误是因为你的函数 train 屏蔽了你的变量 train,因此它被传递给了自己.

This error arises because your function train masks your variable train, and hence it is passed to itself.

说明:

您可以像这样定义变量火车:

You define a variable train like this:

train = df1.iloc[:,[4,6]]

然后在几行之后,你定义一个这样的方法序列:

Then after some lines, you define a method train like this:

def train(classifier, X, y):

所以实际发生的是,您之前版本的 train 已更新为新版本.这意味着 train 现在没有按照您的需要指向 Dataframe 对象,而是指向您定义的函数.在错误中它被清除.

So what actually happens is, your previous version of train is updated with new version. That means that the train now does not point to the Dataframe object as you wanted, but points to the function you defined. In the error it is cleared.

array(<function train at 0x7f3a311320d0>, dtype=object)

查看错误语句中的函数训练.

解决方案:

重命名其中之一(变量或方法).建议:将函数重命名为其他名称,例如 trainingtraining_func 或类似名称.

Rename one of them (the variable or the method). Suggestion: Rename the function to some other name like training or training_func or something like that.

这篇关于单例数组数组(<function train at 0x7f3a311320d0>, dtype=object)不能被视为有效集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆