问题描述
我有一个长度为5的字典结构.该字典结构称为"mat_contents".该信息位于"traindata"中,其各自的标签位于"trainlabels"中.我想从给定的标签值中提取给定数量的样本.例如,来自"traindata"的60个样本(总共80个样本)的标签为"trainlabels"等于1.我在这里看到了一些示例,但它们与我的要求不同.
I have a dict structure with length 5. The dict structure is called "mat_contents". The information is located in "traindata" and their respective labels in "trainlabels". I want to extract a given number of samples from a given label value. For instance, 60 samples (out of 80) from "traindata" with label "trainlabels" equal 1. I have seen some examples in here but they are different from my request.
以此为输入示例
traindata trainlabels
a 1
b 2
c 2
d 1
e 1
f 2
如果我要提取两个trainlabels值为2的随机数据样本的结果可能是:
The result if I want to extract two random samples of traindata with trainlabels value of 2 could be:
b
f
推荐答案
labels = [k for k, v in mat_contents.items() if v == 1]
result = np.random.choice(labels, 2, replace=False)
如果numpy作为np导入,第一行从字典中提取相关标签,第二行从这些标签中选择2个元素的随机子集(不替换).
The first line extracts the relevant labels from your dictionary, and the second line chooses a random subset of 2 elements from these labels (without replacement), if numpy is imported as np.
这篇关于Python-从字典结构中的给定中减去许多样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!