-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathkmeans_clstr.f90
executable file
·1574 lines (1352 loc) · 41.9 KB
/
kmeans_clstr.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
! soft k-means clustering with k=2
! D.J.C. MacKay, Information Theory, Inference & Learning Algorithms, 2003, p.304
! Aug 2006
module kmeans_clstr
use RandomNS
use utils1
implicit none
contains
!----------------------------------------------------------------------
!soft k-means corresponding to the model of axis aligned Gaussians (see
!MacKay, 2003)
subroutine kmeans(k,pt,npt,numdim,cluster,min_pt)
implicit none
integer k,numdim,npt
double precision pt(numdim,npt)
double precision sigsq(k,numdim),means(k,numdim),wt(k),r(npt,k)
integer cluster(npt)
double precision sumr,totR(k),temp,covmat(numdim,numdim),mean(numdim)
integer i,j,indx(1),x,nochg,count,min_pt
logical clstrd
double precision urv
if(k>npt/min_pt+1) k=npt/min_pt+1
clstrd=.false.
nochg=0
count=0
do i=1,numdim
mean(i)=sum(pt(i,1:npt))/dble(npt)
end do
call calc_covmat(npt,numdim,pt,mean,covmat)
do i=1,k
wt(i)=ranmarns(0)
urv=ranmarns(0)
x=int(dble(npt)*urv)+1
means(i,:)=pt(:,x)
end do
! means(1,1)=0
! means(1,2)=0
! means(2,1)=1.
! means(2,2)=1.
temp=huge(1.d0)
do j=1,numdim
if(covmat(j,j)<temp) temp=covmat(j,j)
end do
sigsq=temp
temp=sum(wt)
wt=wt/temp
do
count=count+1
clstrd=.true.
do i=1,npt
do j=1,k
temp=1.
do x=1,numdim
temp=temp*2.506628275*sqrt(sigsq(j,x))
end do
r(i,j)=wt(j)*exp(-sum(((means(j,:)-pt(:,i))**2.)/(2.*sigsq(j,:))))/temp
end do
sumr=sum(r(i,:))
r(i,:)=r(i,:)/sumr
indx=maxloc(r(i,:))
if(cluster(i)/=indx(1)) clstrd=.false.
cluster(i)=indx(1)
end do
if(clstrd) then
nochg=nochg+1
if(nochg==2) exit
else
nochg=0
end if
do i=1,k
totR(i)=sum(r(:,i))
! update means & sigsq
do j=1,numdim
means(i,j)=sum(r(:,i)*pt(j,:))/totR(i)
sigsq(i,j)=sum(r(:,i)*((pt(j,:)-means(i,j))**2.))/totR(i)
end do
end do
!update weights
temp=sum(totR(:))
wt(:)=totR(:)/temp
end do
end subroutine kmeans
!----------------------------------------------------------------------
!soft k-means corresponding to the model of spherical Gaussians (see MacKay 2003)
subroutine kmeans2(k,pt,npt,numdim,cluster,min_pt)
implicit none
integer k,numdim,npt
double precision pt(numdim,npt)
double precision sigsq(k),means(k,numdim),wt(k),r(npt,k)
integer cluster(npt),old_cluster(npt)
double precision sumr,totR(k),temp,covmat(numdim,numdim),mean(numdim)
integer i,j,indx(1),x,nochg,min_pt
logical clstrd
double precision urv
if(k>npt/min_pt+1) k=npt/min_pt+1
clstrd=.false.
nochg=0
do i=1,numdim
mean(i)=sum(pt(i,1:npt))/dble(npt)
end do
call calc_covmat(npt,numdim,pt,mean,covmat)
do i=1,k
wt(i)=ranmarns(0)
urv=ranmarns(0)
x=int(dble(npt)*urv)+1
means(i,:)=pt(:,x)
end do
temp=huge(1.d0)
do j=1,numdim
if(covmat(j,j)<temp) temp=covmat(j,j)
end do
sigsq=temp
temp=sum(wt)
wt=wt/temp
old_cluster=0
do
do i=1,npt
do j=1,k
temp=(2.506628275*sqrt(sigsq(j)))**dble(numdim)
r(i,j)=wt(j)*exp(-sum((means(j,:)-pt(:,i))**2.)/(2.*sigsq(j)))/temp
end do
sumr=sum(r(i,:))
r(i,:)=r(i,:)/sumr
indx=maxloc(r(i,:))
cluster(i)=indx(1)
end do
clstrd=.true.
do i=1,npt
if(old_cluster(i).ne.cluster(i)) then
clstrd=.false.
exit
end if
end do
if(clstrd) then
nochg=nochg+1
if(nochg==2) exit
else
nochg=0
end if
old_cluster=cluster
do i=1,k
totR(i)=sum(r(:,i))
sigsq(i)=0.
! update means & sigsq
do j=1,numdim
means(i,j)=sum(r(:,i)*pt(j,:))/totR(i)
sigsq(i)=sigsq(i)+sum(r(:,i)*((pt(j,:)-means(i,j))**2.))
end do
sigsq(i)=sigsq(i)/(numdim*totR(i))
end do
!update weights
temp=sum(totR(:))
wt(:)=totR(:)/temp
end do
end subroutine kmeans2
!----------------------------------------------------------------------
!simple k-means
subroutine kmeans3(k,pt,npt,numdim,means,cluster,min_pt)
implicit none
integer k,numdim,npt,i1,i2,i3
double precision pt(numdim,npt)
double precision means(k,numdim),dis(min_pt,2)
integer cluster(npt),old_cluster(npt),r(npt,k),totR(k)
double precision temp,dist
integer i,j,x,nochg,scrap(k),min_pt
logical clstrd,flag
double precision urv,d1
if(k>npt/min_pt+1) k=npt/min_pt+1
if(k==1) then
do i=1,numdim
means(1,i)=sum(pt(i,1:npt))/dble(npt)
enddo
cluster=1
return
endif
clstrd=.false.
nochg=0
! call ranmarns(urv)
! x=int(dble(npt)*urv)+1
! means(1,:)=pt(:,x)
! temp=-1.
! do i=2,k
! lmean(:)=sum(means(1:i-1,:))/dble(i-1)
! do j=1,npt
! dist=sum((lmean(:)-pt(:,j))**2.)
! if(dist>temp) then
! temp=dist
! x=j
! end if
! end do
! means(i,:)=pt(:,x)
! end do
! lmean(:)=means(1,:)
! do i=1,k/2+1
! do
! flag=.false.
! call ranmarns(urv)
! x=int(dble(npt)*urv)+1
! do j=1,i-1
! if(x==scrap(j)) then
! flag=.true.
! exit
! end if
! end do
! if(.not.flag) exit
! end do
! scrap(i)=x
! means(i*2-1,1:numdim)=pt(1:numdim,x)
! if(i*2>k) exit
! means(i*2,1:numdim)=2.*lmean(1:numdim)-pt(1:numdim,x)
! end do
!choose random points as starting positions
do i=1,k
do
flag=.false.
urv=ranmarns(0)
x=int(dble(npt)*urv)+1
do j=1,i-1
if(x==scrap(j)) then
flag=.true.
exit
endif
enddo
if(flag) then
cycle
else
scrap(i)=x
exit
endif
enddo
means(i,:)=pt(:,x)
enddo
old_cluster=0
do
do i=1,npt
temp=huge(1.d0)
do j=1,k
dist=sum((means(j,:)-pt(:,i))**2.)
if(dist<temp) then
temp=dist
x=j
endif
enddo
r(i,:)=0
r(i,x)=1
cluster(i)=x
enddo
clstrd=.true.
do i=1,npt
if(old_cluster(i)/=cluster(i)) then
clstrd=.false.
exit
endif
enddo
if(clstrd) then
!check if all the clusters have more than min_pt points
do i=1,k
if(totR(i)<min_pt) then
dis=1.d99
i1=min_pt-totR(i)
do j=1,npt
if(cluster(j)/=i .and. totR(cluster(j))>min_pt) then
d1=sum((means(i,:)-pt(:,j))**2.)
i3=0
do i2=i1,1,-1
if(d1<dis(i2,1)) then
i3=i2
else
exit
endif
enddo
if(i3/=0) then
dis(i3+1:i1,:)=dis(i3:i1-1,:)
dis(i3,1)=d1
dis(i3,2)=dble(j)
endif
endif
enddo
do j=1,i1
i3=int(dis(j,2))
i2=cluster(i3)
cluster(i3)=i
totR(i)=totR(i)+1
totR(i2)=totR(i2)-1
r(i3,:)=0
r(i3,i)=1
enddo
endif
enddo
do i=1,k
!update means
do j=1,numdim
means(i,j)=sum(r(:,i)*pt(j,:))/totR(i)
enddo
enddo
exit
endif
old_cluster=cluster
do i=1,k
totR(i)=sum(r(:,i))
if(totR(i)==0) cycle
!update means
do j=1,numdim
means(i,j)=sum(r(:,i)*pt(j,:))/totR(i)
enddo
enddo
enddo
end subroutine kmeans3
!----------------------------------------------------------------------
!Incremental K-means (Pham, Dimov, Nguyen, 2005)
subroutine kmeans4(k,pt,npt,numdim,cluster,min_pt)
implicit none
integer k,numdim,npt
double precision pt(:,:)
double precision means(k,numdim)
integer cluster(:)
integer totR(k)
integer i,j,x,m,min_pt
logical clstrd
double precision urv
double precision distortion(k)!distortion of each cluster
integer indx(1)
if(k>npt/min_pt+1) k=npt/min_pt+1
distortion=0.
totR=0
!choose a random point to be the cluster center
cluster=1
totR(1)=npt
do j=1,numdim
means(1,j)=sum(pt(j,:))/dble(npt)
end do
if(k==1) return
do m=2,k
clstrd=.false.
!pick the cluster with the max distortion
indx=maxloc(distortion(1:m-1))
!pick a random point from this cluster as new cluster center
j=0
urv=ranmarns(0)
x=int(dble(totR(indx(1)))*urv)+1
do i=1,npt
if(cluster(i)==indx(1)) j=j+1
if(j==x) exit
end do
means(cluster(i),:)=(means(cluster(i),:)*dble(totR(cluster(i)))-pt(:,i))/dble(totR(cluster(i))-1)
means(m,:)=pt(:,i)
totR(cluster(i))=totR(cluster(i))-1
totR(m)=1
cluster(i)=m
do
clstrd=.true.
!check whether each point is closest to its own cluster or to the newly formed
!one & assign accordingly
do i=1,npt
if(sum((pt(1:numdim,i)-means(cluster(i),1:numdim))**2.)> &
sum((pt(1:numdim,i)-means(m,1:numdim))**2.))then
!point moved so not finished yet
clstrd=.false.
!update cluster data
totR(cluster(i))=totR(cluster(i))-1
cluster(i)=m
totR(m)=totR(m)+1
end if
end do
!update means & distortion of each cluster
means=0.
distortion=0.
do i=1,npt
means(cluster(i),1:numdim)=means(cluster(i),1:numdim)+pt(1:numdim,i)
distortion(cluster(i))=distortion(cluster(i))+sum(pt(1:numdim,i)**2.)
end do
do j=1,numdim
means(1:m,j)=means(1:m,j)/dble(totR(1:m))
distortion(1:m)=distortion(1:m)-dble(totR(1:m))*(means(1:m,j)**2.)
end do
if(clstrd) exit
end do
end do
end subroutine kmeans4
!----------------------------------------------------------------------
!2-means with k=2 & clusters placed in their expected positions as the starting points
subroutine kmeans6(pt,npt,numdim,cluster)
implicit none
integer numdim,npt
double precision pt(numdim,npt)
double precision means(2,numdim)
integer cluster(npt)
integer totR(2)
integer i,j,x
logical clstrd
double precision covmat(numdim,numdim),evec(numdim,numdim),eval(numdim)
!calculate the mean of the data
do j=1,numdim
means(1,j)=sum(pt(j,1:npt))/dble(npt)
end do
!do principal component analysis
call calc_covmat(npt,numdim,pt,means(1,:),covmat)
evec=covmat
call diagonalize(evec,eval,numdim,.false.)
!place the cluster centers in their ex[ected locations
means(2,:)=means(1,:)+evec(:,numdim)*sqrt(2.*eval(numdim)/3.1416)
means(1,:)=means(1,:)-evec(:,numdim)*sqrt(2.*eval(numdim)/3.1416)
cluster=1
totR(1)=npt
totR(2)=0
do
clstrd=.true.
!check whether each point is closest to its own cluster or to the newly formed
!one & assign accordingly
do i=1,npt
if(cluster(i)==1) then
x=2
else
x=1
end if
if(sum((pt(1:numdim,i)-means(cluster(i),1:numdim))**2.)> &
sum((pt(1:numdim,i)-means(x,1:numdim))**2.))then
!point moved so not finished yet
clstrd=.false.
!update cluster data
totR(cluster(i))=totR(cluster(i))-1
cluster(i)=x
totR(x)=totR(x)+1
end if
end do
!update means of each cluster
means=0.
do i=1,npt
means(cluster(i),1:numdim)=means(cluster(i),1:numdim)+pt(1:numdim,i)
end do
do j=1,numdim
means(1:2,j)=means(1:2,j)/dble(totR(1:2))
end do
if(clstrd) exit
end do
end subroutine kmeans6
!----------------------------------------------------------------------
!Incremental K-means with Unknown K
!(Pham, Dimov, Nguyen, 2005, "Incremental K-means Algorithm")
!(Pham, Dimov, Nguyen, 2005, "Selection of K in K-means Clustering")
subroutine kmeans5(k,pt,npt,numdim,cluster,min_pt)
implicit none
integer k,numdim,npt
double precision pt(numdim,npt)
double precision means(2,npt/(numdim+1),numdim),meanst(npt/(numdim+1),numdim)
double precision mean(npt/(numdim+1),numdim)
integer cls(2,npt),clst(npt),cluster(npt)
integer totR(2,npt/(numdim+1)),totRt(npt/(numdim+1))!total points in each cluster
integer i,j,x,m,min_pt
logical clstrd
!distortion of each cluster & total distortion of the recent two iterations
double precision distortion(2,npt/(numdim+1)),totDistor(2),distor(npt/(numdim+1))
integer indx(1)
double precision f(npt/(numdim+1)),fmin,a(npt/(numdim+1))!evaluation function
double precision S(npt/(numdim+1))!total distortion
integer count!counter for evaluation function
if(k>npt/min_pt+1) k=npt/min_pt+1
distortion=0.
totR=0
f=0.
a=0.
S=0.
count=0
!choose a random point to be the cluster center
cluster=1
cls(2,:)=1
totR(2,1)=npt
do j=1,numdim
means(2,1,j)=sum(pt(j,:))/dble(npt)
S(1)=S(1)+sum((pt(j,1:npt)-means(2,1,j))**2.)
end do
mean(:,:)=means(2,:,:)
f(1)=1.
fmin=1.
k=1
a(2)=1.-3./dble(4*numdim)
m=1
do
m=m+1
clstrd=.false.
!pick the cluster with the max distortion
indx=maxloc(distortion(2,1:m-1))
!break this cluster
clst(:)=cls(2,:)
totRt(:)=totR(2,:)
meanst(:,:)=means(2,:,:)
x=m-1
j=indx(1)
call kmeansDis(x,pt,npt,numdim,clst,j,totRt,distor,meanst)
cls(1,:)=clst(:)
totR(1,:)=totRt(:)
means(1,:,:)=meanst(:,:)
distortion(1,:)=distor(:)
totDistor(1)=sum(distor(1:m))
do i=1,m-1
if(i==indx(1)) cycle
if(distortion(2,i)>1.5*(totDistor(2)-totDistor(1))) then
clst(:)=cls(2,:)
totRt(:)=totR(2,:)
meanst(:,:)=means(2,:,:)
x=m-1
call kmeansDis(x,pt,npt,numdim,clst,i,totRt,distor,meanst)
if(sum(distor(1:m))<totDistor(1)) then
cls(1,:)=clst(:)
totR(1,:)=totRt(:)
means(1,:,:)=meanst(:,:)
distortion(1,:)=distor(:)
totDistor(1)=sum(distor(1:m))
end if
end if
end do
!calculate the total distortion
S(m)=0.
do i=1,npt
S(m)=S(m)+sum((pt(1:numdim,i)-means(1,cls(1,i),1:numdim))**2.)
end do
!calculate the evaluation function
f(m)=S(m)/(S(m-1)*a(m))
a(m+1)=a(m)+(1.-a(m))/6.
!write(*,*)m,fmin,f(m),f(m-1),a(m)
if(f(m)<fmin.and.f(m)<0.85) then
fmin=f(m)
cluster(:)=cls(1,:)
mean(:,:)=means(1,:,:)
k=m
count=0
else
count=count+1
end if
!if 5 consecutive f values are greater than 0.85 then exit
if(count==5) exit
means(2,:,:)=means(1,:,:)
cls(2,:)=cls(1,:)
totR(2,:)=totR(1,:)
distortion(2,:)=distortion(1,:)
totDistor(2)=totDistor(1)
end do
!meanst=mean
!do i=1,k
!do j=1,numdim
!call Rescale_nest(meanst(i,j),j)
!end do
!end do
!write(*,*)"means",meanst(1:k,:)
!write(*,*)"fmin=",fmin
end subroutine kmeans5
!----------------------------------------------------------------------
!Incremental K-means (Pham, Dimov, Nguyen, 2005) with if npt in any cluster is
!less than numdim+1 than (numdim+1-npt) closest points borrowed from its closest
!neighbour cluster
subroutine kmeans7(k,pt,npt,numdim,cluster,ad,cluster2,min_p)
implicit none
integer k,numdim,npt
double precision pt(:,:)
double precision means(npt/min_p+10,numdim)
integer cluster(:)
integer totR(npt/min_p+10),min_p
integer i,j,x,m,q,r,k0
logical clstrd
double precision urv
!double precision distortion(k)!distortion of each cluster
integer indx(1),indx2(1)
double precision adis(npt)!for keeping a record of the point's distances
double precision dis1,dis2,dis(npt,2)
integer ad,nc,cluster2(:,:),cc(npt),cpt(npt)
!distortion=0.
means=0.
totR=0
!choose a random point to be the cluster center
cluster=1
totR(1)=npt
do j=1,numdim
means(1,j)=sum(pt(j,1:npt))/dble(npt)
end do
k=min(npt/min_p,k+10)
if(k<=1) then
k=1
ad=0
return
end if
k0=k
do m=2,k
clstrd=.false.
!pick the cluster with most points
indx=maxloc(totR(1:m-1))
!if all the clusters have less than npt<2(numdim+1) points then exit
if(totR(indx(1))<2*min_p) then
k=m-1
exit
end if
!pick a random point from this cluster as new cluster center
j=0
urv=ranmarns(0)
x=int(dble(totR(indx(1)))*urv)+1
do i=1,npt
if(cluster(i)==indx(1)) j=j+1
if(j==x) exit
end do
means(cluster(i),:)=(means(cluster(i),:)*dble(totR(cluster(i)))-pt(:,i))/dble(totR(cluster(i))-1)
means(m,:)=pt(:,i)
totR(cluster(i))=totR(cluster(i))-1
totR(m)=1
cluster(i)=m
do
clstrd=.true.
!check whether each point is closest to its own cluster or to the newly formed
!one & assign accordingly
do i=1,npt
dis1=sum((pt(1:numdim,i)-means(cluster(i),1:numdim))**2)
dis2=sum((pt(1:numdim,i)-means(m,1:numdim))**2)
adis(i)=dis1-dis2
end do
do i=1,numdim
means(1:m,i)=means(1:m,i)*dble(totR(1:m))
end do
do
indx2=maxloc(adis)
if(adis(indx2(1))<=0.) then
exit
else if(totR(cluster(indx2(1)))<=min_p) then
adis(indx2(1))=-1.
else
!point moved so not finished yet
clstrd=.false.
!update cluster data
totR(cluster(indx2(1)))=totR(cluster(indx2(1)))-1
means(cluster(indx2(1)),1:numdim)=means(cluster(indx2(1)),1:numdim)-pt(1:numdim,indx2(1))
cluster(indx2(1))=m
totR(m)=totR(m)+1
means(m,1:numdim)=means(m,1:numdim)+pt(1:numdim,indx2(1))
adis(indx2(1))=-1.
end if
end do
!update means
do j=1,numdim
means(1:m,j)=means(1:m,j)/dble(totR(1:m))
end do
!if num of points in the newly formed cluster are less than
!(numdim+1) then borrow closest points from cluster from which the new cluster
!was formed
if(clstrd.and.(totR(m)<min_p.or.totR(indx(1))<min_p)) then
if(totR(m)<min_p) then
x=m
q=indx(1)
else
x=indx(1)
q=m
end if
adis=1.d99
do i=1,npt
if(cluster(i)==q) then
adis(i)=sum((pt(1:numdim,i)-means(x,1:numdim))**2)
end if
end do
means(q,1:numdim)=means(q,1:numdim)*dble(totR(q))
means(x,1:numdim)=means(x,1:numdim)*dble(totR(x))
do i=1,min_p-totR(x)
indx2=minloc(adis)
adis(indx2(1))=1.d99
cluster(indx2(1))=x
means(q,1:numdim)=means(q,1:numdim)-pt(1:numdim,indx2(1))
means(x,1:numdim)=means(x,1:numdim)+pt(1:numdim,indx2(1))
end do
totR(q)=totR(q)-min_p+totR(x)
totR(x)=min_p
means(q,1:numdim)=means(q,1:numdim)/dble(totR(q))
means(x,1:numdim)=means(x,1:numdim)/dble(totR(x))
end if
if(clstrd) exit
end do
end do
!find shared points
ad=min(ad,npt-1)
nc=min(k-1,ad)
cluster2=0
if(ad==0) return
do i=1,k
dis(1:nc,1)=1.d99
do j=1,k
if(i==j) cycle
dis1=sum((means(i,1:numdim)-means(j,1:numdim))**2)
do r=1,nc
if(dis1<dis(r,1)) then
dis(r+1:nc,1)=dis(r:nc-1,1)
dis(r+1:nc,2)=dis(r:nc-1,2)
dis(r,1)=dis1
dis(r,2)=dble(j)
exit
end if
end do
end do
cc(1:nc)=int(dis(1:nc,2))
!make a list of point indices in cluster i
q=0
do j=1,npt
if(cluster(j)==i) then
q=q+1
cpt(q)=j
end if
if(q==totR(i)) exit
end do
!adis=1.d99
dis(1:ad,1)=1.d99
do j=1,npt
do r=1,nc
if(cluster(j)==cc(r)) then
do q=1,totR(i)
dis1=sum((pt(1:numdim,cpt(q))-pt(1:numdim,j))**2)
do k0=1,ad
if(dis(k0,1)>dis1) then
dis(k0+1:ad,1)=dis(k0:ad-1,1)
dis(k0+1:ad,2)=dis(k0:ad-1,2)
dis(k0,1)=dis1
dis(k0,2)=dble(j)
exit
end if
end do
end do
exit
end if
end do
end do
cluster2(i,1:ad)=int(dis(1:ad,2))
end do
end subroutine kmeans7
!----------------------------------------------------------------------
!Incremental K-means (Pham, Dimov, Nguyen, 2005) with if npt in any cluster is
!less than numdim+1 than (numdim+1-npt) closest points borrowed from its closest
!neighbour cluster
subroutine sKmeans(k,pt,npt,numdim,cluster,min_p)
implicit none
integer k,numdim,npt
double precision pt(:,:)
double precision means(npt/min_p+10,numdim)
integer cluster(npt)
integer totR(npt/min_p+10),min_p
integer i,j,x,m,q,k0
logical clstrd
double precision urv
integer indx(1),indx2(1)
double precision adis(npt)!for keeping a record of the point's distances
double precision dis1,dis2
means=0.
totR=0
!choose a random point to be the cluster center
cluster=1
totR(1)=npt
do j=1,numdim
means(1,j)=sum(pt(j,1:npt))/dble(npt)
end do
k=min(npt/min_p,k+10)
if(k<=1) then
k=1
return
end if
k0=k
do m=2,k
clstrd=.false.
!pick the cluster with most points
indx=maxloc(totR(1:m-1))
!if all the clusters have less than npt<2(numdim+1) points then exit
if(totR(indx(1))<2*min_p) then
k=m-1
exit
endif
!pick a random point from this cluster as new cluster center
j=0
urv=ranmarns(0)
x=int(dble(totR(indx(1)))*urv)+1
do i=1,npt
if(cluster(i)==indx(1)) j=j+1
if(j==x) exit
enddo
means(cluster(i),:)=(means(cluster(i),:)*dble(totR(cluster(i)))-pt(:,i))/dble(totR(cluster(i))-1)
means(m,:)=pt(:,i)
totR(cluster(i))=totR(cluster(i))-1
totR(m)=1
cluster(i)=m
do
clstrd=.true.
!check whether each point is closest to its own cluster or to the newly formed
!one & assign accordingly
do i=1,npt
dis1=sum((pt(1:numdim,i)-means(cluster(i),1:numdim))**2)
dis2=sum((pt(1:numdim,i)-means(m,1:numdim))**2)
adis(i)=dis1-dis2
enddo
do i=1,numdim
means(1:m,i)=means(1:m,i)*dble(totR(1:m))
enddo
do
indx2=maxloc(adis)
if(adis(indx2(1))<=0.) then
exit
elseif(totR(cluster(indx2(1)))<=min_p) then
adis(indx2(1))=-1.
else
!point moved so not finished yet
clstrd=.false.
!update cluster data
totR(cluster(indx2(1)))=totR(cluster(indx2(1)))-1
means(cluster(indx2(1)),1:numdim)=means(cluster(indx2(1)),1:numdim)-pt(1:numdim,indx2(1))
cluster(indx2(1))=m
totR(m)=totR(m)+1
means(m,1:numdim)=means(m,1:numdim)+pt(1:numdim,indx2(1))
adis(indx2(1))=-1.
endif
enddo
!update means
do j=1,numdim
means(1:m,j)=means(1:m,j)/dble(totR(1:m))
enddo
!if num of points in the newly formed cluster are less than
!(numdim+1) then borrow closest points from cluster from which the new cluster
!was formed
if(clstrd.and.(totR(m)<min_p.or.totR(indx(1))<min_p)) then
if(totR(m)<min_p) then
x=m
q=indx(1)
else
x=indx(1)
q=m
endif
adis=1.d99
do i=1,npt
if(cluster(i)==q) then
adis(i)=sum((pt(1:numdim,i)-means(x,1:numdim))**2)
endif
enddo
means(q,1:numdim)=means(q,1:numdim)*dble(totR(q))
means(x,1:numdim)=means(x,1:numdim)*dble(totR(x))
do i=1,min_p-totR(x)
indx2=minloc(adis)
adis(indx2(1))=1.d99
cluster(indx2(1))=x
means(q,1:numdim)=means(q,1:numdim)-pt(1:numdim,indx2(1))
means(x,1:numdim)=means(x,1:numdim)+pt(1:numdim,indx2(1))
enddo
totR(q)=totR(q)-min_p+totR(x)
totR(x)=min_p
means(q,1:numdim)=means(q,1:numdim)/dble(totR(q))
means(x,1:numdim)=means(x,1:numdim)/dble(totR(x))
endif
if(clstrd) exit
enddo
enddo
end subroutine sKmeans
!----------------------------------------------------------------------