gogurt - 8 months ago 42

Python Question

Suppose I have a two arrays:

`import numpy as np`

a = np.random.randint(0,10,10)

b = np.random.randint(0,10,10)

I want to generate another length-10 array whose i-th entry is a random integer drawn from the set (

`{0...9}`

`a[i]`

`b[i]`

Being a relative rookie when it comes to NumPy, I thought the easiest way to do this might be:

- obtain the set difference for each
`x = {0...9} - (a[i] union b[i])`

`i`

- do for each
`np.random.choice(x[i], 1)`

`i`

But I'm finding this a little tricky because I can't figure out how to map

`setdiff1d`

Answer

Here is one way:

```
In [87]: col = np.array((a, b)).T
In [88]: r = np.arange(10)
In [89]: np.ravel([np.random.choice(np.setdiff1d(r, i), 1) for i in col])
Out[89]: array([7, 8, 8, 6, 6, 8, 6, 5, 5, 6])
```

Or as a numpytonic approach:

```
In [101]: def func(x):
return np.random.choice(np.setdiff1d(r, x), 1)
.....:
In [102]: np.apply_along_axis(func, 1, col).ravel()
Out[102]: array([6, 7, 9, 6, 4, 6, 7, 4, 0, 7])
```