简体   繁体   中英

numpy.unique throwing error

I'm trying to use the following function:

def randomChose(bp, xsteps, ysteps, bs):
    # Number of points to be chosen
    s = int((bp * xsteps * ysteps) / (bs * bs))

    # Generating an array representing the input indexes
    indices = numpy.arange(xsteps * ysteps)

    # Resampling without replacement
    cs = npr.choice(indices, size=s, replace=False)

    f = []
    for idx in cs:
        nb = indices[max(idx-(bs*bs/2), 0):min(idx+(bs*bs/2)+1, xsteps*ysteps)]
        f.append(nb)
    f = numpy.array(f).flatten()
    fix = numpy.unique(numpy.array(f))

    return fix

Which takes as parameter a number bp, the data dimension xsteps * ysteps and a % bs.

What I want to do is to choose a number of valid indexes considering some neighborhood in this image.

However, I keep receiving error when calling numpy.unique , not always, though:

ValueError                                Traceback (most recent call last)
<ipython-input-35-1b5914c3cbc7> in <module>()
      9     svf_y = []
     10     for s in range(samples):
---> 11         fix = randomChose(bp, xsteps, ysteps, bs)
     12         rs_z0, rs_z1, rs_z2 = interpolate(len(fix), xsteps, ysteps, mean_rs)
     13         ds_z0, ds_z1, ds_z2 = interpolate(len(fix), xsteps, ysteps, mean_ds)

<ipython-input-6-def08adce84b> in randomChose(bp, xsteps, ysteps, bs)
     14         f.append(nb)
     15     f = numpy.array(f).flatten()
---> 16     fix = numpy.unique(numpy.array(f))
     17 
     18     return f

/usr/local/lib/python2.7/dist-packages/numpy/lib/arraysetops.pyc in unique(ar, return_index, return_inverse, return_counts)
    198         ar.sort()
    199         aux = ar
--> 200     flag = np.concatenate(([True], aux[1:] != aux[:-1]))
    201 
    202     if not optional_returns:

ValueError: all the input arrays must have same number of dimensions

This is how I call it:

nx = 57.2
ny = 24.0
xsteps = 144
ysteps = 106
bs = 5     # Block size
bp = 0.1   # Percentage of blocks
fix = randomChose(bp, xsteps, ysteps, bs)

I'm trying to understand what is wrong. As far as I understood, such method expect a ndarray as input, which is being given.

Thank you for any help.

First:

f.append(nb)

should become:

f.append(list(nb))

That makes fa list of lists, that Numpy will have a chance to convert to a Numpy array of integers, BUT ONLY if all the lists have the same length. If not, you will only have a one dimension Numpy array of lists, and flatten() will have no effect.

You may add a

print(type(f[0]))

after the flattening.

The problem is with the edges. Eg, if idx=0 ,

nb = indices[max(idx-(bs*bs/2), 0):min(idx+(bs*bs/2)+1, xsteps*ysteps)]

is going to be [0] - ie, with only one value instead of an xy coordinate. Then you will not be able to flatten your array properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM