gpt4 book ai didi

matlab - 在 MATLAB 中采样数据

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

我有两条数据。一个是实际的 fulldata,它是 49625x6 数值数据的数据集,另一个是 target_class 名为 Book2 的数据的索引,它是 49625x1。

Book2 有六个名称(字符串)一遍又一遍地重复以匹配全数据集条目。我想使用 Book2 从全数据中提取 1,000 个样本,其中 1000 个样本中有 25% 是“蓝色”,75% 是“红色”,然后将其包含在名为 sampledata 的新子样本中。

如何在 MATLAB 中实现这一点?

伪代码:

从 Book2 中选择 250 个蓝色样本,不确定如何“选择”250 个随机“蓝色”样本bluesample = indX(Book2, :)Book2(indX, :) 不确定。

从 Book2 中选择 750 个红色样本,再次不确定如何“选择”750 个随机“红色”样本redsample = indX(Book2, ;)Book2(indX, :) 在这里再次不确定。

将蓝色和红色样本组合成子样本。

subsample = join(bluesample, redsample)

找到子样本的索引并从全数据创建样本数据:

sampledata = subsample(indX(fulldata), :) This line is probably wrong

这是两个数据集的图像:

Enter image description here

Book2 中的每一行都与全数据中的行相匹配。我正在尝试使用 Book2 从全数据中选择一定数量的“正常”和一定数量的“不正常”(是的,我知道它们的名称不恰当)数据,因为 Book2 是全数据的索引,包含类标签。

所以就我的数据集而言,这样说可能更容易:

Choose 250 random samples of the string "normal." from Book2 and log the row number.
Choose 750 random samples of the string "not normal." from Book2 and log the row number.
Combine the two random samples of row numbers together.
Make a new dataset (1000x6) using the combined row numbers (above) of fulldata.

最佳答案

使用 strmatch 提取“正常”记录:

normIdx = strmatch('normal.', Book2);
normalSubset = fulldata(normIdx, :);

然后为了生成 250 个随机非重复整数的列表,我用谷歌搜索了“非重复随机整数的 matlab 列表”,并从 the first result :

p = randperm(size(normalSubset , 1));
p = p(1:250)-1;

现在获取随机选择的 250 条正常记录

normalSample = normalSubset (p, :);

normalSample将是 250 x 6。现在对“不正常”做同样的事情。得到 notNormalSample (750 x 6) 然后合并然后得到

sample = [normalSample ; notNormalSample ]

所以在sample所有的法线都会出现在非法线之前,如果你想把它们混在一起使用randperm()再次:

sample = sample(randperm(1000), :);

关于matlab - 在 MATLAB 中采样数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13355372/

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