简体   繁体   中英

ValueError: Cannot have number of folds n_folds=3 greater than the number of samples: 2

I can't figure out why am I getting this error, because I explicitly set cv=2, so how n_fold could be equal to 3? (I am using python 2 with anaconda)

import numpy as np
from sklearn.cross_validation import cross_val_score
from sklearn.linear_model import LogisticRegressionCV

classifier = LogisticRegressionCV(scoring='roc_auc')
x = np.array([[1, 2, 3], [3, 4, 9], [4, 9, 1], [8, 0, 4], [1, 1, 4], [1.1, 2, 4]])
y = np.array([True, False, True, False, True, False])
cross_val_score(classifier, x, y, cv=2)

After running the code I get: ValueError: Cannot have number of folds n_folds=3 greater than the number of samples: 2

Ah, my usage of LogisticRegressionCV was completely incorrect. Here is the valid one:

import numpy as np
from sklearn.linear_model import LogisticRegressionCV

classifier = LogisticRegressionCV(scoring='roc_auc', cv=2)
classifier.store_cv_values = True
x = np.array([[1, 2, 3], [3, 4, 9], [4, 9, 1], [8, 0, 4], [1, 1, 4], [1.1, 2, 4]])
y = np.array([True, False, True, False, True, False])
classifier.fit(x, y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM