0
$\begingroup$

How to split into train test by train_test_split of float values ? I used LabelEncoder but I have about 300K lines and when I used the cross_val I saw ValueError: The least populated class in y has only 1 member, which is too few. The minimum number of groups for any class cannot be less than 2., what is the better solution?

 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=seed )  

    from imblearn.under_sampling import RandomUnderSampler
    from collections import Counter

    rus = RandomUnderSampler(random_state=seed, return_indices=True) # (50:50)

    from sklearn import preprocessing
    lab_enc = preprocessing.LabelEncoder()
    training_scores_encoded = lab_enc.fit_transform(y_train)
    #print(training_scores_encoded)

    X_res, y_res, idx_res = rus.fit_sample(X_train, training_scores_encoded)

    from sklearn.preprocessing import StandardScaler

    scalerT = StandardScaler().fit(X_train[:,[0]])
    #scalerT = StandardScaler().fit(X[:,[0]])

    X_train[:,[0]] = scalerT.transform(X_train[:,[0]])
    X_test[:,[0]] = scalerT.transform(X_test[:,[0]])
    X_res[:,[0]] = scalerT.transform(X_res[:,[0]])

    scalerA = StandardScaler().fit(X_train[:,[idx_Amount]])
    #scalerA = StandardScaler().fit(X[:,[idx_Amount]])

    X_train[:,[idx_Amount]] = scalerA.transform(X_train[:,[idx_Amount]])
    X_test[:,[idx_Amount]] = scalerA.transform(X_test[:,[idx_Amount]])
    X_res[:,[idx_Amount]] = scalerA.transform(X_res[:,[idx_Amount]])

    from sklearn.linear_model import LogisticRegression


    models = []
    models.append(('LR', LogisticRegression()))


    # Default parameters for each model
    models

    from sklearn.model_selection import cross_val_score
    from sklearn.model_selection import StratifiedShuffleSplit

    XX = X_res
    yy = y_res

    #StratifiedKFold is used by default for cv integer
    #cv=10
    cv = StratifiedShuffleSplit(n_splits=10, test_size=0.3, random_state=seed)
results = []
names = []

for name, model in models:
    cv_results = cross_val_score(model, XX, yy, cv = cv, scoring = 'accuracy')
    results.append(cv_results)
    names.append(name)
    msg = "%s: %.2f (+/- %.2f)" % (name, cv_results.mean(), cv_results.std())
    print(msg)

fig = plt.figure()
fig.suptitle('Accuracy')
ax = fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(names)
plt.show()

results = []
names = []

from sklearn.model_selection import cross_val_score
from sklearn.model_selection import StratifiedShuffleSplit

XX = X_res
yy = y_res

#StratifiedKFold is used by default for cv integer
#cv=10
cv = StratifiedShuffleSplit(n_splits=10, test_size=0.3, random_state=seed)

for name, model in models:
    cv_results = cross_val_score(model, XX, yy, cv = cv, scoring = 'recall')
    results.append(cv_results)
    names.append(name)
    msg = "%s: %.2f (+/- %.2f)" % (name, cv_results.mean(), cv_results.std())
    print(msg)
$\endgroup$
  • $\begingroup$ Please post some code so that we can debug. $\endgroup$ – Danny Jan 6 at 5:08
0
$\begingroup$

The issue is your data violates the requirements of StratifiedShuffleSplit, specifically it's not possible to do a 70:30 split of the data and maintain the same number of distinct y values in the test and train set - most likely because you have a y value which only occurs 1 time?

Perhaps just use ShuffleSplit?

| improve this answer | |
$\endgroup$
  • $\begingroup$ In my code I had it but I omit it by mistake when deleting the unrelated lines!You can check it now! This is not the reason of my problem! $\endgroup$ – user10296606 Jan 6 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.