1
$\begingroup$

I'm trying to get familiar with the sklearn library, and now I'm trying to implement logistic regression for a dataframe containing numerical and categorical values to predict a binary target variable.
While reading some documentation I found the logistic regression should be used to predict binary variables presented by 0 and 1.
My target variable is "YES" and "NO", should I code it to 0 and 1 for the algorithm to work properly, or there is no difference?
Maybe I just didn't get the idea but can someone confirm this to me.

$\endgroup$

1 Answer 1

1
$\begingroup$

The string labels work just fine, here is an example:

from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
import numpy
X, y = load_iris(return_X_y=True)
y_string = numpy.array(['YES' if label == 1 else 'NO' for label in y])
clf = LogisticRegression(random_state=0, solver='lbfgs', multi_class='multinomial').fit(X, y_string)
y_pred = clf.predict(X[50:100, :])
print(y_pred)

Output:

['NO' 'NO' 'NO' 'YES' 'NO' 'YES' 'NO' 'YES' 'NO' 'NO' 'YES' 'NO' 'YES'
 'NO' 'NO' 'NO' 'NO' 'YES' 'YES' 'YES' 'NO' 'NO' 'YES' 'YES' 'NO' 'NO'
 'YES' 'NO' 'NO' 'YES' 'YES' 'YES' 'YES' 'YES' 'NO' 'NO' 'NO' 'YES' 'NO'
 'YES' 'YES' 'NO' 'YES' 'YES' 'YES' 'NO' 'NO' 'NO' 'YES' 'NO']

Yo can replace y_string to y for the numerical example.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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