Okay Todd, In both results, I got the proper values.. for me indices are important also counting. From your code: let's say function--> sort_out(data): x , f = sort_out( data ) The data type: x in ndarray and x[ i ]--> int64 type(f) --> ' list ' type( f[ 0 ] ) --> ' tuple ' type( f[ 0][0] ) --> 'ndarray' type( f[ 0 ][ 0 ][ 0] ) --> 'int64' How do you think to avoid diversity if data type in this example? I think it is not necessary to get diverse dtype as well as more than 1D array.. ?? Среда, 17 апреля 2013, 11:53 +02:00 от Sebastian Berg <sebastian@sipsolutions.net>:
On Wed, 2013-04-17 at 13:32 +0400, Happyman wrote:
Hi Todd, Greaaat thanks for your help.. By the way, the first one (I think) is much simpler.. I tested it and ,of course, it is 1D, but it is also a good idea to consider it for Ndimensional. I prefer the first one! Do you you think first version is okay to use?
If you are only interested in the count, using np.bincount should be much faster then the list comprehension with "==". Of course that gives you a count of zero for all indexes that do not exist. But even then I very much expect that filtering those out afterwards will be faster unless your "indexes" can be arbitrary large. Of course bincount loses the order information, so if you need that, you can only replace the second step with it.
- Sebastian
Среда, 17 апреля 2013, 11:02 +02:00 от Todd < toddrjen@gmail.com >: On Wed, Apr 17, 2013 at 10:46 AM, Todd < toddrjen@gmail.com > wrote: x,i=numpy.unique(y, return_inverse=True)
f=[numpy.where(i==ind) for ind in range(len(x))]
A better version would be (np.where returns tuples, but we don't want tuples):
x,i=numpy.unique(y, return_inverse=True) f=[numpy.where(i==ind)[0] for ind in range(len(x))]
You can also do it this way, but it is much harder to read IMO:
x=numpy.unique(y) f=numpy.split(numpy.argsort(y), numpy.nonzero(numpy.diff(numpy.sort(y)))[0]+1)
This version figures out the indexes needed to put the values of y in sorted order (the same order x uses), then splits it into sub-arrays based on value. The principle is simpler but the implementation looks like clear to me.
Note that these are only guaranteed to work on 1D arrays, I have not tested them on multidimensional arrays
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion