Add Gaussian Noise to Data in Python
To add Gaussian noise to the data, you can use the numpy.random.normal function to generate random numbers from a Gaussian distribution, and then add them to the original data. Here is an example code:
import numpy as np
def add_gaussian_noise(data, mean, std):
noise = np.random.normal(mean, std, size=data.shape)
noisy_data = data + noise
return noisy_data
# 示例数据
data = np.array([1, 2, 3, 4, 5])
mean = 0 # 噪声的均值
std = 0.1 # 噪声的标准差
noisy_data = add_gaussian_noise(data, mean, std)
print(noisy_data)
The add_gaussian_noise function in the code above takes three parameters: the original data (data), the mean of the noise (mean), and the standard deviation of the noise (std). This function first uses the numpy.random.normal function to generate random numbers from a Gaussian distribution with the same shape as the original data (with a mean of mean and a standard deviation of std). These random numbers are then added to the original data to obtain data with Gaussian noise. Finally, the data with noise is printed as output.
Please modify the data and parameters in the sample code according to your actual needs.