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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:41:09+00:00 2026-05-27T08:41:09+00:00

I have a numpy matrix A where the data is organised column-vector-vise i.e A[:,0]

  • 0

I have a numpy matrix A where the data is organised column-vector-vise i.e A[:,0] is the first data vector, A[:,1] is the second and so on. I wanted to know whether there was a more elegant way to zero out the mean from this data. I am currently doing it via a for loop:

mean=A.mean(axis=1)
for k in range(A.shape[1]):
    A[:,k]=A[:,k]-mean

So does numpy provide a function to do this? Or can it be done more efficiently another way?

  • 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-05-27T08:41:10+00:00Added an answer on May 27, 2026 at 8:41 am

    As is typical, you can do this a number of ways. Each of the approaches below works by adding a dimension to the mean vector, making it a 4 x 1 array, and then NumPy’s broadcasting takes care of the rest. Each approach creates a view of mean, rather than a deep copy. The first approach (i.e., using newaxis) is likely preferred by most, but the other methods are included for the record.

    In addition to the approaches below, see also ovgolovin’s answer, which uses a NumPy matrix to avoid the need to reshape mean altogether.

    For the methods below, we start with the following code and example array A.

    import numpy as np
    
    A = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
    mean = A.mean(axis=1)
    

    Using numpy.newaxis

    >>> A - mean[:, np.newaxis]
    array([[-1.,  0.,  1.],
           [-1.,  0.,  1.],
           [-1.,  0.,  1.],
           [-1.,  0.,  1.]])
    

    Using None

    The documentation states that None can be used instead of newaxis. This is because

    >>> np.newaxis is None
    True
    

    Therefore, the following accomplishes the task.

    >>> A - mean[:, None]
    array([[-1.,  0.,  1.],
           [-1.,  0.,  1.],
           [-1.,  0.,  1.],
           [-1.,  0.,  1.]])
    

    That said, newaxis is clearer and should be preferred. Also, a case can be made that newaxis is more future proof. See also: Numpy: Should I use newaxis or None?

    Using ndarray.reshape

    >>> A - mean.reshape((mean.shape[0]), 1)
    array([[-1.,  0.,  1.],
           [-1.,  0.,  1.],
           [-1.,  0.,  1.],
           [-1.,  0.,  1.]])
    

    Changing ndarray.shape directly

    You can alternatively change the shape of mean directly.

    >>> mean.shape = (mean.shape[0], 1)
    >>> A - mean
    array([[-1.,  0.,  1.],
           [-1.,  0.,  1.],
           [-1.,  0.,  1.],
           [-1.,  0.,  1.]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using numpy. I have a matrix with 1 column and N rows
I have a 2D numpy array (i.e matrix) A which contains useful data interspread
I have a matrix in the type of a Numpy array. How would I
I have some data represented in a 1300x1341 matrix. I would like to split
I am trying to pass data around the numpy and boost::ublas layers. I have
Say I have a numpy matrix like so: [[ x1, x2, x3, ... ],
I am using Numpy to store data into matrices. Coming from R background, there
Let's suppose I have a numpy matrix variable called MATRIX with 3 coordinates: (x,
If I have the following matrix: import numpy ar = numpy.array((('0','1','2','3'), ('1','a','b','b'), ('2','b','c','d')), str)
I am using Python and Numpy to do some data analysis. I have 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.