Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9236555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:19:15+00:00 2026-06-18T07:19:15+00:00

I have a numpy array of floats/ints and want to map its elements into

  • 0

I have a numpy array of floats/ints and want to map its elements into their ranks.

If an array doesn’t have duplicates the problem can be solved by the following code

In [49]: a1
Out[49]: array([ 0.1,  5.1,  2.1,  3.1,  4.1,  1.1,  6.1,  8.1,  7.1,  9.1])

In [50]: a1.argsort().argsort()
Out[50]: array([0, 5, 2, 3, 4, 1, 6, 8, 7, 9])

Now I want to extend this method to arrays with possible duplicates, so that duplicates are mapped to the same value. For example, I want array a

a2 = np.array([0.1, 1.1, 2.1, 3.1, 4.1, 1.1, 6.1, 7.1, 7.1, 1.1])

to be mapped to either

0 1 4 5 6 1 7 8 8 1

or to

0 3 4 5 6 3 7 9 9 3

or to

0 2 4 5 6 2 7 8.5 8.5 2

In the first/second case we map duplicates to the minimum/maximum rank among them if we just apply a2.argsort().argsort().
The third case is just the average of first two cases.

Any suggestions?

EDIT (efficiency requirements)

In the initial description I forgot to mention about time requirements. I am seeking for solution in terms of numpy/scipy functions which will let to avoid “pure python overhead”. Just to make it clear, consider the solution proposed by Richard which actually solves the problem but quite slow:

def argsortdup(a1):
  sorted = np.sort(a1)
  ranked = []
  for item in a1:
    ranked.append(sorted.searchsorted(item))
  return np.array(ranked)

In [86]: a2 = np.array([ 0.1,  1.1,  2.1,  3.1,  4.1,  1.1,  6.1,  7.1,  7.1,  1.1])

In [87]: %timeit a2.argsort().argsort()
1000000 loops, best of 3: 1.55 us per loop

In [88]: %timeit argsortdup(a2)
10000 loops, best of 3: 25.6 us per loop

In [89]: a = np.arange(0.1, 1000.1)

In [90]: %timeit a.argsort().argsort()
10000 loops, best of 3: 24.5 us per loop

In [91]: %timeit argsortdup(a)
1000 loops, best of 3: 1.14 ms per loop

In [92]: a = np.arange(0.1, 10000.1)

In [93]: %timeit a.argsort().argsort()
1000 loops, best of 3: 303 us per loop

In [94]: %timeit argsortdup(a)
100 loops, best of 3: 11.9 ms per loop

It is clear from the analysis above that argsortdup is 30-50 times slower than a.argsort().argsort(). The main reason is the use of python loops and lists.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:19:17+00:00Added an answer on June 18, 2026 at 7:19 am

    After upgrading to a latest version of scipy as suggested @WarrenWeckesser in the comments, scipy.stats.rankdata seems to be faster than both scipy.stats.mstats.rankdata and np.searchsorted being the fastet way to do it on larger arrays.

    In [1]: import numpy as np
    
    In [2]: from scipy.stats import rankdata as rd
       ...: from scipy.stats.mstats import rankdata as rd2
       ...: 
    
    In [3]: array = np.arange(0.1, 1000000.1)
    
    In [4]: %timeit np.searchsorted(np.sort(array), array)
    1 loops, best of 3: 385 ms per loop
    
    In [5]: %timeit rd(array)
    10 loops, best of 3: 109 ms per loop
    
    In [6]: %timeit rd2(array)
    1 loops, best of 3: 205 ms per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a numpy array and want to convert it into an ITK image
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like
I have a numpy array like this a = np.array(1) Now if I want
A toy-case for my problem: I have a numpy array of size, say, 1000:
I have a numpy array A such that A.shape[axis] = n+1. Now I want
I want to save an array with floats to at file with numpy.savetxt(). The
I have an array 1200*1200. Some of its values are zero. I want to
I have a numpy array (actually imported from a GIS raster map) which contains
I have a list of numbers which I put into a numpy array: >>>
I have a numpy array a , a.shape=(17,90,144) . I want to find the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.