Alternate to cell array

2 次查看(过去 30 天)
Ammar
Ammar 2023-7-24
回答: Shivansh 2023-9-13
I am working with some codes and My traditional method has been to intialize an array and than put the output of the data into an array, I am doing this for matlabs classification learner and i made the mistake of using an array and hence the output is treated as induvidual features instead of one cell for each feature
clc; clear;
%% Load and truncate the signals
SigA_full = load("F2N_SR_S4_235.mat");
SigB_full = load("F2N_SR_S8_235.mat");
SigA = SigA_full.F2N_SR_S4_235(1:100000, :); % Truncate SigA
SigB = SigB_full.F2N_SR_S8_235(1:100000, :); % Truncate SigB
%% High-pass Filter
fs = 34000; % Hz
filter_freq = 1000; % adjust as per your requirement
filter_freq_norm = filter_freq/(fs/2);
siglen = 50;
p = 1;
for lidx = 1:siglen
%% Load the signal1 and signal2
signal1 = SigA(:, lidx)';
signal2 = SigB(:, lidx)';
%% High-pass Filter
signal1 = highpass(signal1, filter_freq_norm);
signal2 = highpass(signal2, filter_freq_norm);
signal = {signal1, signal2};
signal = signal{1};
%% Calculate features for each signal
% Kurtosis
kurtosisValue = kurtosis(signal);
% Mean
meanValue = mean(signal);
% Standard deviation
sdValue = std(signal);
% Variance
varianceValue = var(signal);
% Skewness
skewnessValue = skewness(signal);
%% Feature array Aggregation
num_bins = 50; % Set bins
bin_averages_signal1 = process_signal(signal1, num_bins);
bin_averages_signal2 = process_signal(signal2, num_bins);
%% Create feature array
features = [kurtosisValue, meanValue, sdValue, varianceValue, skewnessValue, bin_averages_signal1, bin_averages_signal2];
%% Assign the calculated features to the array outside of the loop
f2n3sw2(p,:) = features;
p = p + 1;
end
%%
function bin_averages = process_signal(signal, num_bins)
% Find peaks and their heights
[~, peak_heights] = findpeaks(signal);
% Differences in height between adjacent peaks
peak_height_diffs = diff(peak_heights);
% Calculate the bin size
bin_size = floor(length(peak_height_diffs) / num_bins);
% Array to store the average values of each bin
bin_averages = zeros(1, num_bins);
% Loop through the peak height differences, calculate the average for each bin, and store it in the bin_averages array
for i = 1:num_bins
start_idx = (i - 1) * bin_size + 1;
end_idx = i * bin_size;
bin_data = peak_height_diffs(start_idx:end_idx);
bin_averages(i) = mean(bin_data);
end
end
in this example as you can see me putting the features in a array the feature list is ok until bin_averages_signal1, bin_averages_signal2 where based on the bins it is 50 new cells the issue with this is classfication learner will take it as 50 unique features.
An example for what i want to do is something like this
It would be ok to run the code separately but i would prefer the feature files to be one file instead of having to combine them later as it would be ineffecicent.
  2 个评论
