blib/lib/PDL/Cluster.pm

filter: Statementstmt 100% 42 / 42 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 45 / 45 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« ccluster.c ↑ Summary
1 #
2 # GENERATED WITH PDL::PP from Cluster.pd! Don't modify!
3 #
4 package PDL::Cluster;
5  
6 9 our @EXPORT_OK = qw(cmean cmedian calculate_weights clusterdistance distancematrix getclustercentroids getclustermean getclustermedian getclustermedoids kcluster kmedoids treecluster treeclusterd cuttree somcluster pca rowdistances clusterdistances clustersizes clusterelements clusterelementmask clusterdistancematrix clusterenc clusterdec clusteroffsets clusterdistancematrixenc clusterdistancesenc getclusterwsum attachtonearest attachtonearestd checkprototypes checkpartitions randomprototypes randompartition );
7 9 our %EXPORT_TAGS = (Func=>\@EXPORT_OK);
8  
9 9 ▶use PDL::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 9 our $VERSION = '1.54.004';
15 9 our @ISA = ( 'PDL::Exporter','DynaLoader' );
16 9 push @PDL::Core::PP, __PACKAGE__;
17 9 bootstrap PDL::Cluster $VERSION;
18  
19  
20  
21  
22  
23  
24  
25  
26 #line 14 "Cluster.pd"
27  
28 #---------------------------------------------------------------------------
29 # File: PDL::Cluster.pm
30 # Author: Bryan Jurish <moocow@cpan.org>
31 # Description: PDL wrappers for the C Clustering library.
32 #
33 # Copyright (c) 2005-2021 Bryan Jurish. All rights reserved.
34 # This program is free software. You may modify and/or
35 # distribute it under the same terms as Perl itself.
36 #
37 #---------------------------------------------------------------------------
38 # Based on the C clustering library for cDNA microarray data,
39 # Copyright (C) 2002-2005 Michiel Jan Laurens de Hoon.
40 #
41 # The C clustering library was written at the Laboratory of DNA Information
42 # Analysis, Human Genome Center, Institute of Medical Science, University of
43 # Tokyo, 4-6-1 Shirokanedai, Minato-ku, Tokyo 108-8639, Japan.
44 # Contact: michiel.dehoon 'AT' riken.jp
45 #
46 # See the files "cluster.c" and "cluster.h" in the PDL::Cluster distribution
47 # for details.
48 #---------------------------------------------------------------------------
49  
50 =pod
51  
52 =head1 NAME
53  
54 PDL::Cluster - PDL interface to the C Clustering Library
55  
56 =head1 SYNOPSIS
57  
58 use PDL::Cluster;
59  
60 ##-----------------------------------------------------
61 ## Data Format
62 $d = 42; ##-- number of features
63 $n = 1024; ##-- number of data elements
64  
65 $data = random($d,$n); ##-- data matrix
66 $elt = $data->slice(",($i)"); ##-- element data vector
67 $ftr = $data->slice("($j),"); ##-- feature vector over all elements
68  
69 $wts = ones($d)/$d; ##-- feature weights
70 $msk = ones($d,$n); ##-- missing-datum mask (1=ok)
71  
72 ##-----------------------------------------------------
73 ## Library Utilties
74  
75 $mean = $ftr->cmean();
76 $median = $ftr->cmedian();
77  
78 calculate_weights($data,$msk,$wts, $cutoff,$expnt,
79 $weights);
80  
81 ##-----------------------------------------------------
82 ## Distance Functions
83  
84 clusterdistance($data,$msk,$wts, $n1,$n2,$idx1,$idx2,
85 $dist,
86 $distFlag, $methodFlag2);
87  
88 distancematrix($data,$msk,$wts, $distmat, $distFlag);
89  
90 ##-----------------------------------------------------
91 ## Partitioning Algorithms
92  
93 getclustermean($data,$msk,$clusterids,
94 $ctrdata, $ctrmask);
95  
96 getclustermedian($data,$msk,$clusterids,
97 $ctrdata, $ctrmask);
98  
99 getclustermedoid($distmat,$clusterids,$centroids,
100 $errorsums);
101  
102 kcluster($k, $data,$msk,$wts, $npass,
103 $clusterids, $error, $nfound,
104 $distFlag, $methodFlag);
105  
106 kmedoids($k, $distmat,$npass,
107 $clusterids, $error, $nfound);
108  
109 ##-----------------------------------------------------
110 ## Hierarchical Algorithms
111  
112 treecluster($data,$msk,$wts,
113 $tree, $lnkdist,
114 $distFlag, $methodFlag);
115  
116 treeclusterd($data,$msk,$wts, $distmat,
117 $tree, $lnkdist,
118 $distFlag, $methodFlag);
119  
120 cuttree($tree, $nclusters,
121 $clusterids);
122  
123 ##-----------------------------------------------------
124 ## Self-Organizing Maps
125  
126 somcluster($data,$msk,$wts, $nx,$ny,$tau,$niter,
127 $clusterids,
128 $distFlag);
129  
130 ##-----------------------------------------------------
131 ## Principal Component Analysis
132  
133 pca($U, $S, $V);
134  
135 ##-----------------------------------------------------
136 ## Extensions
137  
138 rowdistances($data,$msk,$wts, $rowids1,$rowids2, $distvec, $distFlag);
139 clusterdistances($data,$msk,$wts, $rowids, $index2,
140 $dist,
141 $distFlag, $methodFlag);
142  
143 clustersizes($clusterids, $clustersizes);
144 clusterelements($clustierids, $clustersizes, $eltids);
145 clusterelementmask($clusterids, $eltmask);
146  
147 clusterdistancematrix($data,$msk,$wts,
148 $rowids, $clustersizes, $eltids,
149 $dist,
150 $distFlag, $methodFlag);
151  
152 clusterenc($clusterids, $clens,$cvals,$crowids, $k);
153 clusterdec($clens,$cvals,$crowids, $clusterids, $k);
154 clusteroffsets($clusterids, $coffsets,$cvals,$crowids, $k);
155 clusterdistancematrixenc($data,$msk,$wts,
156 $clens1,$crowids1, $clens2,$crowids2,
157 $dist,
158 $distFlag, $methodFlag);
159  
160 =cut
161 #line 162 "Cluster.pm"
162  
163  
164 =head1 FUNCTIONS
165  
166 =cut
167  
168  
169  
170  
171  
172  
173 =head2 cmean
174  
175 =for sig
176  
177 Signature: (double a(n); double [o]b())
178 Types: (double)
179  
180 =for usage
181  
182 $b = cmean($a);
183 cmean($a, $b); # all arguments given
184 $b = $a->cmean; # method call
185 $a->cmean($b);
186  
187 =for ref
188  
189 Computes arithmetic mean of the vector $a(). See also PDL::Primitive::avg().
190  
191 =pod
192  
193 Broadcasts over its inputs.
194  
195 =for bad
196  
197 C<cmean> does not process bad values.
198 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
199  
200 =cut
201  
202  
203  
204  
205 9 *cmean = \&PDL::cmean;
206  
207  
208  
209  
210  
211  
212 =head2 cmedian
213  
214 =for sig
215  
216 Signature: (double a(n); double [o]b())
217 Types: (double)
218  
219 =for usage
220  
221 $b = cmedian($a);
222 cmedian($a, $b); # all arguments given
223 $b = $a->cmedian; # method call
224 $a->cmedian($b);
225  
226 =for ref
227  
228 Computes median of the vector $a(). See also PDL::Primitive::median().
229  
230 =pod
231  
232 Broadcasts over its inputs.
233  
234 =for bad
235  
236 C<cmedian> does not process bad values.
237 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
238  
239 =cut
240  
241  
242  
243  
244 9 *cmedian = \&PDL::cmedian;
245  
246  
247  
248  
249  
250  
251 =head2 calculate_weights
252  
253 =for sig
254  
255 Signature: (
256 double data(d,n);
257 int mask(d,n);
258 double weight(d);
259 double cutoff();
260 double exponent();
261 double [o]oweights(d);
262 ; char *distFlag;
263 )
264 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
265 float double ldouble)
266  
267 =for usage
268  
269 $oweights = calculate_weights($data, $mask, $weight, $cutoff, $exponent, $distFlag);
270 calculate_weights($data, $mask, $weight, $cutoff, $exponent, $oweights, $distFlag); # all arguments given
271 $oweights = $data->calculate_weights($mask, $weight, $cutoff, $exponent, $distFlag); # method call
272 $data->calculate_weights($mask, $weight, $cutoff, $exponent, $oweights, $distFlag);
273  
274 This function calculates weights for the features using the weighting scheme
275 proposed by Michael Eisen:
276  
277 w[i] = 1.0 / sum_{j where dist(i,j)<cutoff} (1 - dist(i,j)/cutoff)^exponent
278  
279 where the cutoff and the exponent are specified by the user.
280  
281 =pod
282  
283 Broadcasts over its inputs.
284  
285 =for bad
286  
287 C<calculate_weights> does not process bad values.
288 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
289  
290 =cut
291  
292  
293  
294  
295 9 *calculate_weights = \&PDL::calculate_weights;
296  
297  
298  
299  
300  
301  
302 =head2 clusterdistance
303  
304 =for sig
305  
306 Signature: (
307 double data(d,n);
308 int mask(d,n);
309 double weight(d);
310 int n1();
311 int n2();
312 int index1(n1);
313 int index2(n2);
314 double [o]dist();
315 ;
316 char *distFlag;
317 char *methodFlag;
318 )
319 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
320 float double ldouble)
321  
322 =for usage
323  
324 $dist = clusterdistance($data, $mask, $weight, $n1, $n2, $index1, $index2, $distFlag, $methodFlag);
325 clusterdistance($data, $mask, $weight, $n1, $n2, $index1, $index2, $dist, $distFlag, $methodFlag); # all arguments given
326 $dist = $data->clusterdistance($mask, $weight, $n1, $n2, $index1, $index2, $distFlag, $methodFlag); # method call
327 $data->clusterdistance($mask, $weight, $n1, $n2, $index1, $index2, $dist, $distFlag, $methodFlag);
328  
329 Computes distance between two clusters $index1() and $index2().
330 Each of the $index() vectors represents a single cluster whose values
331 are the row-indices in the $data() matrix of the elements assigned
332 to the respective cluster. $n1() and $n2() are the number of elements
333 in $index1() and $index2(), respectively. Each $index$i() must have
334 at least $n$i() elements allocated.
335  
336 B<CAVEAT:> the $methodFlag argument is interpreted differently than
337 by the treecluster() method, namely:
338  
339 =over 4
340  
341 =item a
342  
343 Distance between the arithmetic means of the two clusters,
344 as for treecluster() "f".
345  
346 =item m
347  
348 Distance between the medians of the two clusters,
349 as for treecluster() "c".
350  
351 =item s
352  
353 Minimum pairwise distance between members of the two clusters,
354 as for treecluster() "s".
355  
356 =item x
357  
358 Maximum pairwise distance between members of the two clusters
359 as for treecluster() "m".
360  
361 =item v
362  
363 Average of the pairwise distances between members of the two clusters,
364 as for treecluster() "a".
365  
366 =back
367  
368 =pod
369  
370 Broadcasts over its inputs.
371  
372 =for bad
373  
374 C<clusterdistance> does not process bad values.
375 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
376  
377 =cut
378  
379  
380  
381  
382 9 *clusterdistance = \&PDL::clusterdistance;
383  
384  
385  
386  
387  
388  
389 =head2 distancematrix
390  
391 =for sig
392  
393 Signature: (
394 double data(d,n);
395 int mask(d,n);
396 double weight(d);
397 double [o]dists(n,n);
398 ; char *distFlag;
399 )
400 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
401 float double ldouble)
402  
403 =for usage
404  
405 $dists = distancematrix($data, $mask, $weight, $distFlag);
406 distancematrix($data, $mask, $weight, $dists, $distFlag); # all arguments given
407 $dists = $data->distancematrix($mask, $weight, $distFlag); # method call
408 $data->distancematrix($mask, $weight, $dists, $distFlag);
409  
410 =for ref
411  
412 Compute triangular distance matrix over all data points.
413  
414 =pod
415  
416 Broadcasts over its inputs.
417  
418 =for bad
419  
420 C<distancematrix> does not process bad values.
421 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
422  
423 =cut
424  
425  
426  
427  
428 9 *distancematrix = \&PDL::distancematrix;
429  
430  
431  
432  
433  
434  
435 =head2 getclustercentroids
436  
437 =for sig
438  
439 Signature: (
440 double data(d,n);
441 int mask(d,n);
442 int clusterids(n);
443 double [o]cdata(d,k);
444 int [o]cmask(d,k);
445 ; char *ctrMethodFlag;
446 )
447 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
448 float double ldouble)
449  
450 =for usage
451  
452 ($cdata, $cmask) = getclustercentroids($data, $mask, $clusterids, $ctrMethodFlag);
453 getclustercentroids($data, $mask, $clusterids, $cdata, $cmask, $ctrMethodFlag); # all arguments given
454 ($cdata, $cmask) = $data->getclustercentroids($mask, $clusterids, $ctrMethodFlag); # method call
455 $data->getclustercentroids($mask, $clusterids, $cdata, $cmask, $ctrMethodFlag);
456  
457 =for ref
458  
459 Find cluster centroids by arithmetic mean (C<ctrMethodFlag="a">) or median over each dimension (C<ctrMethodFlag="m">).
460  
461 =pod
462  
463 Broadcasts over its inputs.
464  
465 =for bad
466  
467 C<getclustercentroids> does not process bad values.
468 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
469  
470 =cut
471  
472  
473  
474  
475 9 *getclustercentroids = \&PDL::getclustercentroids;
476  
477  
478  
479  
480  
481 #line 589 "Cluster.pd"
482  
483 =pod
484  
485 =head2 getclustermean
486  
487 =for sig
488  
489 Signature: (
490 double data(d,n);
491 int mask(d,n);
492 int clusterids(n);
493 double [o]cdata(d,k);
494 int [o]cmask(d,k);
495 )
496  
497 Really just a wrapper for getclustercentroids(...,"a").
498  
499 =cut
500  
501 sub getclustermean {
502 my ($data,$mask,$cids,$cdata,$cmask) = @_;
503 return getclustercentroids($dat,$mask,$cids,$cdata,$cmask,'a');
504 }
505  
506 #line 620 "Cluster.pd"
507  
508 =pod
509  
510 =head2 getclustermedian
511  
512 =for sig
513  
514 Signature: (
515 double data(d,n);
516 int mask(d,n);
517 int clusterids(n);
518 double [o]cdata(d,k);
519 int [o]cmask(d,k);
520 )
521  
522 Really just a wrapper for getclustercentroids(...,"m").
523  
524 =cut
525  
526 sub getclustermedian {
527 my ($data,$mask,$cids,$cdata,$cmask) = @_;
528 return getclustercentroids($dat,$mask,$cids,$cdata,$cmask,'m');
529 }
530 #line 531 "Cluster.pm"
531  
532  
533 =head2 getclustermedoids
534  
535 =for sig
536  
537 Signature: (
538 double distance(n,n);
539 int clusterids(n);
540 int [o]centroids(k);
541 double [o]errors(k);
542 )
543 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
544 float double ldouble)
545  
546 =for usage
547  
548 ($centroids, $errors) = getclustermedoids($distance, $clusterids);
549 getclustermedoids($distance, $clusterids, $centroids, $errors); # all arguments given
550 ($centroids, $errors) = $distance->getclustermedoids($clusterids); # method call
551 $distance->getclustermedoids($clusterids, $centroids, $errors);
552  
553 The getclustermedoid routine calculates the cluster centroids, given to which
554 cluster each element belongs. The centroid is defined as the element with the
555 smallest sum of distances to the other elements.
556  
557 =pod
558  
559 Broadcasts over its inputs.
560  
561 =for bad
562  
563 C<getclustermedoids> does not process bad values.
564 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
565  
566 =cut
567  
568  
569  
570  
571 9 *getclustermedoids = \&PDL::getclustermedoids;
572  
573  
574  
575  
576  
577  
578 =head2 kcluster
579  
580 =for sig
581  
582 Signature: (
583 int nclusters();
584 double data(d,n);
585 int mask(d,n);
586 double weight(d);
587 int npass();
588 int [o]clusterids(n);
589 double [o]error();
590 int [o]nfound();
591 ;
592 char *distFlag;
593 char *ctrMethodFlag;
594 )
595 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
596 float double ldouble)
597  
598 =for usage
599  
600 ($clusterids, $error, $nfound) = kcluster($nclusters, $data, $mask, $weight, $npass, $distFlag, $ctrMethodFlag);
601 kcluster($nclusters, $data, $mask, $weight, $npass, $clusterids, $error, $nfound, $distFlag, $ctrMethodFlag); # all arguments given
602 ($clusterids, $error, $nfound) = $nclusters->kcluster($data, $mask, $weight, $npass, $distFlag, $ctrMethodFlag); # method call
603 $nclusters->kcluster($data, $mask, $weight, $npass, $clusterids, $error, $nfound, $distFlag, $ctrMethodFlag);
604  
605 K-Means clustering algorithm. The "ctrMethodFlag" determines how
606 clusters centroids are to be computed; see getclustercentroids() for details.
607  
608 Because the C library code reads from the C<clusterids> if and only if
609 C<npass> is 0, before writing to it, it would be inconvenient to
610 set it to C<[io]>. However for efficiency reasons, as of 2.096, PDL
611 will not convert it (force a read-back on the conversion) for you
612 if you pass in the wrongly-typed data. This means that you should
613 be careful to pass in C<long> data of the right size if you set C<npass>
614 to 0.
615  
616 See also: kmedoids().
617  
618 =pod
619  
620 Broadcasts over its inputs.
621  
622 =for bad
623  
624 C<kcluster> does not process bad values.
625 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
626  
627 =cut
628  
629  
630  
631  
632 9 *kcluster = \&PDL::kcluster;
633  
634  
635  
636  
637  
638  
639 =head2 kmedoids
640  
641 =for sig
642  
643 Signature: (
644 int nclusters();
645 double distance(n,n);
646 int npass();
647 int [o]clusterids(n);
648 double [o]error();
649 int [o]nfound();
650 )
651 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
652 float double ldouble)
653  
654 =for usage
655  
656 ($clusterids, $error, $nfound) = kmedoids($nclusters, $distance, $npass);
657 kmedoids($nclusters, $distance, $npass, $clusterids, $error, $nfound); # all arguments given
658 ($clusterids, $error, $nfound) = $nclusters->kmedoids($distance, $npass); # method call
659 $nclusters->kmedoids($distance, $npass, $clusterids, $error, $nfound);
660  
661 K-Medoids clustering algorithm (uses distance matrix).
662  
663 See also: kcluster().
664  
665 =pod
666  
667 Broadcasts over its inputs.
668  
669 =for bad
670  
671 C<kmedoids> does not process bad values.
672 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
673  
674 =cut
675  
676  
677  
678  
679 9 *kmedoids = \&PDL::kmedoids;
680  
681  
682  
683  
684  
685  
686 =head2 treecluster
687  
688 =for sig
689  
690 Signature: (
691 double data(d,n);
692 int mask(d,n);
693 double weight(d);
694 int [o]tree(2,n);
695 double [o]lnkdist(n);
696 ;
697 char *distFlag;
698 char *methodFlag;
699 )
700 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
701 float double ldouble)
702  
703 =for usage
704  
705 ($tree, $lnkdist) = treecluster($data, $mask, $weight, $distFlag, $methodFlag);
706 treecluster($data, $mask, $weight, $tree, $lnkdist, $distFlag, $methodFlag); # all arguments given
707 ($tree, $lnkdist) = $data->treecluster($mask, $weight, $distFlag, $methodFlag); # method call
708 $data->treecluster($mask, $weight, $tree, $lnkdist, $distFlag, $methodFlag);
709  
710 Hierachical agglomerative clustering.
711  
712 $tree(2,n) represents the clustering solution.
713 Each row in the matrix describes one linking event,
714 with the two columns containing the name of the nodes that were joined.
715 The original genes are numbered 0..(n-1), nodes are numbered
716 -1..-(n-1).
717 $tree(2,n) thus actually uses only (2,n-1) cells.
718  
719 $lnkdist(n) represents the distance between the two subnodes that were joined.
720 As for $tree(), $lnkdist() uses only (n-1) cells.
721  
722 =pod
723  
724 Broadcasts over its inputs.
725  
726 =for bad
727  
728 C<treecluster> does not process bad values.
729 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
730  
731 =cut
732  
733  
734  
735  
736 9 *treecluster = \&PDL::treecluster;
737  
738  
739  
740  
741  
742  
743 =head2 treeclusterd
744  
745 =for sig
746  
747 Signature: (
748 double data(d,n);
749 int mask(d,n);
750 double weight(d);
751 double distances(n,n);
752 int [o]tree(2,n);
753 double [o]lnkdist(n);
754 ;
755 char *distFlag;
756 char *methodFlag;
757 )
758 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
759 float double ldouble)
760  
761 =for usage
762  
763 ($tree, $lnkdist) = treeclusterd($data, $mask, $weight, $distances, $distFlag, $methodFlag);
764 treeclusterd($data, $mask, $weight, $distances, $tree, $lnkdist, $distFlag, $methodFlag); # all arguments given
765 ($tree, $lnkdist) = $data->treeclusterd($mask, $weight, $distances, $distFlag, $methodFlag); # method call
766 $data->treeclusterd($mask, $weight, $distances, $tree, $lnkdist, $distFlag, $methodFlag);
767  
768 Hierachical agglomerative clustering using given distance matrix.
769  
770 See distancematrix() and treecluster(), above.
771  
772 =pod
773  
774 Broadcasts over its inputs.
775  
776 =for bad
777  
778 C<treeclusterd> does not process bad values.
779 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
780  
781 =cut
782  
783  
784  
785  
786 9 *treeclusterd = \&PDL::treeclusterd;
787  
788  
789  
790  
791  
792  
793 =head2 cuttree
794  
795 =for sig
796  
797 Signature: (
798 int tree(2,n);
799 int nclusters();
800 int [o]clusterids(n);
801 )
802 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
803 float double ldouble)
804  
805 =for usage
806  
807 $clusterids = cuttree($tree, $nclusters);
808 cuttree($tree, $nclusters, $clusterids); # all arguments given
809 $clusterids = $tree->cuttree($nclusters); # method call
810 $tree->cuttree($nclusters, $clusterids);
811  
812 Cluster selection for hierarchical clustering trees.
813  
814 =pod
815  
816 Broadcasts over its inputs.
817  
818 =for bad
819  
820 C<cuttree> does not process bad values.
821 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
822  
823 =cut
824  
825  
826  
827  
828 9 *cuttree = \&PDL::cuttree;
829  
830  
831  
832  
833  
834  
835 =head2 somcluster
836  
837 =for sig
838  
839 Signature: (
840 double data(d,n);
841 int mask(d,n);
842 double weight(d);
843 int nxnodes();
844 int nynodes();
845 double inittau();
846 int niter();
847 int [o]clusterids(2,n);
848 ; char *distFlag;
849 )
850 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
851 float double ldouble)
852  
853 =for usage
854  
855 $clusterids = somcluster($data, $mask, $weight, $nxnodes, $nynodes, $inittau, $niter, $distFlag);
856 somcluster($data, $mask, $weight, $nxnodes, $nynodes, $inittau, $niter, $clusterids, $distFlag); # all arguments given
857 $clusterids = $data->somcluster($mask, $weight, $nxnodes, $nynodes, $inittau, $niter, $distFlag); # method call
858 $data->somcluster($mask, $weight, $nxnodes, $nynodes, $inittau, $niter, $clusterids, $distFlag);
859  
860 =for ref
861  
862 Self-Organizing Map clustering, does not return centroid data.
863  
864 =pod
865  
866 Broadcasts over its inputs.
867  
868 =for bad
869  
870 C<somcluster> does not process bad values.
871 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
872  
873 =cut
874  
875  
876  
877  
878 9 *somcluster = \&PDL::somcluster;
879  
880  
881  
882  
883  
884  
885 =head2 pca
886  
887 =for sig
888  
889 Signature: (
890 double [o]U(d,n);
891 double [o]S(d);
892 double [o]V(d,d);
893 )
894 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
895 float double ldouble)
896  
897 =for usage
898  
899 ($U, $S, $V) = pca();
900 pca($U, $S, $V); # all arguments given
901  
902 Principal Component Analysis (SVD), operates in-place on $U() and requires ($SIZE(n) E<gt>= $SIZE(d)).
903  
904 =pod
905  
906 Broadcasts over its inputs.
907  
908 =for bad
909  
910 C<pca> does not process bad values.
911 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
912  
913 =cut
914  
915  
916  
917  
918 9 *pca = \&PDL::pca;
919  
920  
921  
922  
923  
924  
925 =head2 rowdistances
926  
927 =for sig
928  
929 Signature: (
930 double data(d,n);
931 int mask(d,n);
932 double weight(d);
933 int rowids1(ncmps);
934 int rowids2(ncmps);
935 double [o]dist(ncmps);
936 ; char *distFlag;
937 )
938 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
939 float double ldouble)
940  
941 =for usage
942  
943 $dist = rowdistances($data, $mask, $weight, $rowids1, $rowids2, $distFlag);
944 rowdistances($data, $mask, $weight, $rowids1, $rowids2, $dist, $distFlag); # all arguments given
945 $dist = $data->rowdistances($mask, $weight, $rowids1, $rowids2, $distFlag); # method call
946 $data->rowdistances($mask, $weight, $rowids1, $rowids2, $dist, $distFlag);
947  
948 Computes pairwise distances between rows of $data().
949 $rowids1() contains the row-indices of the left (first) comparison operand,
950 and $rowids2() the row-indices of the right (second) comparison operand. Since each
951 of these are assumed to be indices into the first dimension $data(), it should be the case that:
952  
953 0 <= $rowids1(i),rowids2(i) < $SIZE(n) for 0 <= i < $SIZE(ncmps)
954  
955 See also clusterdistance(), clusterdistances(), clusterdistancematrixenc(), distancematrix().
956  
957 =pod
958  
959 Broadcasts over its inputs.
960  
961 =for bad
962  
963 C<rowdistances> does not process bad values.
964 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
965  
966 =cut
967  
968  
969  
970  
971 9 *rowdistances = \&PDL::rowdistances;
972  
973  
974  
975  
976  
977  
978 =head2 clusterdistances
979  
980 =for sig
981  
982 Signature: (
983 double data(d,n);
984 int mask(d,n);
985 double weight(d);
986 int rowids(nr);
987 int index2(n2);
988 double [o]dist(nr);
989 ;
990 char *distFlag;
991 char *methodFlag;
992 )
993 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
994 float double ldouble)
995  
996 =for usage
997  
998 $dist = clusterdistances($data, $mask, $weight, $rowids, $index2, $distFlag, $methodFlag);
999 clusterdistances($data, $mask, $weight, $rowids, $index2, $dist, $distFlag, $methodFlag); # all arguments given
1000 $dist = $data->clusterdistances($mask, $weight, $rowids, $index2, $distFlag, $methodFlag); # method call
1001 $data->clusterdistances($mask, $weight, $rowids, $index2, $dist, $distFlag, $methodFlag);
1002  
1003 Computes pairwise distance(s) from each of $rowids() as a singleton cluster
1004 with the cluster represented by $index2(), which should be an index
1005 vector as for clusterdistance(). See also clusterdistancematrixenc().
1006  
1007 =pod
1008  
1009 Broadcasts over its inputs.
1010  
1011 =for bad
1012  
1013 C<clusterdistances> does not process bad values.
1014 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
1015  
1016 =cut
1017  
1018  
1019  
1020  
1021 9 *clusterdistances = \&PDL::clusterdistances;
1022  
1023  
1024  
1025  
1026  
1027  
1028 =head2 clustersizes
1029  
1030 =for sig
1031  
1032 Signature: (int clusterids(n); int [o]clustersizes(k))
1033 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
1034 float double ldouble)
1035  
1036 =for usage
1037  
1038 $clustersizes = clustersizes($clusterids);
1039 clustersizes($clusterids, $clustersizes); # all arguments given
1040 $clustersizes = $clusterids->clustersizes; # method call
1041 $clusterids->clustersizes($clustersizes);
1042  
1043 Computes the size (number of elements) of each cluster in $clusterids().
1044 Useful for allocating less than maximmal space for $clusterelements().
1045  
1046 =pod
1047  
1048 Broadcasts over its inputs.
1049  
1050 =for bad
1051  
1052 The output piddle should never be marked BAD.
1053  
1054 =cut
1055  
1056  
1057  
1058  
1059 9 *clustersizes = \&PDL::clustersizes;
1060  
1061  
1062  
1063  
1064  
1065  
1066 =head2 clusterelements
1067  
1068 =for sig
1069  
1070 Signature: (int clusterids(n); int [o]clustersizes(k); int [o]eltids(mcsize,k))
1071 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
1072 float double ldouble)
1073  
1074 =for usage
1075  
1076 ($clustersizes, $eltids) = clusterelements($clusterids);
1077 clusterelements($clusterids, $clustersizes, $eltids); # all arguments given
1078 ($clustersizes, $eltids) = $clusterids->clusterelements; # method call
1079 $clusterids->clusterelements($clustersizes, $eltids);
1080  
1081 Converts the vector $clusterids() to a matrix $eltids() of element (row) indices
1082 indexed by cluster-id. $mcsize() is the maximum number of elements per cluster,
1083 at most $n. The output PDLs $clustersizes() and $eltids() can be passed to
1084 clusterdistancematrix().
1085  
1086 =pod
1087  
1088 Broadcasts over its inputs.
1089  
1090 =for bad
1091  
1092 C<clusterelements> does not process bad values.
1093 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
1094  
1095 =cut
1096  
1097  
1098  
1099  
1100 9 *clusterelements = \&PDL::clusterelements;
1101  
1102  
1103  
1104  
1105  
1106  
1107 =head2 clusterelementmask
1108  
1109 =for sig
1110  
1111 Signature: (int clusterids(n); byte [o]eltmask(k,n))
1112 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
1113 float double ldouble)
1114  
1115 =for usage
1116  
1117 $eltmask = clusterelementmask($clusterids);
1118 clusterelementmask($clusterids, $eltmask); # all arguments given
1119 $eltmask = $clusterids->clusterelementmask; # method call
1120 $clusterids->clusterelementmask($eltmask);
1121  
1122 Get boolean membership mask $eltmask() based on cluster assignment in $clusterids().
1123 No value in $clusterids() may be greater than or equal to $k.
1124 On completion, $eltmask(k,n) is a true value iff $clusterids(n)=$k.
1125  
1126 =pod
1127  
1128 Broadcasts over its inputs.
1129  
1130 =for bad
1131  
1132 C<clusterelementmask> does not process bad values.
1133 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
1134  
1135 =cut
1136  
1137  
1138  
1139  
1140 9 *clusterelementmask = \&PDL::clusterelementmask;
1141  
1142  
1143  
1144  
1145  
1146  
1147 =head2 clusterdistancematrix
1148  
1149 =for sig
1150  
1151 Signature: (
1152 double data(d,n);
1153 int mask(d,n);
1154 double weight(d);
1155 int rowids(nr);
1156 int clustersizes(k);
1157 int eltids(mcsize,k);
1158 double [o]dist(k,nr);
1159 ;
1160 char *distFlag;
1161 char *methodFlag;
1162 )
1163 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
1164 float double ldouble)
1165  
1166 =for usage
1167  
1168 $dist = clusterdistancematrix($data, $mask, $weight, $rowids, $clustersizes, $eltids, $distFlag, $methodFlag);
1169 clusterdistancematrix($data, $mask, $weight, $rowids, $clustersizes, $eltids, $dist, $distFlag, $methodFlag); # all arguments given
1170 $dist = $data->clusterdistancematrix($mask, $weight, $rowids, $clustersizes, $eltids, $distFlag, $methodFlag); # method call
1171 $data->clusterdistancematrix($mask, $weight, $rowids, $clustersizes, $eltids, $dist, $distFlag, $methodFlag);
1172  
1173 B<DEPRECATED> in favor of clusterdistancematrixenc().
1174 In the future, this method is expected to become a wrapper for clusterdistancematrixenc().
1175  
1176 Computes distance between each row index in $rowids()
1177 considered as a singleton cluster
1178 and each of the $k clusters whose elements are given by a single row of $eltids().
1179 $clustersizes() and $eltids() are as output by the clusterelements() method.
1180  
1181 See also clusterdistance(), clusterdistances(), clustersizes(), clusterelements(), clusterdistancematrixenc().
1182  
1183 =pod
1184  
1185 Broadcasts over its inputs.
1186  
1187 =for bad
1188  
1189 C<clusterdistancematrix> does not process bad values.
1190 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
1191  
1192 =cut
1193  
1194  
1195  
1196  
1197 9 *clusterdistancematrix = \&PDL::clusterdistancematrix;
1198  
1199  
1200  
1201  
1202  
1203 #line 1196 "Cluster.pd"
1204  
1205 =pod
1206  
1207 =head2 clusterenc
1208  
1209 =for sig
1210  
1211 Signature: (
1212 int clusterids(n);
1213 int [o]clusterlens(k1);
1214 int [o]clustervals(k1);
1215 int [o]clusterrows(n);
1216 ;
1217 int k1;
1218 )
1219  
1220 Encodes datum-to-cluster vector $clusterids() for efficiently mapping
1221 clusters-to-data. Returned PDL $clusterlens() holds the lengths of each
1222 cluster containing at least one element. $clustervals() holds the IDs
1223 of such clusters as they appear as values in $clusterids(). $clusterrows()
1224 is such that:
1225  
1226 all( rld($clusterlens, $clustervals) == $clusterids )
1227  
1228 ... if all available cluster-ids are in use.
1229  
1230 If specified, $k1 is a perl scalar
1231 holding the number of clusters (maximum cluster index + 1); an
1232 appropriate value will guessed from $clusterids() otherwise.
1233  
1234 Really just a wrapper for some lower-level PDL and PDL::Cluster calls.
1235  
1236 =cut
1237  
1238 sub clusterenc {
1239 my ($cids, $clens,$cvals,$crows, $kmax) = @_;
1240 $kmax = $cids->max+1 if (!defined($kmax));
1241  
1242 ##-- cluster sizes
1243 $clens = zeroes(long, $kmax) if (!defined($clens));
1244 clustersizes($cids,$clens);
1245  
1246 ##-- cluster-id values
1247 if (!defined($cvals)) { $cvals = PDL->sequence(long,$kmax); }
1248 else { $cvals .= PDL->sequence(long,$kmax); }
1249  
1250 ##-- cluster-row values: handle BAD and negative values
1251 #if (!defined($crows)) { $crows = $cids->qsorti->where($cids->isgood & $cids>=0); }
1252 #else { $crows .= $cids->qsorti->where($cids->isgood & $cids>=0); }
1253  
1254 ##-- cluster-row values: treat BAD and negative values like anything else
1255 if (!defined($crows)) { $crows = $cids->qsorti; }
1256 else { $crows .= $cids->qsorti; }
1257  
1258 return ($clens,$cvals,$crows);
1259 }
1260  
1261 #line 1262 "Cluster.pd"
1262  
1263 =pod
1264  
1265 =head2 clusterdec
1266  
1267 =for sig
1268  
1269 Signature: (
1270 int clusterlens(k1);
1271 int clustervals(k1);
1272 int clusterrows(n);
1273 int [o]clusterids(n);
1274 )
1275  
1276 Decodes cluster-to-datum vectors ($clusterlens,$clustervals,$clusterrows)
1277 into a single datum-to-cluster vector $clusterids().
1278 $(clusterlens,$clustervals,$clusterrows) are as returned by the clusterenc() method.
1279  
1280 Un-addressed row-index values in $clusterrows() will be assigned the pseudo-cluster (-1)
1281 in $clusterids().
1282  
1283 Really just a wrapper for some lower-level PDL calls.
1284  
1285 =cut
1286  
1287 sub clusterdec {
1288 my ($clens,$cvals,$crows, $cids2) = @_;
1289  
1290 ##-- get $cids
1291 $cids2 = zeroes($cvals->type, $crows->dims) if (!defined($cids2));
1292 $cids2 .= -1;
1293  
1294 ##-- trim $crows
1295 #my $crows_good = $crows->slice("0:".($clens->sum-1)); ##-- assume bad indices are at END of $crows (BAD,inf,...)
1296 my $crows_good = $crows->slice(-$clens->sum.":-1"); ##-- assume bad indices are at BEGINNING of $crows (-1, ...)
1297  
1298 ##-- decode
1299 $clens->rld($cvals, $cids2->index($crows_good));
1300  
1301 return $cids2;
1302 }
1303  
1304 #line 1312 "Cluster.pd"
1305  
1306 =pod
1307  
1308 =head2 clusteroffsets
1309  
1310 =for sig
1311  
1312 Signature: (
1313 int clusterids(n);
1314 int [o]clusteroffsets(k1+1);
1315 int [o]clustervals(k1);
1316 int [o]clusterrows(n);
1317 ;
1318 int k1;
1319 )
1320  
1321 Encodes datum-to-cluster vector $clusterids() for efficiently mapping
1322 clusters-to-data. Like clusterenc(), but returns cumulative offsets
1323 instead of lengths.
1324  
1325 Really just a wrapper for clusterenc(), cumusumover(), and append().
1326  
1327 =cut
1328  
1329 sub clusteroffsets {
1330 my ($cids, $coffsets,$cvals,$crows, $kmax) = @_;
1331 my ($clens);
1332 ($clens,$cvals,$crows) = clusterenc($cids,undef,$cvals,$crows,$kmax);
1333 $coffsets = $clens->append(0)->rotate(1)->cumusumover;
1334  
1335 return ($coffsets,$cvals,$crows);
1336 }
1337 #line 1338 "Cluster.pm"
1338  
1339  
1340 =head2 clusterdistancematrixenc
1341  
1342 =for sig
1343  
1344 Signature: (
1345 double data(d,n);
1346 int mask(d,n);
1347 double weight(d);
1348 int clens1(k1);
1349 int crowids1(nc1);
1350 int clens2(k2);
1351 int crowids2(nc2);
1352 double [o]dist(k1,k2);
1353 ;
1354 char *distFlag;
1355 char *methodFlag;
1356 )
1357 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
1358 float double ldouble)
1359  
1360 =for usage
1361  
1362 $dist = clusterdistancematrixenc($data, $mask, $weight, $clens1, $crowids1, $clens2, $crowids2, $distFlag, $methodFlag);
1363 clusterdistancematrixenc($data, $mask, $weight, $clens1, $crowids1, $clens2, $crowids2, $dist, $distFlag, $methodFlag); # all arguments given
1364 $dist = $data->clusterdistancematrixenc($mask, $weight, $clens1, $crowids1, $clens2, $crowids2, $distFlag, $methodFlag); # method call
1365 $data->clusterdistancematrixenc($mask, $weight, $clens1, $crowids1, $clens2, $crowids2, $dist, $distFlag, $methodFlag);
1366  
1367 Computes cluster-distance between each pair of clusters in (sequence($k1) x sequence($k2)), where 'x'
1368 is the Cartesian product. Cluster contents are passed as pairs ($clens(),$crowids()) as returned
1369 by the clusterenc() function (assuming that the $cvals() vector returned by clusterenc() is a flat sequence).
1370  
1371 The deprecated method clusterdistancematrix() can be simulated by this function in the following
1372 manner: if a clusterdistancematrix() call was:
1373  
1374 clustersizes ($cids, $csizes=zeroes(long,$k));
1375 clusterelements($cids, $celts =zeroes(long,$csizes->max)-1);
1376 clusterdistancematrix($data,$msk,$wt, $rowids, $csizes,$celts,
1377 $cdmat=zeroes(double,$k,$rowids->dim(0)),
1378 $distFlag, $methodFlag
1379 );
1380  
1381 Then the corresponding use of clusterdistancematrixenc() would be:
1382  
1383 ($clens,$cvals,$crows) = clusterenc($cids);
1384 clusterdistancematrixenc($data,$msk,$wt,
1385 $clens, $crows, ##-- "real" clusters in output dim 0
1386 $rowids->ones, $rowids, ##-- $rowids as singleton clusters in output dim 1
1387 $cdmat=zeroes(double,$clens->dim(0),$rowids->dim(0)),
1388 $distFlag, $methodFlag);
1389  
1390 If your $cvals() are not a flat sequence, you will probably need to do some index-twiddling
1391 to get things into the proper shape:
1392  
1393 if ( !all($cvals==$cvals->sequence) || $cvals->dim(0) != $k )
1394 {
1395 my $cdmat0 = $cdmat;
1396 my $nr = $rowids->dim(0);
1397 $cdmat = pdl(double,"inf")->slice("*$k,*$nr")->make_physical(); ##-- "missing" distances are infinite
1398 $cdmat->dice_axis(0,$cvals) .= $cdmat0;
1399 }
1400  
1401 $distFlag and $methodFlag are interpreted as for clusterdistance().
1402  
1403 See also clusterenc(), clusterdistancematrix().
1404  
1405 =pod
1406  
1407 Broadcasts over its inputs.
1408  
1409 =for bad
1410  
1411 C<clusterdistancematrixenc> does not process bad values.
1412 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
1413  
1414 =cut
1415  
1416  
1417  
1418  
1419 9 *clusterdistancematrixenc = \&PDL::clusterdistancematrixenc;
1420  
1421  
1422  
1423  
1424  
1425  
1426 =head2 clusterdistancesenc
1427  
1428 =for sig
1429  
1430 Signature: (
1431 double data(d,n);
1432 int mask(d,n);
1433 double weight(d);
1434 int coffsets1(k1);
1435 int crowids1(nc1);
1436 int cwhich1(ncmps);
1437 int coffsets2(k2);
1438 int crowids2(nc2);
1439 int cwhich2(ncmps);
1440 double [o]dists(ncmps);
1441 ;
1442 char *distFlag;
1443 char *methodFlag;
1444 )
1445 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
1446 float double ldouble)
1447  
1448 =for usage
1449  
1450 $dists = clusterdistancesenc($data, $mask, $weight, $coffsets1, $crowids1, $cwhich1, $coffsets2, $crowids2, $cwhich2, $distFlag, $methodFlag);
1451 clusterdistancesenc($data, $mask, $weight, $coffsets1, $crowids1, $cwhich1, $coffsets2, $crowids2, $cwhich2, $dists, $distFlag, $methodFlag); # all arguments given
1452 $dists = $data->clusterdistancesenc($mask, $weight, $coffsets1, $crowids1, $cwhich1, $coffsets2, $crowids2, $cwhich2, $distFlag, $methodFlag); # method call
1453 $data->clusterdistancesenc($mask, $weight, $coffsets1, $crowids1, $cwhich1, $coffsets2, $crowids2, $cwhich2, $dists, $distFlag, $methodFlag);
1454  
1455 Computes cluster-distance between selected pairs of co-indexed clusters in ($cwhich1,$cwhich2).
1456 Cluster contents are passed as pairs ($coffsetsX(),$crowidsX()) as returned
1457 by the clusteroffsets() function.
1458  
1459 $distFlag and $methodFlag are interpreted as for clusterdistance().
1460  
1461 See also clusterenc(), clusterdistancematrixenc().
1462  
1463 =pod
1464  
1465 Broadcasts over its inputs.
1466  
1467 =for bad
1468  
1469 C<clusterdistancesenc> does not process bad values.
1470 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
1471  
1472 =cut
1473  
1474  
1475  
1476  
1477 9 *clusterdistancesenc = \&PDL::clusterdistancesenc;
1478  
1479  
1480  
1481  
1482  
1483  
1484 =head2 getclusterwsum
1485  
1486 =for sig
1487  
1488 Signature: (
1489 double data(d,n);
1490 int mask(d,n);
1491 double clusterwts(k,n);
1492 double [o]cdata(d,k);
1493 int [o]cmask(d,k);
1494 )
1495 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
1496 float double ldouble)
1497  
1498 =for usage
1499  
1500 ($cdata, $cmask) = getclusterwsum($data, $mask, $clusterwts);
1501 getclusterwsum($data, $mask, $clusterwts, $cdata, $cmask); # all arguments given
1502 ($cdata, $cmask) = $data->getclusterwsum($mask, $clusterwts); # method call
1503 $data->getclusterwsum($mask, $clusterwts, $cdata, $cmask);
1504  
1505 Find cluster centroids by weighted sum. This can be considered an
1506 expensive generalization of the getclustermean() and getclustermedian()
1507 functions. Here, the input PDLs $data() and $mask(), as well as the
1508 output PDL $cdata() are as for getclustermean(). The matrix $clusterwts()
1509 determines the relative weight of each data row in determining the
1510 centroid of each cluster, potentially useful for "fuzzy" clustering.
1511 The equation used to compute cluster means is:
1512  
1513 $cdata(d,k) = sum_{n} $clusterwts(k,n) * $data(d,n) * $mask(d,n)
1514  
1515 For centroids in the same range as data elements, $clusterwts()
1516 should sum to 1 over each column (k):
1517  
1518 all($clusterwts->xchg(0,1)->sumover == 1)
1519  
1520 getclustermean() can be simulated by instantiating $clusterwts() with
1521 a uniform distribution over cluster elements:
1522  
1523 $clusterwts = zeroes($k,$n);
1524 $clusterwts->indexND(cat($clusterids, xvals($clusterids))->xchg(0,1)) .= 1;
1525 $clusterwts /= $clusterwts->xchg(0,1)->sumover;
1526 getclusterwsum($data,$mask, $clusterwts, $cdata=zeroes($d,$k));
1527  
1528 Similarly, getclustermedian() can be simulated by setting $clusterwts() to
1529 1 for cluster medians and otherwise to 0. More sophisticated centroid
1530 discovery methods can be computed by this function by setting
1531 $clusterwts(k,n) to some estimate of the conditional probability
1532 of the datum at row $n given the cluster with index $k:
1533 p(Elt==n|Cluster==k). One
1534 way to achieve such an estimate is to use (normalized inverses of) the
1535 singleton-row-to-cluster distances as output by clusterdistancematrix().
1536  
1537 =pod
1538  
1539 Broadcasts over its inputs.
1540  
1541 =for bad
1542  
1543 C<getclusterwsum> does not process bad values.
1544 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
1545  
1546 =cut
1547  
1548  
1549  
1550  
1551 9 *getclusterwsum = \&PDL::getclusterwsum;
1552  
1553  
1554  
1555  
1556  
1557  
1558 =head2 attachtonearest
1559  
1560 =for sig
1561  
1562 Signature: (
1563 double data(d,n);
1564 int mask(d,n);
1565 double weight(d);
1566 int rowids(nr);
1567 double cdata(d,k);
1568 int cmask(d,k);
1569 int [o]clusterids(nr);
1570 double [o]cdist(nr);
1571 ;
1572 char *distFlag;
1573 char *methodFlag;
1574 )
1575 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
1576 float double ldouble)
1577  
1578 =for usage
1579  
1580 ($clusterids, $cdist) = attachtonearest($data, $mask, $weight, $rowids, $cdata, $cmask, $distFlag, $methodFlag);
1581 attachtonearest($data, $mask, $weight, $rowids, $cdata, $cmask, $clusterids, $cdist, $distFlag, $methodFlag); # all arguments given
1582 ($clusterids, $cdist) = $data->attachtonearest($mask, $weight, $rowids, $cdata, $cmask, $distFlag, $methodFlag); # method call
1583 $data->attachtonearest($mask, $weight, $rowids, $cdata, $cmask, $clusterids, $cdist, $distFlag, $methodFlag);
1584  
1585 Assigns each specified data row to the nearest cluster centroid.
1586 Data elements are given by $data() and $mask(), feature weights are
1587 given by $weight(), as usual. Cluster centroids are defined by
1588 by $cdata() and $cmask(), and the indices of rows to be attached
1589 are given in the vector $rowids(). The output vector $clusterids()
1590 contains for each specified row index the identifier of the nearest
1591 cluster centroid. The vector $cdist() contains the distance to
1592 the best clusters.
1593  
1594 See also: clusterdistancematrix(), attachtonearestd().
1595  
1596 =pod
1597  
1598 Broadcasts over its inputs.
1599  
1600 =for bad
1601  
1602 C<attachtonearest> does not process bad values.
1603 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
1604  
1605 =cut
1606  
1607  
1608  
1609  
1610 9 *attachtonearest = \&PDL::attachtonearest;
1611  
1612  
1613  
1614  
1615  
1616 #line 1659 "Cluster.pd"
1617  
1618 =pod
1619  
1620 =head2 attachtonearestd
1621  
1622 =for sig
1623  
1624 Signature: (
1625 double cdistmat(k,n);
1626 int rowids(nr);
1627 int [o]clusterids(nr);
1628 double [o]dists(nr);
1629 )
1630  
1631 Assigns each specified data row to the nearest cluster centroid,
1632 as for attachtonearest(), given the datum-to-cluster distance
1633 matrix $cdistmat(). Currently just a wrapper for a few PDL calls.
1634 In scalar context returns $clusterids(), in list context returns
1635 the list ($clusterids(),$dists()).
1636  
1637 =cut
1638  
1639 sub attachtonearestd {
1640 my ($cdm,$rowids,$cids,$dists)=@_;
1641 $cids = zeroes(long, $rowids->dim(0)) if (!defined($cids));
1642 $dists = zeroes(double, $rowids->dim(0)) if (!defined($dists));
1643  
1644 ##-- dice matrix
1645 my $cdmr = $cdm->dice_axis(1,$rowids);
1646  
1647 ##-- get best
1648 $cdmr->minimum_ind($cids);
1649 $dists .= $cdmr->index($cids);
1650  
1651 return wantarray ? ($cids,$dists) : $cids;
1652 }
1653 #line 1654 "Cluster.pm"
1654  
1655  
1656 =head2 checkprototypes
1657  
1658 =for sig
1659  
1660 Signature: (
1661 protos(k);
1662 [o]cprotos(k);
1663 byte [t]otmp(n);
1664 ; int nsize => n)
1665 Types: (byte short ushort long)
1666  
1667 =for usage
1668  
1669 $cprotos = checkprototypes($protos, $nsize);
1670 checkprototypes($protos, $cprotos, $nsize); # all arguments given
1671 $cprotos = $protos->checkprototypes($nsize); # method call
1672 $protos->checkprototypes($cprotos, $nsize);
1673 $protos->inplace->checkprototypes($nsize); # can be used inplace
1674 checkprototypes($protos->inplace, $nsize);
1675  
1676 (Deterministic)
1677  
1678 Ensure that the assignment $protos() from $k objects to
1679 integer "prototype" indices in the range [0,$n( contains no repetitions of any
1680 of the $n possible prototype values. One use for this function is
1681 the restriction of (randomly generated) potential clustering solutions
1682 for $k clusters in which each cluster is represented by a
1683 "prototypical" element from a data sample of size $n.
1684  
1685 Requires: $n >= $k.
1686  
1687 =pod
1688  
1689 Can operate inplace with C<protos> as output C<cprotos>.
1690 Broadcasts over its inputs.
1691  
1692 =for bad
1693  
1694 C<checkprototypes> does not process bad values.
1695 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
1696  
1697 =cut
1698  
1699  
1700  
1701  
1702 9 *checkprototypes = \&PDL::checkprototypes;
1703  
1704  
1705  
1706  
1707  
1708  
1709 =head2 checkpartitions
1710  
1711 =for sig
1712  
1713 Signature: (
1714 part(n);
1715 [o]cpart(n);
1716 [t]ptmp(k);
1717 ; int ksize => k)
1718 Types: (byte short ushort long)
1719  
1720 =for usage
1721  
1722 $cpart = checkpartitions($part, $ksize);
1723 checkpartitions($part, $cpart, $ksize); # all arguments given
1724 $cpart = $part->checkpartitions($ksize); # method call
1725 $part->checkpartitions($cpart, $ksize);
1726 $part->inplace->checkpartitions($ksize); # can be used inplace
1727 checkpartitions($part->inplace, $ksize);
1728  
1729 (Deterministic)
1730  
1731 Ensure that the partitioning $part() of $n objects into $k bins
1732 (identified by integer values in the range [0,$k-1])
1733 contains at least one instance of each of the
1734 $k possible values. One use for this function is
1735 the restriction of (randomly generated) potential clustering solutions
1736 for $n elements into $k clusters to those which assign at least one
1737 element to each cluster.
1738  
1739 Requires: $n >= $k.
1740  
1741 =pod
1742  
1743 Can operate inplace with C<part> as output C<cpart>.
1744 Broadcasts over its inputs.
1745  
1746 =for bad
1747  
1748 C<checkpartitions> does not process bad values.
1749 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
1750  
1751 =cut
1752  
1753  
1754  
1755  
1756 9 *checkpartitions = \&PDL::checkpartitions;
1757  
1758  
1759  
1760  
1761  
1762 #line 1813 "Cluster.pd"
1763  
1764 =pod
1765  
1766 =head2 randomprototypes
1767  
1768 =for sig
1769  
1770 Signature: (int k; int n; [o]prototypes(k))
1771  
1772 Generate a random set of $k prototype indices drawn from $n objects,
1773 ensuring that no object is used more than once. Calls checkprototypes().
1774  
1775 See also: checkprototypes(), randomassign(), checkpartitions(), randompartition().
1776  
1777 =cut
1778  
1779 sub randomprototypes {
1780 my ($k,$n,$protos) = @_;
1781 $protos = zeroes(long, $k) if (!defined($protos));
1782 $protos .= PDL->random($k)*$n;
1783 checkprototypes($protos->inplace, $n);
1784 return $protos;
1785 }
1786  
1787 #line 1845 "Cluster.pd"
1788  
1789 =pod
1790  
1791 =head2 randompartition
1792  
1793 =for sig
1794  
1795 Signature: (int k; int n; [o]partition(n))
1796  
1797 Generate a partitioning of $n objects into $k clusters,
1798 ensuring that every cluster contains at least one object.
1799 Calls checkpartitions().
1800 This method is identical in functionality to randomassign(),
1801 but may be faster if $k is significantly smaller than $n.
1802  
1803 See also: randomassign(), checkpartitions(), checkprototypes(), randomprototypes().
1804  
1805 =cut
1806  
1807 sub randompartition {
1808 my ($k,$n,$part) = @_;
1809 $part = zeroes(long, $n) if (!defined($part));
1810 $part .= PDL->random($n)*$k;
1811 checkpartitions($part->inplace, $k);
1812 return $part;
1813 }
1814  
1815 #line 1884 "Cluster.pd"
1816 ##---------------------------------------------------------------------
1817 =pod
1818  
1819 =head1 COMMON ARGUMENTS
1820  
1821 Many of the functions described above require one or
1822 more of the following parameters:
1823  
1824 =over 4
1825  
1826 =item d
1827  
1828 The number of features defined for each data element.
1829  
1830 =item n
1831  
1832 The number of data elements to be clustered.
1833  
1834 =item k
1835  
1836 =item nclusters
1837  
1838 The number of desired clusters.
1839  
1840 =item data(d,n)
1841  
1842 A matrix representing the data to be clustered, double-valued.
1843  
1844 =item mask(d,n)
1845  
1846 A matrix indicating which data values are missing. If
1847 mask(i,j) == 0, then data(i,j) is treated as missing.
1848  
1849 =item weights(d)
1850  
1851 The (feature-) weights that are used to calculate the distance.
1852  
1853 B<Warning:> Not all distance metrics make use of weights;
1854 you must provide some nonetheless.
1855  
1856 =item clusterids(n)
1857  
1858 A clustering solution. $clusterids() maps data elements
1859 (row indices in $data()) to values in the range [0,$k-1].
1860  
1861 =back
1862  
1863 =cut
1864  
1865 ##---------------------------------------------------------------------
1866 =pod
1867  
1868 =head2 Distance Metrics
1869  
1870 Distances between data elements (and cluster centroids, where applicable)
1871 are computed using one of a number of built-in metrics. Which metric
1872 is to be used for a given computation is indicated by a character
1873 flag denoted above with $distFlag(). In the following, w[i] represents
1874 a weighting factor in the $weights() matrix, and $W represents the total
1875 of all weights.
1876  
1877 Currently implemented distance
1878 metrics and the corresponding flags are:
1879  
1880 =over 4
1881  
1882 =item e
1883  
1884 Pseudo-Euclidean distance:
1885  
1886 dist_e(x,y) = 1/W * sum_{i=1..d} w[i] * (x[i] - y[i])^2
1887  
1888 Note that this is not the "true" Euclidean distance, which is defined as:
1889  
1890 dist_E(x,y) = sqrt( sum_{i=1..d} (x[i] - y[i])^2 )
1891  
1892 =item b
1893  
1894 City-block ("Manhattan") distance:
1895  
1896 dist_b(x,y) = 1/W * sum_{i=1..d} w[i] * |x[i] - y[i]|
1897  
1898 =item c
1899  
1900 Pearson correlation distance:
1901  
1902 dist_c(x,y) = 1-r(x,y)
1903  
1904 where r is the Pearson correlation coefficient:
1905  
1906 r(x,y) = 1/d * sum_{i=1..d} (x[i]-mean(x))/stddev(x) * (y[i]-mean(y))/stddev(y)
1907  
1908 =item a
1909  
1910 Absolute value of the correlation,
1911  
1912 dist_a(x,y) = 1-|r(x,y)|
1913  
1914 where r(x,y) is the Pearson correlation coefficient.
1915  
1916 =item u
1917  
1918 Uncentered correlation (cosine of the angle):
1919  
1920 dist_u(x,y) = 1-r_u(x,y)
1921  
1922 where:
1923  
1924 r_u(x,y) = 1/d * sum_{i=1..d} (x[i]/sigma0(x)) * (y[i]/sigma0(y))
1925  
1926 and:
1927  
1928 sigma0(w) = sqrt( 1/d * sum_{i=1..d} w[i]^2 )
1929  
1930 =item x
1931  
1932 Absolute uncentered correlation,
1933  
1934 dist_x(x,y) = 1-|r_u(x,y)|
1935  
1936 =item s
1937  
1938 Spearman's rank correlation.
1939  
1940 dist_s(x,y) = 1-r_s(x,y) ~= dist_c(ranks(x),ranks(y))
1941  
1942 where r_s(x,y) is the Spearman rank correlation. Weights are ignored.
1943  
1944 =item k
1945  
1946 Kendall's tau (does not use weights).
1947  
1948 dist_k(x,y) = 1 - tau(x,y)
1949  
1950 =item (other values)
1951  
1952 For other values of dist, the default (Euclidean distance) is used.
1953  
1954 =back
1955  
1956 =cut
1957  
1958 ##---------------------------------------------------------------------
1959 =pod
1960  
1961 =head2 Link Methods
1962  
1963 For hierarchical clustering, the 'link method' must be specified
1964 by a character flag, denoted above as $methodFlag.
1965 Known link methods are:
1966  
1967 =over 4
1968  
1969 =item s
1970  
1971 Pairwise minimum-linkage ("single") clustering.
1972  
1973 Defines the distance between two clusters as the
1974 least distance between any two of their respective elements.
1975  
1976 =item m
1977  
1978 Pairwise maximum-linkage ("complete") clustering.
1979  
1980 Defines the distance between two clusters as the
1981 greatest distance between any two of their respective elements.
1982  
1983 =item a
1984  
1985 Pairwise average-linkage clustering (centroid distance using arithmetic mean).
1986  
1987 Defines the distance between two clusters as the
1988 distance between their respective centroids, where each
1989 cluster centroid is defined as the arithmetic mean of
1990 that cluster's elements.
1991  
1992 =item c
1993  
1994 Pairwise centroid-linkage clustering (centroid distance using median).
1995  
1996 Identifies the distance between two clusters as the
1997 distance between their respective centroids, where each
1998 cluster centroid is computed as the median of
1999 that cluster's elements.
2000  
2001 =item (other values)
2002  
2003 Behavior for other values is currently undefined.
2004  
2005 =back
2006  
2007 For the first three, either the distance matrix or the gene expression data is
2008 sufficient to perform the clustering algorithm. For pairwise centroid-linkage
2009 clustering, however, the gene expression data are always needed, even if the
2010 distance matrix itself is available.
2011  
2012 =cut
2013  
2014 ##---------------------------------------------------------------------
2015 =pod
2016  
2017 =head1 ACKNOWLEDGEMENTS
2018  
2019 Perl by Larry Wall.
2020  
2021 PDL by Karl Glazebrook, Tuomas J. Lukka, Christian Soeller, and others.
2022  
2023 C Clustering Library by
2024 Michiel de Hoon,
2025 Seiya Imoto,
2026 and Satoru Miyano.
2027  
2028 Orignal Algorithm::Cluster module by John Nolan and Michiel de Hoon.
2029  
2030 =cut
2031  
2032 ##----------------------------------------------------------------------
2033 =pod
2034  
2035 =head1 KNOWN BUGS
2036  
2037 Dimensional requirements are sometimes too strict.
2038  
2039 Passing weights to Spearman and Kendall link methods wastes space.
2040  
2041 =cut
2042  
2043 ##---------------------------------------------------------------------
2044 =pod
2045  
2046 =head1 AUTHOR
2047  
2048 Bryan Jurish E<lt>moocow@cpan.orgE<gt> wrote and maintains the PDL::Cluster distribution.
2049  
2050 Michiel de Hoon wrote the underlying C clustering library for cDNA microarray data.
2051  
2052 =head1 COPYRIGHT
2053  
2054 PDL::Cluster is a set of wrappers around the C Clustering library for cDNA microarray data.
2055  
2056 =over 4
2057  
2058 =item *
2059  
2060 The C clustering library for cDNA microarray data.
2061 Copyright (C) 2002-2005 Michiel Jan Laurens de Hoon.
2062  
2063 This library was written at the Laboratory of DNA Information Analysis,
2064 Human Genome Center, Institute of Medical Science, University of Tokyo,
2065 4-6-1 Shirokanedai, Minato-ku, Tokyo 108-8639, Japan.
2066 Contact: michiel.dehoon 'AT' riken.jp
2067  
2068 See the files F<REAMDE.cluster>, F<cluster.c> and F<cluster.h> in the PDL::Cluster distribution
2069 for details.
2070  
2071 =item *
2072  
2073 PDL::Cluster wrappers copyright (C) Bryan Jurish 2005-2018. All rights reserved.
2074 This package is free software, and entirely without warranty.
2075 You may redistribute it and/or modify it under the same terms
2076 as Perl itself.
2077  
2078 =back
2079  
2080 =head1 SEE ALSO
2081  
2082 perl(1), PDL(3perl), Algorithm::Cluster(3perl), cluster(1),
2083 L<http://bonsai.hgc.jp/~mdehoon/software/cluster/software.htm|http://bonsai.hgc.jp/~mdehoon/software/cluster/software.htm>
2084  
2085 =cut
2086 #line 2087 "Cluster.pm"
2087  
2088 # Exit with OK status
2089  
2090 9 1;
« ccluster.c ↑ Summary