1

I am new to Python. In one of my assignment question, part of the question requires us to compute the average of each element in a sub-matrix and replace each element with the mean using operators that's available in Numpy.

An example of the matrix could be

M = [[[1,2,3],[2,3,4]],[[3,4,5],[4,5,6]]]

Through some operations, it is expected to get a matrix like the following:

M = [[[2,2,2],[3,3,3]],[[4,4,4],[5,5,5]]]

I have looked at some numpy documentations and still haven't figured out, would really appreciate if someone can help.

2 Answers 2

1

You have a few different options here. All of them follow the same general idea. You have an MxNxL array and you want to apply a reduction operation along the last axis that will leave you with an MxN result by default. However, you want to broadcast that result across the same MxNxL shape you started with.

Numpy has a parameter in most reduction operations that allows you to keep the reduced dimension present in the output array, which will allow you to easily broadcast that result into the correct sized matrix. The parameter is called keepdims, you can read more in the documentation to numpy.mean.

Here are a few approaches that all take advantage of this.

Setup

avg = M.mean(-1, keepdims=1)

# array([[[2.],
#         [3.]],
# 
#        [[4.],
#         [5.]]])

Option 1
Assign to a view of the array. However, it will also coerce float averages to int, so cast your array to float first for precision if you want to do this.

M[:] = avg

Option 2
An efficient read only view using np.broadcast_to

np.broadcast_to(avg, M.shape)

Option 3
Broadcasted multiplication, more for demonstration than anything.

avg * np.ones(M.shape)

All will produce (same except for possibly the dtype):

array([[[2., 2., 2.],
        [3., 3., 3.]],

       [[4., 4., 4.],
        [5., 5., 5.]]])
Sign up to request clarification or add additional context in comments.

Comments

0

In one line of code:

M.mean(-1, keepdims=1) * np.ones(M.shape)

2 Comments

This is just a copy paste of my last option.
Sorry I didn't notice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.