1

I want to replace elements in a np.array, for instance:

arr = np.array([4,5,6,7,3])

I want to replace every element which meets my condition with a given value, for example 3<=x<=5. And replace it with a random number such as randint(90, 99).

Therefore, my expected output is:

[91 94  6  7 92]

I tried something like this:

out = np.where(arr>4, randint(90, 99), arr)

But I have 2 probelms:
1) I can't specity an interval
2) I can't get 3 random numbers - but only one

Alex
  • 1,447
  • 7
  • 23
  • 48
  • What was your attempt? This kind of thing is bread and butter of numpy and would be covered in most tutorials – roganjosh Mar 14 '19 at 20:03
  • I'm not really familiar with numpy, but this seems pretty simple. Can you please show what you've tried? – Christian Dean Mar 14 '19 at 20:03
  • Second answer solves your problem – roganjosh Mar 14 '19 at 20:04
  • Use this: `arr[np.argwhere((3<=arr)&(arr<=5))] = 99` – Vlad Mar 14 '19 at 20:14
  • @Vlad how can I assign a randomInt ? if I try arr[np.argwhere((3<=arr)&(arr<=5))] = randint(90, 99) it won't assign 3 different integers, but only 1 random.. – Alex Mar 14 '19 at 20:21
  • You want to assign the same random number for each element that meets condition or each element that meets condition is assigned to number drawn from some distribution? – Vlad Mar 14 '19 at 20:23
  • 1
    @vlad no need for `np.argwhere`. `arr[(3 <= arr) & (arr <= 5)] = 99`. The OP has now decided to change the assigned value to some random number; the question continues to expand even after being closed – roganjosh Mar 14 '19 at 20:25
  • To get your three random values: `arr[(3 <= arr) & (arr <= 5)] = np.random.randint(90, 99, 3)`. Numpy `random` will allow you to specify a `size` to give more than 1 result, which will then be "unpacked" into the places where your filter is True. Of course, this issue can easily expand again when we don't know _how many_ values will be True, but we can't keep expanding a single question to cover these things. That's not how SO works. – roganjosh Mar 14 '19 at 20:28
  • @roganjosh, you're absolutely right. Thanks for pointing that out. – Vlad Mar 14 '19 at 20:31

1 Answers1

-4

Use a loop, it's simple:

j=0
for i in arr:
    if(3<=i and i<=5)
        arr[j]=99
        j+=1
Karls
  • 731
  • 7
  • 17
  • I though there was some build-in function for this.. – Alex Mar 14 '19 at 20:04
  • 4
    No, _don't_ use a python loop for this. If you do that, you may as well be using python lists. This defeats the purpose of arrays. The whole point of numpy is being able to vectorize this kind of thing and get orders of magnitude speed up over regular python. – roganjosh Mar 14 '19 at 20:05
  • 2
    Neither simple nor appropriate for numpy... – cs95 Mar 14 '19 at 20:08
  • Just because you will not use _explict_ loops, doesn't mean you will not use it at all. – Karls Mar 14 '19 at 20:24
  • 3
    No, but I'd rather my loop runs in C code that can also duck under the GIL and runs several hundred times faster. – roganjosh Mar 14 '19 at 20:26