如何规范化 Python 中矩阵的值?

How to normalize values from a matrix in Python?

我在 python 代码中创建了带有噪声的信号。我将它们存储在一个数组中 - new_array,大小为 (10,17,199,1)。索引 0 表示用于创建信号的频率范围,索引 1 表示由具有不同添加噪声的频率创建的振幅(范围从 0.5 到 2.2 的标准偏差,增量为 0.1),索引 2 和 3 表示时域即数组的长度。

目前,这是我在规范化时生成的...

.

我的问题是如何使信号正常化?当我使用 preprocessing.normalize 时,信号不是我所期望的。有人有什么主意吗?我在下面发布了我的代码。

import numpy as np 
from sklearn import preprocessing


def frequency_labels(s_frequency):
    L = []
    for w, f2 in enumerate(s_frequency):
        l = " {} Hz".format(f2)
        print("f1=",f2)
        L.append(l)
    return L

def time_labels(time):
    H = []
    for r,t in enumerate(time):
        h = " {} s".format(t)
        H.append(h)
    return H

def gaussian_noise(increment,len_time):
    mean = 0
    standard_deviation = np.arange(0.5,2.2,increment) 
    ## want 8096 different noise signals of different standard deviations
    sd = standard_deviation.reshape(len(standard_deviation),1)
    noise = np.empty((len(sd), (len_time), (1)), dtype=np.float16)
     
    for t, value in enumerate(sd):
         
        noise[t] = np.random.normal(mean,value,len_time).reshape(len_time,1)
        
    return(noise)


max_freq = 50
s_frequency = np.arange(20,30,1) # range of frequencies
fs = 200
time = np.arange(0,2-(1/fs),(1/fs))
amplitude = np.empty((len(time)), dtype=np.float16)
len_time = len(time)
len_frequency = len(s_frequency)
array = np.empty((len(time)), dtype=np.float16)
increment = 0.1  #0.00021
L = frequency_labels(s_frequency)
H = time_labels(time)
k = 0 
noise = gaussian_noise(increment,len_time)
new_array = np.empty((len(s_frequency),(len(noise)),len(time),(1)),dtype=np.float16)

for f1 in s_frequency:

    
    for i, t in enumerate(time):
       
      amplitude[i] = np.sin(2*np.pi*f1*t)
           
    amplitude = amplitude.reshape(len(time),1)
    #n_amplitude = preprocessing.normalize(amplitude)
    new_array[k] = np.add(noise,amplitude)

    for r in range(17):
        new_array[k,r,:,:] = preprocessing.normalize(new_array[k,r,:,:])
        
    k = k + 1

对于代码的最后一部分,我使用 (i) 将其置于 -1 和 1 的范围内,以及 (ii) 将其置于 0 和 1 的范围内

(i) new_array[k,r,:,:] = new_array[k,r,:,:]/new_array[k,r,:,:].max(axis=0)
(ii) new_array[k,r,:,:] = (new_array[k,r,:,:] - new_array[k,r,:,:].min(0))/new_array[k,r,:,:].ptp(0)
    

我在其中一个在线论坛上找到了这个解决方案。