1
$\begingroup$

I'm trying to set stratify = True, but somehow jupyter notebook says 'name 'y' is not defined'

X_train, X_test, y_train, y_test = train_test_split(
    loan.drop('Loan_Status', axis=1), 
    loan['Loan_Status'], test_size=0.2, random_state=0, stratify=y)

Can anyone tell me what is the proper way to do it?

I'm using Scikit-learn v0.19.1 and have tried to set stratify = True / y / 2 but none of them worked.

$\endgroup$
4
$\begingroup$

You need to pass an array containing the class-labels (or whatever the criterion for stratifying is) as an argument to stratify.

In your case, the answer is probably loan['Loan_Status'].values.

$\endgroup$
1
  • $\begingroup$ It works! and I have understood the problem! thanks a lot :) $\endgroup$ – Hồng Ngọc Trương May 21 '18 at 15:52
1
$\begingroup$

The parameter to stratify needs to be defined, ie, y has to be defined first.

X = loan.drop('Loan_Status', axis=1)
y = loan['Loan_Status']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0, stratify=y)
$\endgroup$

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.