1

I can't modify the actual value of a numpy array in a loop. My code is the following :

labels_class = np.copy(labels_train)
for label in labels_class:
  labels_class[label] = 1 if (label == classifier) else 0

labels_class - is just a numpy array of size N and of values [0, 39]. The value of labels_class[label] is correct(==modified) in the loop, but outside of the loop labels_classremains unchanged.

I have also tried nditer, did not work :

 for label in np.nditer(labels_class, op_flags=['readwrite']):
      label = 1 if (label == classifier) else 0

In the reference, it is said that "to actually modify the element of the array, x should be indexed with the ellipsis"

How do I do that? What is the syntax?

4
  • 2
    Perhaps you meant to enumerate through the numpy array. Simply iterating over an iterable yields the elements of the iterable, not the indexes of the elements. Commented Apr 6, 2015 at 20:05
  • I meant accessing the elements of the array in the loop and modifying actual values of it. Commented Apr 6, 2015 at 20:09
  • To actually change label you have to use something like label[:]=... or label[...] = .... Review the nditer tutorial if you want to go that route. Commented Apr 6, 2015 at 21:45
  • docs.scipy.org/doc/numpy/reference/… 'modifying array values with nditer' Commented Apr 6, 2015 at 21:51

3 Answers 3

7

Your iterator is not creating indices, but the actual elements in the array

for label in labels_class

In the above label is not an index, but the actual element you are trying to change

You can do something like this:

for i, label in enumerate(labels_class):
     labels_class[i] = 1 if (label == classifier) else 0
Sign up to request clarification or add additional context in comments.

3 Comments

This is the solution! Thank you. However, I must say that in for label in labels_class label is not the actual element but a copy.
You don't need the 1 if ... else 0 part, you can just leave label == classifier.
@Oleksandra, python variables are different than c variables, for example. Python ints and floats are immutable so there is no difference between a copy and the actual element.
5

The syntax for modifying array values with nditer is demonstrated at

http://docs.scipy.org/doc/numpy/reference/arrays.nditer.html#modifying-array-values

a = np.arange(6).reshape(2,3)
for x in np.nditer(a, op_flags=['readwrite']):
    x[...] = 2 * x

'x should be indexed with the ellipsis' refers to the x[...].

Indexing with enumerate is perfectly fine as well. But this is the way to do it with nditer. See later sections in the nditer page about using flags=['f_index'].

When iterating over arrays you need to clearly understand the difference between variables which are indexes, scalars, or array elements which can be modified. x = 1 is not the same as A[i]= 1 or x[...]=1.

Comments

0

how about this?

labels_class[label_class == classifier] = 1
labels_class[label_class != classifier] = 0

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.