0
$\begingroup$

I'm learning how to implement and evaluate a Logistic Regression Model, for this I need to change the values of my array from strings to 0 & 1.

I have the following numpy ndarray as a result of a DataFrame.values call ['PAIDOFF', 'COLLECTION', 'COLLECTION', 'PAIDOFF', 'PAIDOFF', 'PAIDOFF', ...] I would like to know how can I change the values like: 'PAIDOFF' to 0 and 'COLLECTION' to 1

Any help will be much appreciated

$\endgroup$

1 Answer 1

2
$\begingroup$
import numpy as np

a = np.array(['PAIDOFF', 'COLLECTION', 'COLLECTION', 'PAIDOFF'])

f = lambda x: 1 if x == "COLLECTION" else 0

np.fromiter(map(f,a),dtype=np.int)

Alternative:

np.where(a == "COLLECTION",1,0)
$\endgroup$
1
  • $\begingroup$ So simple, thank you very much! $\endgroup$ Jan 7, 2021 at 22:12

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.