生成数据
我们将从在二维网格中生成两个二维斑点开始。一个斑点为正,另一个为负。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import Normalize
def normal_pdf(x, mean, var):
return np.exp(-(x - mean)**2 / (2*var))
## 生成斑点所在的空间
xmin, xmax, ymin, ymax = (0, 100, 0, 100)
n_bins = 100
xx = np.linspace(xmin, xmax, n_bins)
yy = np.linspace(ymin, ymax, n_bins)
## 生成斑点。值的范围大致为 -0.0002 到 0.0002
means_high = [20, 50]
means_low = [50, 60]
var = [150, 200]
gauss_x_high = normal_pdf(xx, means_high[0], var[0])
gauss_y_high = normal_pdf(yy, means_high[1], var[0])
gauss_x_low = normal_pdf(xx, means_low[0], var[1])
gauss_y_low = normal_pdf(yy, means_low[1], var[1])
weights = (np.outer(gauss_y_high, gauss_x_high)
- np.outer(gauss_y_low, gauss_x_low))
## 我们还将创建一个灰色背景,像素将渐隐到其中
greys = np.full((*weights.shape, 3), 70, dtype=np.uint8)