How do I standardize a matrix?

后端 未结 5 711
误落风尘
误落风尘 2021-01-03 23:25

Basically, take a matrix and change it so that its mean is equal to 0 and variance is 1. I\'m using numpy\'s arrays so if it can already do it it\'s better, but I can implem

5条回答
  •  再見小時候
    2021-01-03 23:47

    Use sklearn.preprocessing.scale.

    http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.scale.html

    Here is an example.

    >>> from sklearn import preprocessing
    >>> import numpy as np
    >>> X_train = np.array([[ 1., -1.,  2.],
    ...                     [ 2.,  0.,  0.],
    ...                     [ 0.,  1., -1.]])
    >>> X_scaled = preprocessing.scale(X_train)
    >>> X_scaled
    array([[ 0.  ..., -1.22...,  1.33...],
           [ 1.22...,  0.  ..., -0.26...],
           [-1.22...,  1.22..., -1.06...]])
    

    http://scikit-learn.org/stable/modules/preprocessing.html#standardization-or-mean-removal-and-variance-scaling

提交回复
热议问题