k-means clustering


How can I use AFNI to do the K-means clustering? Because I want to identify different connectivity patterns.

There is a program called 3dkmeans that may meet your needs.

Hi everyone,

I was wondering what the function 3dkmeans does whit the option -g 0 ?
It seems that it still create clusters, but I don’t understand how (which algorithm is used)?

Thank you in advance for any help!

Hello again,

Sorry I have some other questions about this function. As it is mention at the beginning of the help-page program, it seems that the algorithm used are based on the ‘Michiel Jan Laurens de Hoon C Clustering Library’.

1/ In this library, the pearson correlation scores are computed like this:
r =1/n * sum[ ( (xi−mean(x)) / sd(x) ) * ( (yi−mean(y)) / sd(y) ) ]
why not 1/(n-1) instead of 1/n ?

2/ does the ‘uncentered’ version corresponded to the Uncentered correlation (cosine of the angle) described by the ‘Michiel Jan Laurens de Hoon C Clustering Library’?

3/ in the -g 2 option, what do you mean by Weighted_Pearson_Correlation ?

4/ Finally, the help-page mentioned that the -g 7 option is the default when there is only one value per voxel. But could we still use this metric for time-series data (meaning that there is several values per voxel?)

Sorry again for all these questions and thank you for your reply.


This program was written long ago, so I can’t recall everything right now, but as you have found out it is based on Michiel de Hoon’s library, so for details please check clustering documentation (by Michiel de Hoon) that you can find here:


Quick answers to your questions would be:
2) yes
3) The weight vector contains weights for the elements in the data, by default 1, but it is good to have an option to tweak the data; see documentation page 8
4) yes it can be used with 3d+t data

I have checked cluster.c code and must say, that it is not computed just with 1/n… although it is true they stated in documentation that factor is 1/n.

Hope I cleared some doubts,

Many thanks for your reply.

Hi AFNI experts,

If I understand correctly, the author “hjr61547” seems asking if there is a program could classify or group functional connectivity maps, e.g. sorting connectivity snapshots into k groups by maximizing within-cluster similarity and between-cluster dissimilarity. I knew there is a k-means clusting algorithm in MATLAB could do this, while I don’t know if there is an AFNI program could do similar thing.
I noticed that there is one called 3dkmeans, while it seems classifying clusters in the spatial domain. If so, could you write a program for the above purpose please?
I would say it will be a very useful program for AFNI users. Here are two references using similar approach:

Allen et a., 2016. Cerebral Cortex. Tracking Whole-Brain Connectivity Dynamics in the Resting State
Gonzalez-Castillo et al., 2015. PNAS. Tracking ongoing cognition in individuals using brief, whole-brain functional connectivity patterns

Thank you,