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

  • SEARCH
  • Home
  • 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 9240671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:13:50+00:00 2026-06-18T08:13:50+00:00

(This question is similar to Numpy averaging with multi-dimensional weights along an axis ,

  • 0

(This question is similar to Numpy averaging with multi-dimensional weights along an axis, but more complicated.)

I have a numpy array, d, d.shape=(16,3,90,144), and a numpy array of weights, e, e.shape=(16,3). I want to take a weighted average of a along axis 1 using e. So the output should be a numpy array with shape (16,90,144). I can accomplish this with a list comprehension:

np.array([np.average(d[n], weights=e[n], axis=0) for n in range(16)])

But as in the previous question, I would like to avoid having to convert from a list back to a numpy array. This case is more complicated than the previous question because the weights aren’t the same each time (i.e. weights=e[n], not weights=b).

Can anybody help? Thanks!

  • 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-18T08:13:51+00:00Added an answer on June 18, 2026 at 8:13 am

    It would be nice to use np.average directly. However, to do so, d and the weights e would have to have the same shape, and broadcasting is not done implicitly for you here.

    Explicitly broadcasting e (using np.broadcast_arrays) so it has same shape as d is possible, but a waste of memory. So instead of doing that, would could a peek at the source code defining numpy.average and try to reproduce the calculation:

    In [121]: d = np.random.random((16,3,90,144))
    
    In [122]: e = np.random.random((16,3))
    
    In [123]: f = e[:,:,None,None]
    
    In [124]: scl = f.sum(axis = 1)
    
    In [125]: avg = np.multiply(d,f).sum(axis = 1)/scl
    

    Here is a check that the calculation returns the same result as the list comprehension:

    In [126]: avg_lc = np.array([np.average(d[n], weights=e[n], axis=0) for n in range(d.shape[0])])
    
    In [127]: np.allclose(avg, avg_lc)
    Out[127]: True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have post the similar question before,but this time the problem is different,I got
I have a question similar to this question but it is MonoTouch specific. When
I've asked a question similar to this but thought I would ask a more
I have a very similar question to this question , but I am still
This question sounds similar to many posed here, but it's obnoxiously different. I have
This question is similar to mine, but does not contain an answer. I want
This question is similar to knockoutjs databind with jquery-ui datepicker , but instead of
This question is similar to this one, but with an extra wrinkle: Auto-removing all
This question is similar, but is about calling the function from inside the class:
This question is similar to LINQ group one type of item but handled in

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.