Search code examples
pythonnormalizationz-score

How to do zscore normalization with the same scaling factor on multiple channels in the data?


I have a list with 3-6 channels, as a multidimensional list/array. I want to zscore normalize all channels of the data, but it is important that the scaling factor is the same for all channels because the difference in mean between channels is important for my application. I have taken a look at:

https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.zscore.html

including the source code but I am not sure if it normalizes the channels individually or together. Is it possible to use scipy zscore for what I am trying to do?


Solution

  • Don't use the zscore function

    It does do each channel independently, as you suspected.

    Do it manually

    In a 2D array, numpy defaults to averaging and SD'ing across the entire 2D dataset, which makes it easy to do this manually.

    Here is an example with 3 channels.

    import numpy as np
    
    data = np.random.rand(10, 3)
    
    # One number for each, i.e. across ALL channels
    mean = np.mean(data)
    std = np.std(data)
    
    # Now it is easy to normalise all data against the common mean and SD
    data_norm = (data - mean) / std
    
    print(data_norm)