I have a numpy array of classification of text in the form of String array, i.e.
y_train = ['A', 'B', 'A', 'C',...]
. I am trying to apply SKlearn multinomial NB algorithm to predict classes for entire dataset.
I want to convert the String classes into integers to be able to input into the algorithm and convert ['A', 'B', 'A', 'C', ...]
into ['1', '2', '1', '3', ...]
I can write a for loop to go through array and create a new one with int classifiers but is there a direct function to achieve this
Another way is use the astype('category').cat.codes of the dataframe to convert the string values into number
X=df[['User ID', 'Gender', 'Age', 'EstimatedSalary']]
X['Gender']=X['Gender'].astype('category').cat.codes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With