您发布的链接说:“我们得到了一个‘批’百个随机数据点,从我们的训练集”。在我的例子中,我使用了一个全局函数(不像你的例子中的方法),所以在语法上会有所不同。
在我的函数中,您需要传递想要的样本数和数据数组。
下面是正确的代码,从而确保样品有正确的标签:
import numpy as np
def next_batch(num, data, labels):
'''
Return a total of `num` random samples and labels.
'''
idx = np.arange(0 , len(data))
np.random.shuffle(idx)
idx = idx[:num]
data_shuffle = [data[ i] for i in idx]
labels_shuffle = [labels[ i] for i in idx]
return np.asarray(data_shuffle), np.asarray(labels_shuffle)
Xtr, Ytr = np.arange(0, 10), np.arange(0, 100).reshape(10, 10)
print(Xtr)
print(Ytr)
Xtr, Ytr = next_batch(5, Xtr, Ytr)
print('\n5 random samples')
print(Xtr)
print(Ytr)
及示范运行:
[0 1 2 3 4 5 6 7 8 9]
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
5 random samples
[9 1 5 6 7]
[[90 91 92 93 94 95 96 97 98 99]
[10 11 12 13 14 15 16 17 18 19]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]]
来源
2016-12-06 12:41:19
edo
我相信这不会按照用户的期望工作。输入Xtr和输出Ytr之间有1:1的相关性。随机化发生在每个人身上。相反,应该挑选一组随机值,然后应用于两组。 –
谢谢,我更新了我的帖子。 – edo
@edo您可以使用'data [idx]'来代替[id [i]中的数据[i]],这样您就不会再从榜单跳转到列表并再次回到ndarrays。 –