Image Analyst
Image Analyst 2023-7-24
I'm having a little bit of trouble following. What variable or columns of variables are your predictors, and what vector is your response (ground truth classification) variable? Please show this screenshot after you have loaded up the variables from your workspace.
Ammar
Ammar 2023-7-25
@Image Analyst I am rusty in utilizing the classfication learner and machinelearning overall, so i use another code to do the labelling and label what the predictoes represent. As I have too many files, each with a different use case.
the code that has been sent is use for feature extraction and than in another version i combine them with my other data files and give them label such motion or very near motion. As for labelling the predictors i dont label and instead leave them as is, this is where i realized my mistake.
My feature array that i generated such as the example of the 1s and 0s is a custom code designed to get a binary output from 2 signals but it has lenght of 500, 1s and 0s, and as such when inputting into the classfication learner i realized they need to be put in one cell or else it is taken as 500 induvidual predictors which based of doing a weights analysis i relaized is not the right approach I had assumed that the predictors would be learning the pattern of the binary output.
same goes for the code provided above
features = [kurtosisValue, meanValue, sdValue, varianceValue, skewnessValue, bin_averages_signal1, bin_averages_signal2];
where while not lableded train feature 1 - 5 without labelling the predictors i knew what they were, and bin_averages_signal1 would represent values 6 to 55. But now i feel its the wrong way to do it and the predictor should be in one cell and not 6 to 55 hence i wanted to figure it out.
attached below is the way i combine the files and assign labels to them
BigFeatureArrayVNM = [vnm1sw1; vnm2sw1; vnm3sw1; vnm4sw1; vnm5sw1; vnm6sw1; vnm7sw1; vnm8sw1; vnm9sw1; vnm10sw1];
Unrecognized function or variable 'vnm1sw1'.
BigFeatureArrayMo = [n2f1sw1; n2f2sw1; n2f3sw1; n2f4sw1; n2f5sw1; fm1sw1; fm2sw1; f2n1sw1; f2n2sw1; f2n3sw1];
siglen = 500;
VNM = cellstr('HH');
for l = 1:siglen
VNM(:,l) = cellstr('Very Near motion');
end
Mo = cellstr('HH');
for l = 1:siglen
Mo(:,l) = cellstr('Motion');
end
FinalFeatVNM.Sig = BigFeatureArrayVNM;
FinalFeatVNM.Labels = VNM;
FinalFeatMo.Sig = BigFeatureArrayMo;
FinalFeatMo.Labels = Mo;
TrainArr.Sig = [BigFeatureArrayVNM BigFeatureArrayMo ];
TrainArr.Labels = [VNM Mo];
TrainFeatures = TrainArr.Sig';
trainingLabels = TrainArr.Labels';
AllFeats = [array2table(TrainFeatures) array2table(trainingLabels)];

请先登录,再进行评论。

回答(1 个)

Shivansh
Shivansh 2023-9-13
Hi Ammar,
I understand that you are working on a classification problem and the output results are not correct because of labelling of predictors in dataset. It seems that you want to combine multiple feature arrays with their corresponding labels into a single training dataset for the classification learner.
You can use the following steps to write a script for creating your dataset:
  1. Initialize empty arrays for ‘TrainFeatures’ and ‘trainingLabels’ before the loop.
  2. Inside the loop, concatenate the current feature array with ‘TrainFeatures’ using the vertical concatenation operator ‘vertcat’.
  3. Concatenate the current labels array with ‘trainingLabels’ using ‘vertcat’ as well.
  4. After the loop, create a table from ‘TrainFeatures’ using ‘array2table’.
  5. Concatenate ‘TrainFeaturesTable’ with ‘trainingLabels’ using ‘horzcat’ to create the final training dataset:
The AllFeats contains the combined feature arrays and their corresponding labels in a format suitable for training with the classification learner.
Here's the updated code snippet:
TrainFeatures = [];
trainingLabels = [];
% Loop through each feature array and labels
for i = 1:num_files
currentFeatureArray = % Load the current feature array
currentLabels = % Load the corresponding labels
% Concatenate current feature array with TrainFeatures
TrainFeatures = vertcat(TrainFeatures, currentFeatureArray);
% Concatenate current labels with trainingLabels
trainingLabels = vertcat(trainingLabels, currentLabels);
end
% Convert TrainFeatures to a table
TrainFeaturesTable = array2table(TrainFeatures);
% Concatenate TrainFeaturesTable with trainingLabels
AllFeats = horzcat(TrainFeaturesTable, array2table(trainingLabels));
You can use the ‘AllFeats’ table as your training dataset in the classification learner, where the features and labels are properly combined and organized.
You can refer to following documentations for the functions used in the code:
  1. vertcat: Concatenate arrays vertically - MATLAB vertcat - MathWorks India
  2. horzcat: Concatenate arrays horizontally - MATLAB horzcat - MathWorks India
  3. array2table: Convert homogeneous array to table - MATLAB array2table - MathWorks India
Hope it helps!

类别

Help CenterFile Exchange 中查找有关 Single-Rate Filters 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by