我正在尝试创建一个函数来创建虚假数据以用于单独的分析。以下是该功能的要求。
问题1
在此问题中,您将使用 numpy 创建虚假数据。在下面的单元格中,函数 create_data 接受 2 个参数“n”和“rand_gen”。
- “rand_gen”参数是伪随机数生成器。我们使用伪随机数生成器来产生相同的结果。
- 使用伪随机生成器的 numpy.random.randn 函数创建长度为 n 的 numpy 数组并返回该数组。
这是我创建的函数。
def create_data(n, rand_gen):
'''
Creates a numpy array with n samples from the standard normal distribution
Parameters
-----------
n : integer for the number of samples to create
rand_gen : pseudo-random number generator from numpy
Returns
-------
numpy array from the standard normal distribution of size n
'''
numpy_array = np.random.randn(n)
return numpy_array
这是我在函数上运行的第一个测试。
create_data(10, np.random.RandomState(seed=23))
我需要输出是这个精确的数组。
[0.66698806, 0.02581308, -0.77761941, 0.94863382, 0.70167179,
-1.05108156, -0.36754812, -1.13745969, -1.32214752, 1.77225828]
我的输出仍然是完全随机的,我不完全理解 RandomState 调用试图用种子来创建上面的数组而不是让它完全随机。我知道我需要在函数中使用 rand_gen 变量,但我不知道在哪里,我认为这是因为我只是不明白它要做什么。
最佳答案
定义 numpy_array = rand_gen.randn(n)
关于python - 在 Python 中创建假数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52880256/