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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:43:09+00:00 2026-06-17T09:43:09+00:00

I want to find block mean of a 2D array in NumPy. For simplicity,

  • 0

I want to find block mean of a 2D array in NumPy. For simplicity, let us assume that the array is as follows:

array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]])

I want to divide this array into 3 blocks of size 2×4, and then find the mean of all three blocks (so that the shape of the mean is 2×4. The first block is formed by the first 4 columns, the next one by the next 4 columns and so on. So my blocks are:

array([[0, 1, 2, 3],
       [12, 13, 14, 15]])

array([[ 4,  5,  6,  7],
       [16, 17, 18, 19]])

array([[ 8,  9, 10, 11],
       [20, 21, 22, 23]])

I can use a loop to do this but I get a feel that it would be better to first convert this array into a 3D array by reshape and then use the mean method on the 3D array along the third axis. This could be similar to this question.

Would appreciate if someone can provide me with:

1). An appropriate Pythonic command to do the block mean without even converting to 3D, if such a trick exists.

2). If not an appropriate Pythonic command to do the 2D to 3D conversion.

3). An insight whether it would be more efficient (in terms of space) to do it using a loop or by using the command above.

  • 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-17T09:43:10+00:00Added an answer on June 17, 2026 at 9:43 am

    Numpy methods are going to beat python loops almost always, so I am going to skip your 1.

    As for 2, in this particular case the following works:

    a = np.array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
                  [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]])
    a = a.reshape(2, 3, 4)
    >>> a
    array([[[ 0,  1,  2,  3],
            [ 4,  5,  6,  7],
            [ 8,  9, 10, 11]],
    
           [[12, 13, 14, 15],
            [16, 17, 18, 19],
            [20, 21, 22, 23]]])
    >>> np.mean(a, axis=1)
    array([[  4.,   5.,   6.,   7.],
           [ 16.,  17.,  18.,  19.]])
    

    The trick is in the reshape. For a general case where you want blocks of n columns, the following is an option

    a = a.reshape((a.shape[0], -1, n))
    

    Your concerns in 3 are mostly unwarranted. reshape returns a view of the original array, not a copy, so the conversion to 3D only requires altering the shape and strides attributes of the array, without having to copy any of the actual data.

    EDIT
    To be sure that reshaping does not copy the array, but returns a view, do the reshape as

    a.shape = a = a.reshape((a.shape[0], -1, n))
    

    The example in the docs goes along the lines of:

    >>> a = np.arange(12).reshape(3,4)
    >>> b = a.T
    >>> b.shape = (12,)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: incompatible shape for a non-contiguous array
    

    And in general there are only problems if you have been doing transpose, rollaxis, swapaxes or the like on your array.

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

Sidebar

Related Questions

I have a bullet that can bounce against a block. I want to find
I just want to find out if there is any way to block a
I want to find out if it is possible to create Android API's that
I want to find the first element that has appeared in previous positions in
HI, I want to find all input elements in a HTML string that has
I find myself writing code like this when I want to repeat some execution
I want to find out the column number that a td belongs to using
I have a standard scrollable content with some block elements. I want to find
I want to find all instances of an element attribute that contains a certain
I have the next question. There is part of code: $result = $this->find('all', array(

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.