How to use calculate the several means of same class

Hello.
I have a data (90 x 2857), column 2857 is a label(class).
I'm using table function.
I want to read rows of the same class from table, select N rows, calculate the average.
Could you giva an idea to make the code?
data = readtable("outfile.csv");
data.Properties.VariableNames{end} = 'label';
data{:,1:end-1} = normalize(data{:,1:end-1},1); % I belive this should be "1"
data = sortrows(data,'label'); % The data are sorted by the lables

1 Comment

I did't find how to select rows for same class randomly.
Can I get some ideas to make a code?

Sign in to comment.

 Accepted Answer

Try this
data = csvread('outfile.csv');
values = data(:,1:end-1);
labels = data(:,end);
avg = splitapply(@(x) mean(x,1), values, labels+1);
The avg matrix is 10x2856, each row corresponds to mean value of one class.

7 Comments

Thank you so much.
Using the above result, I want to calculate the euclidean distance between avg of each class and the query.
Could you explain how to compute Euclidean distance between avg of each class and several examples of the same class using for loop?
data = csvread('outfile.csv');
values = data(:,1:end-1);
labels = data(:,end);
avg = splitapply(@(x) mean(x,1), values, labels+1);
mean_class1 = avg(1,:);
query = values(1,:);
query1 = values(11,:);
query2 = values(21,:);
% calculate euclidean distance
euclidean_bend = pdist2(mean_class1, query, 'euclidean');
euclidean_bend1 = pdist2(mean_class1, query1, 'euclidean');
euclidean_bend2 = pdist2(mean_class1, query2, 'euclidean');
Check this. It calculate the distance of each class member from the average of that class
data = csvread('outfile.csv');
values = data(:,1:end-1);
labels = data(:,end);
avg = splitapply(@(x) {mean(x,1)}, values, labels+1);
grps = splitapply(@(x) {x}, values, labels+1);
Distance = cellfun(@(x,y) {pdist2(x,y)}, grps, avg);
Thank you so much.
I want to calculate the accuracy of the classification.
Could you explain how to find the original class and compare the prediction with the original class(label)?
I want to get a % average accuracy using Distance and softmax.
clear all
close all
data = csvread('outfile.csv');
values = data(:,1:end-1);
labels = data(:,end);
avg = splitapply(@(x) {mean(x,1)}, values, labels+1);
grps = splitapply(@(x) {x}, values, labels+1);
Distance = cellfun(@(x,y) {pdist2(x,y)}, grps, avg);
test1 = Distance{1};
test2 = Distance{2};
test3 = Distance{3};
test4 = Distance{4};
test5 = Distance{5};
test6 = Distance{6};
test7 = Distance{7};
test8 = Distance{8};
%softmax : a = softmax(n) = exp(n)/sum(exp(n))
dlY1 = softmax(test1);
dlY2 = softmax(test2);
dlY3 = softmax(test3);
dlY4 = softmax(test4);
dlY5 = softmax(test5);
dlY6 = softmax(test6);
dlY7 = softmax(test7);
dlY8 = softmax(test8);
@Kong, I am not sure about your question? Can you explain what is your classification model, and how do you make a prediction?
Thank you so much. After calculating the mean of each class, I want to select sample row to compute Euclidean distance between the row and the mean of classes.
For example, I have 10 classes. The results are 10 Euclidean distance. I want to use softmax after the results of Euclidean distance and finally apply loss function.
@Kong, your question is not clear. Do you want to apply softmax function to all the distance values
data = csvread('outfile.csv');
values = data(:,1:end-1);
labels = data(:,end);
avg = splitapply(@(x) {mean(x,1)}, values, labels+1);
grps = splitapply(@(x) {x}, values, labels+1);
Distance = cellfun(@(x,y) {pdist2(x,y)}, grps, avg);
dlY = cellfun(@(x) {softmax(x)}, Distance);
Thank you so much.
I want to select 5 rows of the same classes randomly and compute the mean of each 5 rows.
(10 Combination 5)
So I want to get all possible combinations' average values of the same classes.
In this code, I computed all rows of the same class and got one average value.
Could you explain how to fix the code?
data = csvread('outfile.csv');
values = data(:,1:end-1);
labels = data(:,end);
avg = splitapply(@(x) {mean(x,1)}, values, labels+1);

Sign in to comment.

More Answers (0)

Categories

Asked:

on 2 Apr 2020

Edited:

on 6 Apr 2020

Community Treasure Hunt

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

Start Hunting!