josh1234 - 4 months ago 35

Python Question

Can you create a numpy array with all unique values in it?

`myArray = numpy.random.random_integers(0,100,2500)`

myArray.shape = (50,50)

So here I have a given random 50x50 numpy array, but I could have non-unique values. Is there a way to ensure every value is unique?

Thank you

I have created a basic function to generate a list and populate a unique integer.

`dist_x = math.sqrt(math.pow((extent.XMax - extent.XMin), 2))`

dist_y = math.sqrt(math.pow((extent.YMax - extent.YMin),2))

col_x = int(dist_x / 100)

col_y = int(dist_y / 100)

if col_x % 100 > 0:

col_x += 1

if col_y % 100 > 0:

col_y += 1

print col_x, col_y, 249*169

count = 1

a = []

for y in xrange(1, col_y + 1):

row = []

for x in xrange(1, col_x + 1):

row.append(count)

count += 1

a.append(row)

del row

numpyArray = numpy.array(a)

Is there a better way to do this?

Thanks

Answer

The most convenient way to get a unique random sample from a set is probably `np.random.choice`

with `replace=False`

.

For example:

```
import numpy as np
# create a (5, 5) array containing unique integers drawn from [0, 100]
uarray = np.random.choice(np.arange(0, 101), replace=False, size=(5, 5))
# check that each item occurs only once
print((np.bincount(uarray.ravel()) == 1).all())
# True
```

If `replace=False`

the set you're sampling from must, of course, be at least as big as the number of samples you're trying to draw:

```
np.random.choice(np.arange(0, 101), replace=False, size=(50, 50))
# ValueError: Cannot take a larger sample than population when 'replace=False'
```

If all you're looking for is a random permutation of the integers between 1 and the number of elements in your array, you could also use `np.random.permutation`

like this:

```
nrow, ncol = 5, 5
uarray = (np.random.permutation(nrow * ncol) + 1).reshape(nrow, ncol)
```