[英]Can i optimize my random numbers generator using numpy?
您可以使用 sklearn 的make_blobs
,它随机生成各向同性的高斯斑点。 为了更好地控制中心和协方差,您可以查看multivariate_normal
:
from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=4, centers=4, n_features=2,
cluster_std=0.3, random_state=0)
print(X)
array([[2.19931109, 2.35193717],
[1.95204867, 1.30826216],
[1.9263585 , 4.15243012],
[2.84382807, 3.32650945]])
例如,对于大量随机样本:
plt.figure(figsize=(7,7))
X, y = make_blobs(n_samples=400, centers=4, n_features=2,
cluster_std=0.3, random_state=0)
plt.scatter(X[:, 0], X[:, 1])
plt.show()
示例图是两个 2D 随机变量的总和,我们可以将其称为corners
点和2D uniform clusters
。 您可以像这样使用 numpy 原语构造它们。
import numpy as np
# corner locations of 2-D uniform clusters
corners = np.random.rand(4, 2)
# scale of square 2-D uniform variates
scale = 0.3
square_rand = scale * np.random.rand(500, 2)
# select random corners for each item and add
corner_ix = np.random.choice(4, 500)
four_clust = corners[corner_ix] + square_rand
请注意将单位平方均匀减小到 0.3 的scale
因子(通过散点图中的眼球测量)。
版本 2,使图像更接近您的 QPSK 散点图 plot:
import numpy as np
import matplotlib.pyplot as plt
# scale of square 2-D uniform variates
scale = 0.3
# corner locations of 2-D uniform clusters
centers = np.array([[0.2, 0.2], [0.2, 0.8], [0.8, 0.2], [0.8, 0.8]])
corners = centers - scale/2
square_rand = scale * np.random.rand(500, 2)
# select random corners for each item and add
corner_ix = np.random.choice(4, 500)
four_clust = corners[corner_ix] + square_rand
plt.plot(four_clust[:,0], four_clust[:,1], 'x')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.