Replace a string numpy array with a number Replace a string numpy array with a number numpy numpy

Replace a string numpy array with a number


Use factorize:

a = pd.factorize(z)[0].tolist()print (a)[0, 0, 0, 0, 1, 1, 1, 2, 2, 2]

Or numpy.unique:

a = np.unique(z, return_inverse=True)[1].tolist()print (a)[0, 0, 0, 0, 1, 1, 1, 2, 2, 2]


you can use a dictionary:

my_dict = {'Iris-setosa': 0, 'Iris-versicolor': 1, 'Iris-virginica': 2}

then use list comprehension:

z = [my_dict[zi] for zi in z]


Are you trying to count the number of occurrence as you are trying to do logistic regression?

If yes, you can use the following as well.

import collectionsz = ['Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa','Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor','Iris-virginica', 'Iris-virginica', 'Iris-virginica']print (collections.Counter(z))

It will print as below:

Counter({'Iris-setosa': 4, 'Iris-versicolor': 3, 'Iris-virginica': 3})

If you want to print in another way, you can do the following:

import collectionsz = ['Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa','Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor','Iris-virginica', 'Iris-virginica', 'Iris-virginica']for item in collections.Counter(z):    print(str(item)+ ' ' + str(collections.Counter(z)[item]))

The output will be

Iris-setosa 4Iris-versicolor 3Iris-virginica 3