I hope you are doing well. I have the following code Which implements K-means in MATLAB, I want to implement it in python. I am unable to implement it in python. Can anybody please help me with that
Dataset
0.119349659383,2765187888.188327790000,-50.272277924288,0.000010124208
0.119639999551,2780553879.583636760000,-45.173332876699,0.000015075661
0.119899673836,2765356033.223678110000,-50.327888424563,0.000010123978
0.120209965074,2780981089.939126490000,-45.152589356947,0.000015059274
0.120449679454,2765635512.158593650000,-50.363949423158,0.000010131346
dataset= readmatrix('newdata.txt');
[idx,C,sumdist] = kmeans(dataset,3,'Display','final','Replicates',5);
figure
gscatter(dataset(:,1),dataset(:,2),idx,'bgm')
hold on
plot(C(:,1),C(:,2),'kx')
legend('Cluster 1','Cluster 2','Cluster 3','Cluster Centroid')
dataset_idx=zeros(size(dataset,1));
dataset_idx=dataset(:,:);
dataset_idx(:,5)=idx;
clusters = cell(3,1);
for i = 1:3
clusters{i} = dataset_idx(dataset_idx(:,5) == i,:);
figure;
scatter(clusters{i}(:,1),clusters{i}(:,2))
legend(sprintf('Cluster %d',i))
title(sprintf('Cluster %d',i))
end
for i = 1:3
T = clusters{i}(:,1);
fprintf('\nCLUSTER %d:\n',i)
DeltaT = diff(T);
MclusterTimeseries = mean(DeltaT);
formatSpec = 'Mean DeltaT of Cluster %d is %4e\n';
fprintf(formatSpec,i,MclusterTimeseries)
MclusterFrequncy = mean(clusters{i}(:,2));
formatSpec = 'Mean Frequncy of Cluster %d is %4e\n';
fprintf(formatSpec,i,MclusterFrequncy)
MclusterAmplitude = max(clusters{i}(:,3));
formatSpec = 'Max Amplitude of Cluster %d is %4.4f\n';
fprintf(formatSpec,i,MclusterAmplitude)
Mcluster1PW = mean(clusters{i}(:,4));
formatSpec = 'Mean Pulse Width of Cluster %d is %4e\n';
fprintf(formatSpec,i,Mcluster1PW)
end