Craig Weiss
Craig Weiss

Reputation: 31

How do you center N*M matrix in Python using sklearn

I have a N by M array where N corresponds to the number of points in an M dimensional space. I would like to center these points by subtracting the mean point using the learn library.

Upvotes: 2

Views: 8091

Answers (1)

Andre Holzner
Andre Holzner

Reputation: 18675

you don't need sklearn for this, you'll use numpy (which is also used by scikit-learn). Here is an example for N = 2 and M = 3:

import numpy as np
points = np.array([
   [1.,2.,3.],   # 1st point
   [4.,5.,6.]]   # 2nd point
)

meanPoint = points.mean(axis = 0)

# subtract mean point
points -= meanPoint

Upvotes: 4

Related Questions