2

I am facing a very basic problem in if condition in python.

The array current_img_list is of dimension (500L,1). If the number 82459 is in this array, I want to subtract it by 1.

index = np.random.randint(0,num_train-1,500)
# shape of current_img_list is (500L,1)
# values in index array is randomized
current_img_list = train_data['img_list'][index]
# shape of current_img_list is (500L,1)

if (any(current_img_list) == 82459):
        i = np.where(current_img_list == 82459)
        final_list = i-1

Explanation of variables - train_data is of type dict. It has 4 elements in it. img_list is one of the elements with size (215375L,1). The value of num_train is 215375 and size of index is (500L,1)

Firsly I don't know whether this loop is working or not. I tried all() function and numpy.where() function but to no success. Secondly, I can't think of a way of how to subtract 1 from 82459 directly from the index at which it is stored without affecting the rest of the values in this array.

Thanks

1
  • The any and all functions only check for 'true-ness'. any returns true if any value in the sequence evaluates to true and all returns true if all items evaluate to true. Commented Jun 28, 2017 at 19:17

3 Answers 3

3

Looping over the array in Python will be much slower than letting numpy's vectorized operators do their thing:

import numpy as np

num_train = 90000   # for example
index = np.random.randint(0,num_train-1,500)
index[index == 82459] -= 1
Sign up to request clarification or add additional context in comments.

2 Comments

I have made some edit in the question (explanation of variables). Since the values in the current_img_list is randomized, how do I set the if condition for a specific value.
Exactly as I already said - index[index == 82459] will affect all items in index which are equal to 82459.
0
current_img_list = np.array([1,2,3,82459,4,5,6])
i = np.where(current_img_list == 82459)
current_img_list[i] -= 1

2 Comments

I forgot to mention that the values in current_img_list are randomized. I think I need to place an if condition for those cases when 82459 is not in the array. I am confused about what shall be the correct syntax for the abive if condition.
@Ak94 I understand that real values are random. I have initialized the example array with a known sequence. The purpose of the example is to show how to decrement those array elements that are equal to 82459. It is irrelevant how the array was obtained.
0

I'm a little confused by whats trying to be achieved here, but I'll give it a go:

If you're trying to subtract 1 from anywhere in your array that is equal to 82459, then what maybe iterate through the array, with a for loop. Each time the current index is equal to 82459, just set the number at that index -= 1.

If you need more help, please post the rest of the relevant code so I can debug.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.