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

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


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

Explanation:

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):

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)

See the function train inside the error statement.

Solution:

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.


I got the same error in another context (sklearn train_test_split) and the reason was simply that I had passed a positional argument as keyword argument which led to misinterpretation in the called function.


A variation on the first answer - another reason you could get this is if a column name in your data is the same as an attribute/method of the object containing the data.

In my case, I was trying to access the column "count" in the dataframe "df" with the ostensibly legal syntax df.count.

However count is considered an attribute of pandas dataframe objects. The resulting name collision creates the (rather befuddling) error.