Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting TypeError: Singleton array array(None, dtype=object) cannot be considered a valid collection

I am using different cross validation method. I first use k fold method on my code and it was perfectly well but when I use repeatedstratifiedkfold method it gives me this error

TypeError: Singleton array array(None, dtype=object) cannot be considered a valid collection.

Can any one help me in this regard. Below is the minimal code that produces the issue.

import numpy as np
from sklearn.model_selection import RepeatedStratifiedKFold


ss = RepeatedStratifiedKFold(n_splits=5, n_repeats=2, random_state=0)

X = np.random.rand(100, 5)
y = np.random.rand(100, 1)

for train_index, test_index in ss.split(X):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]

Here is the full trackback -

start
Traceback (most recent call last):

  File "C:\Users\full details of final year project\AZU\test_tace_updated.py", line 81, in <module>
    main()

  File "C:\Users\AZU\test_tace_updated.py", line 54, in main
    for train, test in ss.split(X):

  File "C:\Users\anaconda3\lib\site-packages\sklearn\model_selection\_split.py", line 1201, in split
    for train_index, test_index in cv.split(X, y, groups):

  File "C:\Users\anaconda3\lib\site-packages\sklearn\model_selection\_split.py", line 731, in split
    y = check_array(y, ensure_2d=False, dtype=None)

  File "C:\Users\anaconda3\lib\site-packages\sklearn\utils\validation.py", line 63, in inner_f
    return f(*args, **kwargs)

  File "C:\Users\anaconda3\lib\site-packages\sklearn\utils\validation.py", line 667, in check_array
    n_samples = _num_samples(array)

  File "C:\Users\anaconda3\lib\site-packages\sklearn\utils\validation.py", line 202, in _num_samples
    raise TypeError("Singleton array %r cannot be considered"

TypeError: Singleton array array(None, dtype=object) cannot be considered a valid collection.
like image 837
Rao Kiran Avatar asked Aug 09 '26 15:08

Rao Kiran


1 Answers

I am inclined to say that this is a bug in sklean (definitely not sure about that) but if you also include y in your split function, the issue seems to go away. The following code runs as expected.

import numpy as np
from sklearn.model_selection import RepeatedStratifiedKFold

ss = RepeatedStratifiedKFold(n_splits=5, n_repeats=2, random_state=0)

X = np.random.rand(100, 5)
y = np.zeros(100)

for train_index, test_index in ss.split(X, y):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]

It's weird because according to the documentation, y should be optional.

like image 130
Ananda Avatar answered Aug 12 '26 15:08

Ananda