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 7859217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:49:34+00:00 2026-06-02T21:49:34+00:00

do you know a quick/elegant Python/Scipy/Numpy solution for the following problem: You have a

  • 0

do you know a quick/elegant Python/Scipy/Numpy solution for the following problem:
You have a set of x, y coordinates with associated values w (all 1D arrays). Now bin x and y onto a 2D grid (size BINSxBINS) and calculate quantiles (like the median) of the w values for each bin, which should at the end result in a BINSxBINS 2D array with the required quantiles.

This is easy to do with some nested loop,but I am sure there is a more elegant solution.

Thanks,
Mark

  • 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-02T21:49:35+00:00Added an answer on June 2, 2026 at 9:49 pm

    This is what I came up with, I hope it’s useful. It’s not necessarily cleaner or better than using a loop, but maybe it’ll get you started toward something better.

    import numpy as np
    bins_x, bins_y = 1., 1.
    x = np.array([1,1,2,2,3,3,3])
    y = np.array([1,1,2,2,3,3,3])
    w = np.array([1,2,3,4,5,6,7], 'float')
    
    # You can get a bin number for each point like this
    x = (x // bins_x).astype('int')
    y = (y // bins_y).astype('int')
    shape = [x.max()+1, y.max()+1]
    bin = np.ravel_multi_index([x, y], shape)
    
    # You could get the mean by doing something like:
    mean = np.bincount(bin, w) / np.bincount(bin)
    
    # Median is a bit harder
    order = bin.argsort()
    bin = bin[order]
    w = w[order]
    edges = (bin[1:] != bin[:-1]).nonzero()[0] + 1
    med_index = (np.r_[0, edges] + np.r_[edges, len(w)]) // 2
    median = w[med_index]
    
    # But that's not quite right, so maybe
    median2 = [np.median(i) for i in np.split(w, edges)]
    

    Also take a look at numpy.histogram2d

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick one; I know a solution, but I'm looking for something more elegant if
I have a postgres database that I want to know some quick stats. For
I have a quick question here. I know that the cakePHP find('first') function returns
I want my program always know all of the mountpoints. After a quick google
I'm having a low-brainwave day... Does anyone know of a quick & elegant way
Quick little question... I know that sometimes in other languages libraries have part of
I have a quick regex question that I figured someone might know off the
Does anyone know is there is a quick way to get all the records
I know the quick and dirty way of dividing by 60 to get minute,
Do you know a quick way to implement method(s) from an Interface to a

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.