different outputs for same input in every run

Dear all,
I am using fitcknn function and I expect a 8*2 matrix to be the output (detected varialble). But sometime the output is correct and other times not. I used fixed rand number seed and clear all variables before every run but it does not work.
Any idea what is the problem of my code? Many thanks in advance.
clear all;
data = load('data.mat').data;
%%%%%%%%%
find_kmeans=kmeans(data(:,1),2);
data_2=table(data(:,1),find_kmeans(:));
data_2.Properties.VariableNames = ["counts", "detect"];
Mdl=fitcknn(data_2,'detect');
Mdl.NumNeighbors=1;
test_data=data(:,1);
pred=predict(Mdl,test_data);
wanted_points=(find (pred==2));
wanted_points_cnts= data(wanted_points,1);
detected=table(wanted_points(:),wanted_points_cnts(:));
scatter(wanted_points,wanted_points_cnts)

2 件のコメント

Walter Roberson
Walter Roberson 2022 年 8 月 4 日
I do not see a fixed random number seed?
Reminder: kmeans() does random initialization, so you would need to set the seed before the kmeans call.
Hamid
Hamid 2022 年 8 月 4 日
@Walter Roberson Many thanks for the useful comment. I removed it here and maybe the line I wrote my randon seed was wrong.

サインインしてコメントする。

 採用された回答

KSSV
KSSV 2022 年 8 月 4 日

0 投票

%%%%%%%%%
rng(1)
find_kmeans=kmeans(data(:,1),2);
data_2=table(data(:,1),find_kmeans(:));
data_2.Properties.VariableNames = ["counts", "detect"];
Mdl=fitcknn(data_2,'detect');
Mdl.NumNeighbors=1;
test_data=data(:,1);
pred=predict(Mdl,test_data);
wanted_points=(find (pred==2));
wanted_points_cnts= data(wanted_points,1);
detected=table(wanted_points(:),wanted_points_cnts(:));
scatter(wanted_points,wanted_points_cnts)
drawnow

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

製品

リリース

R2022a

質問済み:

2022 年 8 月 4 日

コメント済み:

2022 年 8 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by