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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:05:48+00:00 2026-06-04T21:05:48+00:00

I want to center multi-dimensional data in a n x m matrix ( <class

  • 0

I want to center multi-dimensional data in a n x m matrix (<class 'numpy.matrixlib.defmatrix.matrix'>), let’s say X . I defined a new array ones(645), lets say centVector to produce the mean for every row in matrix X. And now I want to iterate every row in X, compute the mean and assign this value to the corresponding index in centVector. Isn’t this possible in a single row in scipy/numpy? I am not used to this language and think about something like:

centVector = ones(645)
for key, val in X:
    centVector[key] = centVector[key] * (val.sum/val.size)

Afterwards I just need to subtract the mean in every Row:

X = X - centVector

How can I simplify this?
EDIT: And besides, the above code is not actually working – for a key-value loop I need something like enumerate(X). And I am not sure if X - centVector is returning the proper solution.

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

    First, some example data:

    >>> import numpy as np
    >>> X = np.matrix(np.arange(25).reshape((5,5)))
    >>> print X
    [[ 0  1  2  3  4]
     [ 5  6  7  8  9]
     [10 11 12 13 14]
     [15 16 17 18 19]
     [20 21 22 23 24]]
    

    numpy conveniently has a mean function. By default however, it’ll give you the mean over all the values in the array. Since you want the mean of each row, you need to specify the axis of the operation:

    >>> np.mean(X, axis=1)
    matrix([[  2.],
            [  7.],
            [ 12.],
            [ 17.],
            [ 22.]])
    

    Note that axis=1 says: find the mean along the columns (for each row), where 0 = rows and 1 = columns (and so on). Now, you can subtract this mean from your X, as you did originally.

    Unsolicited advice

    Usually, it’s best to avoid the matrix class (see docs). If you remove the np.matrix call from the example data, then you get a normal numpy array.

    Unfortunately, in this particular case, using an array slightly complicates things because np.mean will return a 1D array:

    >>> X = np.arange(25).reshape((5,5))
    >>> r_means = np.mean(X, axis=1)
    >>> print r_means
    [  2.   7.  12.  17.  22.]
    

    If you try to subtract this from X, r_means gets broadcast to a row vector, instead of a column vector:

    >>> X - r_means
    array([[ -2.,  -6., -10., -14., -18.],
           [  3.,  -1.,  -5.,  -9., -13.],
           [  8.,   4.,   0.,  -4.,  -8.],
           [ 13.,   9.,   5.,   1.,  -3.],
           [ 18.,  14.,  10.,   6.,   2.]])
    

    So, you’ll have to reshape the 1D array into an N x 1 column vector:

    >>> X - r_means.reshape((-1, 1))
    array([[-2., -1.,  0.,  1.,  2.],
           [-2., -1.,  0.,  1.,  2.],
           [-2., -1.,  0.,  1.,  2.],
           [-2., -1.,  0.,  1.,  2.],
           [-2., -1.,  0.,  1.,  2.]])
    

    The -1 passed to reshape tells numpy to figure out this dimension based on the original array shape and the rest of the dimensions of the new array. Alternatively, you could have reshaped the array using r_means[:, np.newaxis].

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

Sidebar

Related Questions

I want to use Mountain Lion's new feature Notification Center. In NSUserNotification.h I found
I want to center a div in the document body in jquery. I have
I want to center a background image. There is no div used, this is
So I want to center a text View inside a linear layout where I
I want to set this progressbar in the center of Web view: <FrameLayout android:layout_width=fill_parent
I want to be able to center zoom on all my polylines for my
I want to set the text in Scroll label at center, can any one
I want to get the Name and Description in the center of the columns..
I want to have my list (nav) align to the center of an image
I want my activity (a normal activity with a digital clock at the center

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.