Hi I have an array of distances `a= np.array([20.5 ,5.3 ,60.7 ,3.0 ], 'double')`

and I need the indices of the sorted array (for example `[3, 1, 0, 2]`

, for `a.sort()`

). There is a function in numpy to do that?

Thanks

Yes, there's the `x = numpy.argsort(a)`

function or `x = numpy.ndarray.argsort(a)`

method. It does exactly what you're asking for. You can also call `argsort`

as a method on an `ndarray`

object like so: `a.argsort()`

.

Here's a link to the documentation: http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html#numpy.argsort

Similar Questions

How can I define _SOME CODE_ in the next code fragment in order to get the results shown below? vector = numpy.array([a,b,c,d]) for i in xrange(4): print vector[_SOME CODE_ using i] It sould give me

This is an interview question: given an integer x and a sorted array a[] of N distinct integers, design a linear-time algorithm to determine if there exists two distinct indices i and j such that a[i]

I have a bi-dimensional np.array like x = np.array([[1,2], [4,5], [4,6], [5,4], [4,5]]) now I want the indices where x is equal to [4,5] (-> [1, 4]). The operator == works in a different way: x ==

I had to make a histogram of a given array, the frequency of the numbers in the array should be marked with *. My program works, the problem is that the array isn't sorted correctly if there is a nega

I need to perform some calculations a large list of numbers. Do array.array or numpy.array offer significant performance boost over typical arrays? I don't have to do complicated manipulations on the

I want to take some indices from the elements of a numpy.array. This is what I've got so far: import numpy as np a = np.array(range(20)) a.shape=4,5 #~ [[ 0 1 2 3 4] #~ [ 5 6 7 8 9] #~ [10 11 12 13 14

Related, but different, IMHO: (1) numpy: frequency counts for unique values in an array (2) Using Numpy arrays as lookup tables Setup: import numpy as np from scipy.stats import itemfreq x = np.array

I have a 2D masked array of values that I need to sort from lowest to highest. For example: import numpy as np # Make a random masked array >>> ar = np.ma.array(np.round(np.random.normal(50,

I want to get the c array as result, but I don't know how: import numpy as np a = xrange(10) b = np.array([3,2,1,9]) c is made of elements of a that are not in b: c = np.array([0,4,5,6,7,8])

I'm wondering how to get a random numpy array of integers using DNA bases. I have the basic numpy function working, but I can't accomplish this without transforming the numpy array into a list of stri

I would like to resample a numpy array as suggested here Resampling a numpy array representing an image however this resampling will do so by a factor i.e. x = np.arange(9).reshape(3,3) print scipy.nd

If I have the following array: [5, 1, -7, 3, 6, 8, 0, -1, -3] By sorting it I get [-7, -3, -1, 0, 1, 3, 5, 6, 8] That's fine, but what I want is the keys for the array when sorted. This: [2, 8, 7, 6,

I need to read complex numbers from a text file into a numpy array. My question is similar to this one Writing and reading complex numbers using numpy.savetxt and numpy.loadtxt however, the solution h

I want to find a fast way (without for loop) in Python to assign reoccuring indices of an array. This is the desired result using a for loop: import numpy as np a=np.arange(9, dtype=np.float64).reshap

I have a NumPy record array of floats: import numpy as np ar = np.array([(238.03, 238.0, 237.0), (238.02, 238.0, 237.01), (238.05, 238.01, 237.0)], dtype=[('A', 'f'), ('B', 'f'), ('C', 'f')]) How can

This is a bit fiddly. I'm looking to get an array (or matrix) that is circular, such that Let: a = [1,2,3] Then I would like a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 1 a[4] = 2 Etc for all index values of

This works in MATLAB: >> p = [1, 0, 2, 4, 3, 6, 5]; >> p(p+1) ans = 0 1 2 3 4 5 6 Is there a way to do the same thing in NumPy? I can't figure out how: >>> p = mat([1, 0, 2, 4, 3

