我有下面列出的输出的代码和图片,但我想在已绘制的特定标准偏差内从这些球体中抽取随机样本。变量 sdwith 用于在代码中设置它以用于线网的输出。random.multivariate_normal 进行抽样,但您不能设置要从中抽样的最大概率或标准偏差数。这在 numpy 中是否可行,或者最好的方法是什么?
def sphere(r=1.0,npts=(20,20)):
"""Create a simple sphere.
Returns x, y, z coordinates for the sphere
"""
phi=linspace(0,pi,npts[0])
theta=linspace(0,2*pi,npts[1])
phi, theta = meshgrid(phi,theta)
x = r*sin(phi)*cos(theta)
y = r*sin(phi)*sin(theta)
z = r*cos(phi)
return x, y, z
pet_bar = load('data_mod.npy')
num_vowels = 10
sdwidth = 1
npts = 20
cov_mat = zeros((num_vowels,3,3))
means_mat = zeros((num_vowels,3))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
colors = ['g','b','r','c','m','y','k','0.5']
for i in range(10):
#change below to use different parts of the dataset
indices = intersect1d(where( pet_bar[:,0] == 1)[0], where( pet_bar[:,2] == i+1)[0])
# determines whether take all or >0 just takes unanimously heard correctly
indices = intersect1d(indices, where(pet_bar[:,3] > 0.5)[0])
pet_bar_anal = pet_bar[indices,-3:]
cov_mat[i] = cov(pet_bar_anal, rowvar=False)
means_mat[i] = mean(pet_bar_anal, axis=0)
x, y, z = sphere(1, (npts,npts))
ap = vstack((x.flatten(),y.flatten(),z.flatten()))
d, v = eig(cov_mat[i])
n = dot(v, (sdwidth*sqrt(d))*eye(3,3))
out = dot(n,ap)
bp = out + tile(means_mat[i], (npts**2,1)).T
xp = reshape(bp[0], x.shape)
yp = reshape(bp[1], x.shape)
zp = reshape(bp[2], x.shape)
ax.plot_wireframe(array(xp),array(yp),array(zp), rstride=2, cstride=2, color=colors[i%len(colors)])
ax.set_xlim3d((0,ax.get_xlim3d()[1]))
ax.set_ylim3d((0,ax.get_ylim3d()[1]))
ax.set_zlim3d((0,ax.get_zlim3d()[1]))
plt.draw()
plt.show()