gpt4 book ai didi

python - Numpy 是否给出了意想不到的结果?

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:33 24 4
gpt4 key购买 nike

我有一个数据集 XX.shape 产生 (10000, 9)。我想使用以下代码选择 X 的子集:

X = np.asarray(np.random.normal(size = (10000,9)))
train_fraction = 0.7 # fraction of X that will be marked as train data
train_size = int(X.shape[0]*train_fraction) # fraction converted to number
test_size = X.shape[0] - train_size # remaining rows will be marked as test data
train_ind = np.asarray([False]*X.shape[0])
train_ind[np.random.randint(low = X.shape[0], size = (train_size,))] = True # mark True at 70% of the places

问题是 np.sum(train_ind) 不是预期值 7000。相反,它给出了随机值,如 5033 等。

我最初认为 np.random.randint(low = X.shape[0], size = (train_size,)) 可能是罪魁祸首。但是当我执行 np.random.randint(low = X.shape[0], size = (train_size,)).shape 时,我得到 (7000,)

我哪里出错了?

最佳答案

采用np.random.choice(np.arange(0,X.shape[0]), size = train_size, Replace = False)

问题是,np.random.randint 不会注入(inject),基本上数字 1 可能会出现两次。这意味着索引 1 将被设置为 True 两次,而另一个索引不会被设置为 True

np.random.choice 函数确保每个数字最多出现一次(如果您设置replace = False

关于python - Numpy 是否给出了意想不到的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45611279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com