How do I standardize a matrix?

后端 未结 5 719
误落风尘
误落风尘 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:38

    from sklearn.preprocessing import StandardScaler
    
    standardized_data = StandardScaler().fit_transform(your_data)
    

    Example:

    >>> import numpy as np
    >>> from sklearn.preprocessing import StandardScaler
    
    >>> data = np.random.randint(25, size=(4, 4))
    >>> data
    array([[17, 12,  4, 17],
           [ 1, 16, 19,  1],
           [ 7,  8, 10,  4],
           [22,  4,  2,  8]])
    
    >>> standardized_data = StandardScaler().fit_transform(data)
    >>> standardized_data
    array([[ 0.63812398,  0.4472136 , -0.718646  ,  1.57786412],
           [-1.30663482,  1.34164079,  1.55076242, -1.07959124],
           [-0.57735027, -0.4472136 ,  0.18911737, -0.58131836],
           [ 1.24586111, -1.34164079, -1.02123379,  0.08304548]])
    

    Works well on large datasets.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题