【问题标题】:Separate matrix of column vectors into training and testing data in MATLAB在 MATLAB 中将列向量矩阵分离为训练和测试数据
【发布时间】:2020-03-19 01:41:40
【问题描述】:

我目前正在使用 MNIST 数据集在 MATLAB 中做一个项目。我有一个 n = 50000 的训练数据集,由 784 x 50000 的矩阵表示(50000 个大小为 784 的列向量)。

我正在尝试将我的训练和测试数据(分别为 70-30)分开,但我使用的方法有点罗嗦,而且我喜欢蛮力。既然这是 MATLAB,我相信一定有更好的方法。下面列出了我一直在使用的代码。我是 MATLAB 的新手,所以请帮忙!谢谢:)

% MNIST  - data loads into trn and trnAns, representing
% the input vectors and the desired output vectors, respectively
load('Data/mnistTrn.mat');

mnist_train = zeros(784, 35000);
mnist_train_ans = zeros(10, 35000);

mnist_test = zeros(784, 15000);
mnist_test_ans = zeros(10, 15000);

indexes = zeros(1,50000);
for i = 1:50000
    indexes(i) = i;
end
indexes(randperm(length(indexes)));

for i = 1:50000
  if i <= 35000
      mnist_train (:,i) = trn(:,indexes(i));
      mnist_train_ans(:,i) = trnAns(:,indexes(i));
    else
      mnist_test(:,i-35000) = trn(:,indexes(i));
      mnist_test_ans(:,i-35000) = trnAns(:,indexes(i));
    end
end

【问题讨论】:

    标签: matlab matrix vector neural-network artificial-intelligence


    【解决方案1】:

    我希望这有效:

    % MNIST  - data loads into trn and trnAns, representing
    % the input vectors and the desired output vectors, respectively
    load('Data/mnistTrn.mat');
    
    % Generating a random permutation for both trn and trnAns:
    perm = randperm(50000);
    
    % Shuffling both trn and trnAns columns using a single random permutations: 
    trn  = trn(:, perm);
    trnAns = trnAns(:, perm);
    
    
    mnist_train = trn(:, 1:35000);
    mnist_train_ans = trnAns(:, 1:35000);
    
    mnist_test = trn(:, 35001:50000);
    mnist_test_ans = trnAns(:, 35001:50000);
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 2010-11-25
      • 1970-01-01
      • 2020-01-11
      • 2018-12-18
      • 2011-01-04
      相关资源
      最近更新 更多