Saving results from loop with intervals

2 次查看(过去 30 天)
Hi everyone, I am applying Naive Bayes Classification by hand to a data set with formula. My data has 45.000 columns and no class informations. I would like to seperate it with classes. I will have nearly 100 classes but I don't want to write it by hand. I want to handle it with loop and I am looking for hours but I couldn't figure it out.
veri = readtable("2018.csv");
maxgh = max(veri.GlobalHoriz_W_m_2_);
mingh = min(veri.GlobalHoriz_W_m_2_);
meangh = mean(veri.GlobalHoriz_W_m_2_);
stdgh = std(veri.GlobalHoriz_W_m_2_);
vericell = table2cell(veri);
ghcolumn = cell2mat(vericell(:,3));
*class1 = vericell(ghcolumn>=mingh & ghcolumn <=95,:);
class2 = vericell(ghcolumn>95 & ghcolumn <=195,:);
class3 = vericell(ghcolumn>195 & ghcolumn <=295,:);
class4 = vericell(ghcolumn>295 & ghcolumn <=395,:);
class5 = vericell(ghcolumn>395 & ghcolumn <=495,:);
class6 = vericell(ghcolumn>495 & ghcolumn <=595,:);
class7 = vericell(ghcolumn>595 & ghcolumn <=695,:);
class8 = vericell(ghcolumn>695 & ghcolumn <=795,:);
class9 = vericell(ghcolumn>795 & ghcolumn <=895,:);
class10 = vericell(ghcolumn>895 & ghcolumn <=995,:);
class11 = vericell(ghcolumn>995 & ghcolumn <=maxgh,:);*
I would like to create seperate tables for the values which is between -5,95 95,195 195,295 etc.. with loop of course
For now I increased the numbers for 100 but I would like to increase them by 10 and I don't want to do it one by one with hand . Any help will be appreciated so much. Thanks in advance.
  4 个评论
Sukru Yavuz
Sukru Yavuz 2018-2-28
编辑:Sukru Yavuz 2018-2-28
@Guillaume I have to create different classes to increase the accuracy of Naive Bayes formula. I will test a date to see which class it belongs to. That's why I am creating different class tables. And you know By narrowing the intervals the number of classes will increase and also accuracy will increase.
@Stephen Cobeldick After seperating it to different classes, I will select a date and find which class does that date belongs to. That's why I am creating different tables to be able to calculate the possibilities for each of them.
Sukru Yavuz
Sukru Yavuz 2018-2-28
编辑:Sukru Yavuz 2018-2-28
My advisor told me to do it this way but like you said im pretty sure there are more efficient ways than that. Let's just say I would like to group my class label. The minimum value of that column is -5.071, the maximum value of that is 1008. I would like to set the intervals as 20 and split them.

请先登录,再进行评论。

回答(1 个)

Guillaume
Guillaume 2018-2-28
编辑:Guillaume 2018-2-28
As Stephen and I said, splitting your table into multiple tables is probably going to complicate things for you. I would recommend you create a new column instead which tells you which class each row belongs to:
veri = readtable("2018.csv");
veri.Class = discretize(veri{:, 3}, min(veri{:, 3}):20:max(veri{:, 3})+20)
That's all that is needed. No loop required. You can then do calculations by class using rowfun or varfun with the 'GroupingVariable', 'Class' option. For example, to get the mean of column 4 per each class:
varfun(@mean, veri, 'InputVariables', 4, 'GroupingVariables', 'Class')
If you really want to split the table:
splittables = splitapply(@(rows) {veri(rows, :)}, (1:height(veri))', veri.Class)
splittables{i} is the table of class i. But again, this should not be needed. Calculating the above mean is more complicated once it's split.
  1 个评论
Sukru Yavuz
Sukru Yavuz 2018-2-28
Now it is making more sense. I understood it completely. Thank you for being this clear sir. Have a nice evening.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Time Series Events 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by