繁体   English   中英

我可以使用 numpy 优化我的随机数生成器吗?

[英]Can i optimize my random numbers generator using numpy?

这段代码np.random.rand(1000, 2)基本上给出了大群数字

在此处输入图像描述

但是有没有办法让这些随机数在 4 个间隔之间生成,以便我可以实现类似这4 个数字集群的东西?

在此处输入图像描述

请注意,output 格式(列表列表)对我很重要

output 示例:

np.random.rand(4, 2)

 [[0.20481852 0.39206741]
  [0.76406832 0.81779067]
  [0.94912136 0.9966882 ]
  [0.07224877 0.95471273]]

您可以使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM