python - Using Numpy Array to Create Unique Array -
can create numpy array unique values in it?
myarray = numpy.random.random_integers(0,100,2500) myarray.shape = (50,50)
so here have given random 50x50 numpy array, have non-unique values. there way ensure every value unique?
thank you
update:
i have created basic function generate list , populate 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 = [] y in xrange(1, col_y + 1): row = [] x in xrange(1, col_x + 1): row.append(count) count += 1 a.append(row) del row numpyarray = numpy.array(a)
is there better way this?
thanks
the convenient way unique random sample set np.random.choice
replace=false
.
for example:
import numpy np # create (5, 5) array containing unique integers drawn [0, 100] uarray = np.random.choice(np.arange(0, 101), replace=false, size=(5, 5)) # check each item occurs once print((np.bincount(uarray.ravel()) == 1).all()) # true
if replace=false
set you're sampling must, of course, @ least big number of samples you're trying draw:
np.random.choice(np.arange(0, 101), replace=false, size=(50, 50)) # valueerror: cannot take larger sample population when 'replace=false'
if you're looking random permutation of integers between 1 , number of elements in array, use np.random.permutation
this:
nrow, ncol = 5, 5 uarray = (np.random.permutation(nrow * ncol) + 1).reshape(nrow, ncol)
Comments
Post a Comment