When I print a numpy array, I get a truncated representation, but I want the full array. Is there any way to do this? Examples: >>> numpy.arange(10000) array([ 0, 1, 2, ..., 9997, 9998, 9999

Is there any way to prevent pandas from changing the default print format for numpy arrays? With plain numpy, I get: >>> numpy.array([123.45, 0.06]) array([ 1.23450000e+02, 6.00000000e-02])

I want to print numpy arrays nicer, using the indices into the array [0 1] indexes row zero and column one: Suppose we have a big numpy array x = np.arange(400*8000).reshape(400,8000).astype(float) N

I am a little bit confuses reading the documentation of argmin function in numpy. It looks like it should do the job: Reading this Return the indices of the minimum values along an axis. I might as

Is there's a way to get the indices properly from a Pymel/Maya API function? I know Pymel has a function called getEdges() however according to their docs this get's them from the selected face, howev

If b is a 2x2 np.ndarray and the following assignment is performed, what does numpy do in the background, i.e. does it convert the list[100, 100] first to a numpy array or does it directly use the lis

I have two arrays in numpy. The first is a 2d array, which can be thought of as a list of vectors. The second is a 1d array, which can be thought of as a list of indices into the 2d array. I want to s

I am trying to relate the python/numpy indices of two arrays with different sizes, but I cannot pass index one from the small array to the large array through a subroutine. For example, I have two nu

Thanks to the help in a previous question, I've got the following code returning the expected results with find_all. Original array (sorted by votes[0]): votes_array = [{votes=>[13], id=>4

I have a numpy array of this form [[-0.77947021 0.83822138] [ 0.15563491 0.89537743] [-0.0599077 -0.71777995] [ 0.20759636 0.75893338]] I want to create numpy array of this form [x1, x2, x1*x2] where

I have this numpy array where the values in each row will always be sorted and monotonically increasing: a = np.array([[1, 2, 3, 4, 8], [2, 5, 6, 7, 8], [5, 7, 11, 12, 13]]) and I want to search for

This question is closely related to How to split an array according to a condition in numpy? but I am looking for a more general way to split an array given an unknown number of indices: import numpy

How to obtain new array (new) from original array (x) by calculating mean as follows: new = [[mean(1,3), mean(1,3), mean(1,3), mean(1,3), mean(1,3)],[mean(2,4),mean(2,4),mean(2,4),mean(2,4),mean(2,4)]

I have a 2d numpy array. I am looking for the indices of the first occurance of a specific element in each row. The output will be an (n x 2) array, where n is the number of rows and every entry conta

I have a numpy array that I would like to share between a bunch of python processes in a way that doesn't involve copies. I create a shared numpy array from an existing numpy array using the sharedmem

I'm observing some odd behavior. Here is the snippet of code: >>> import numpy as np >>> a = [[1, .3], [0, .5], [2, .23]] >>> b = np.array(a.sort()) >>> b array(Non

I have two index arrays and I want to return all the indices in between, like a slice function, manually it would look like this: ind1 = np.array([2,6]) ind2 = np.array([2,3]) final = np.array([[2,2,2

I'd like to segment my image using numpy's label and then based on the number of indices found in each label remove those which satisfy my criteria. For example if an image with regions in it that I'd

I have an array of numbers A, and I want to create a new array B representing the indicies of A after A is reordered from smallest to largest. For example: A = [50 10 60 90 30]; The reordered A would

I am trying to create a structured array in the below format: import numpy as np x = np.array([(2009, (('USA', 10.), ('CHN', 12.))), (2010, (('BRA', 10.), ('ARG', 12.)))], dtype=[('year', '<i4'), [

I have a numpy array with a shape of: (11L, 5L, 5L) I want to calculate the mean over the 25 elements of each 'slice' of the array [0, :, :], [1, :, :] etc, returning 11 values. It seems silly, but I

I'm performing data mining on images. Each pixel is treated as a data point. The image is read as follows: im=Image.open('lena.bmp') im=numpy.array(im) print im.shape Depending on whether the image i

I want to extract the second and the 3rd to the fifth columns of the NumPy array, how would I go about it? A = array([[0, 1, 2, 3, 4, 5, 6], [4, 5, 6, 7, 4, 5, 6]]) A[:, [1, 4:6]] This obviously does

Now I have an numpy array X with certain column names, format and length. How can I set all the values to 0 (or empty) in this array, without deleting the format/names etc.?

My question is an extension of that one: Immutable numpy array? This code prints False, because even though a is immutable, b is not. a = np.arange(10) a.setflags(write=False) b = a[1:] b[1] = -1 prin

What is an easy way in Java to sort the array of elements maintaining information about indexes where the elements were stored in the original array? Is there a built-in function? There are few ways I

import os, glob, numpy as np files = glob.glob('*.jpg') indices = np.array([i for i,j in enumerate(files)]) selected_indices = np.random.choice(indices,500,replace=False) print files[selected_indices]

For example, to sort an array of complex numbers first by real part, then by magnitude of imaginary part, and then with negative imaginary parts first: def order(a): return a.real, abs(a.imag), sign(a

I have an array of boolean values and need to randomly select a specific quantity of indices for values which are true. What is the most efficient way to generate the array of indices? For instance, B

How do I put values from a sorted list and array into a dictionary and determine how many time numbers repeat themselves? This is my code so far. from numpy import * from random import * lista=[] for

I am trying to get the values of an ndarray using an array of indices. Suppose a is the target array and b is the array of indices. Then: a = np.asarray([[[1,2,3]],[[2,3,4]],[[3,4,5]]]) b = np.asarray

I have an array whose size and values keep changing during execution all the time. I want to do that with minimum possible performance overhead. Instead ochanging array size I simply have member int v

I am a newbie in Numpy. I want to find indices of elements equal to a set of values. For example, I know this works: >>> x = np.array([[0, 1], [2, 3], [4, 5]]) >>> x array([[0, 1], [