0

I have an array of 75 integer values, and I'm trying to replace values in the array with strings if they are within a certain range of values.

All values in the array are between 0 and 75. I want to replace all values between 0-24 with a particular string, all values between 25-49 with a different string, and all values 50+ with a third string.

I'm trying to do this with numpy,

setosa = 'Iris-setosa '
versicolor = 'Iris-versicolor '
virginica = 'Iris-virginica '
arr_predicted1 = np.array(arr_indices).reshape(1, 75)

arr_predicted2 = np.where(arr_predicted1 < 25, setosa, arr_predicted1)
arr_predicted2 = np.where((arr_predicted1 > 24) & (arr_predicted1 < 50), 
versicolor, arr_predicted1)
arr_predicted2 = np.where(arr_predicted1 > 49, virginica, arr_predicted1)

Issue I'm facing is that it only does the third np.where command, where it replaces all values that are > 49 with the virginica string like I wanted, but it seems to ignore the previous two commands where I want to replace the other two ranges with their respective strings (setosa and versicolor).

Is there a way to do this? Or perhaps create a new array where the strings correspond to the value ranges I want, essentially replacing the values with strings but creating a new array?

1
  • 1
    You overwrite the arr_predicted2 every time, that is why it only does the last one. Commented Jun 8, 2018 at 9:53

2 Answers 2

2

try numpy.select:

import numpy as np

conditions = [arr_predicted1 < 25, (arr_predicted1 > 24) & (arr_predicted1 < 50), arr_predicted1 > 49]
choice = [setosa, versicolor, virginica]


arr_predicted2 = np.select(condtions, choice)
Sign up to request clarification or add additional context in comments.

Comments

1

You overwrite the arr_predicted2 every time, that is why it only does the last one:

setosa = 'Iris-setosa '
versicolor = 'Iris-versicolor '
virginica = 'Iris-virginica '
arr_predicted1 = np.array(arr_indices).reshape(1, 75)

arr_predicted2 = np.where(arr_predicted1 < 25, setosa, arr_predicted1)
arr_predicted3 = np.where((arr_predicted2 > 24) & (arr_predicted2 < 50), 
    versicolor, arr_predicted2)
arr_predicted4 = np.where(arr_predicted3 > 49, virginica, arr_predicted3)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.