Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [train_data, d, test_data, d_test] = readTrainingData(distribution)
- total_files = 10; % Összes fájl száma
- total_data_per_file = 1000; % Egy fájlban lévő összes adat
- if(distribution>=1)
- distribution = 0.9;
- error("Distribution can't be bigger or equal than 1.")
- end
- train_data_per_file = round(total_data_per_file*distribution);
- test_data_per_file = round(total_data_per_file*(1-distribution));
- train_data = zeros(train_data_per_file*total_files, 785); % Tanító halmaz inicializálása
- d = zeros(train_data_per_file * total_files, 10);
- d(:) = -1;
- test_data = zeros(total_files * test_data_per_file, 785); % Teszt halmaz inicializálása
- d_test = zeros(total_files * test_data_per_file, 10);
- d_test(:) = -1;
- d_counter = 1;
- d_test_counter = 1;
- for file_index = 0:(total_files - 1)
- file_name = sprintf('data%d', file_index); % Aktuális fájl neve
- fid = fopen(file_name, 'r');
- for i = 1:total_data_per_file
- t = fread(fid, [28 28], 'uchar');% Beolvasás
- % 2D mátrix átalakítása 1D vektorrá, ahol 0 van a mátrixban, ott is 0 marad, egyébként 1
- t_vector = double(t(:)'./255);
- t_vector_with_ones = [1, t_vector]; % Minden sor elejére beszúrunk egy 1-est
- if i <= train_data_per_file
- % Tanító halmazba mentés
- train_data(d_counter, :) = t_vector_with_ones;
- d(d_counter, file_index + 1) = 1;
- d_counter = d_counter + 1;
- else
- % Teszt halmazba mentés
- test_data(d_test_counter, :) = t_vector_with_ones;
- d_test(d_test_counter, file_index + 1) = 1;
- d_test_counter = d_test_counter + 1;
- end
- end
- fclose(fid);
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement