Click here to Skip to main content
15,890,973 members
Home / Discussions / Algorithms
   

Algorithms

 
AnswerRe: Finding Matches in Two Large Lists of Strings Pin
Moreno Airoldi29-May-09 8:41
Moreno Airoldi29-May-09 8:41 
AnswerRe: Finding Matches in Two Large Lists of Strings Pin
PIEBALDconsult12-Jul-09 17:58
mvePIEBALDconsult12-Jul-09 17:58 
AnswerRe: Finding Matches in Two Large Lists of Strings Pin
Tadeusz Westawic13-Jul-09 18:08
Tadeusz Westawic13-Jul-09 18:08 
Question[Message Deleted] Pin
Padmanabha_M26-May-09 17:54
Padmanabha_M26-May-09 17:54 
AnswerRe: arctan Pin
Tim Craig26-May-09 18:28
Tim Craig26-May-09 18:28 
Questionimage segmentation Pin
sridharmcapsg25-May-09 20:16
sridharmcapsg25-May-09 20:16 
AnswerRe: image segmentation Pin
riced25-May-09 23:51
riced25-May-09 23:51 
QuestionK means with Mahalanobis - Singularity [modified] Pin
FatMooseHenry21-May-09 22:41
FatMooseHenry21-May-09 22:41 
Hello
Im doing K-means clustering and am about to implement the Mahalanobis distance. I have a problem with sometimes the matrix is singular. Im not really sure what it means in this case and what to do about it? Im fairly sure that my code is ok, but here is the code for calculating the covariance matrix:

public static Matrix CovarianceMatrix(List<double[]> dataset)
        {
            /*
                cov_xx cov_xy ...
                cov_yx cov_yy ...
                ...
             */

            //Calculate mean for this cluster
            // cov_xx = sum[x*x]/n, cov_xy = sum[x*y]/n
            double[] means = new double[dataset[0].Length];
            Matrix cov = new Matrix(dataset[0].Length, dataset[0].Length);
            double sum = 0;

            for (int i = 0; i < dataset[0].Length; i++)
            {
                for (int j = 0; j < dataset.Count; j++)
                {
                    means[i] += dataset[j][i];
                }
                means[i] /= dataset.Count;
            }

            double[,] subresults = new double[dataset[0].Length, dataset.Count];
            for (int j = 0; j < dataset.Count; j++)
            {
                for (int i = 0; i < dataset[0].Length; i++)
                {
                    subresults[i, j] = dataset[j][i] - means[i];
                }
            }
            
            //fill covariance
            for (int i = 0; i < dataset[0].Length; i++)
            {
                for (int j = i; j < dataset[0].Length; j++)
                {
                    double s = 0;
                    for (int x = 0; x < dataset.Count; x++)
                    {
                        s += subresults[i, x] * subresults[j, x];
                    }
                    cov.SetElement(i, j, s / dataset.Count);
                    if (i != j) cov.SetElement(j, i, s / dataset.Count);
                }
            }
            return cov;
        }


And here for the distance:
public static double Mahalanobis(double[] vector1, double[] vector2, Matrix covariance)
{
    Matrix v1 = new Matrix(vector1, vector1.Length);
    Matrix v2 = new Matrix(vector2, vector2.Length);
    Matrix m = v1.Subtract(v2);

    return (double)(m.Transpose()).Multiply(covariance.Inverse()).Multiply(m).GetElement(0, 0);
}

If more information (or comments), or a working code sample is perfered, let me know. However, some times it can cluster without problem, so I think it is more about how to handle the singularity than the code it self.

Looking forward to hear from you

modified on Friday, May 22, 2009 5:32 AM

AnswerRe: K means with Mahalanobis - Singularity Pin
73Zeppelin22-May-09 10:06
73Zeppelin22-May-09 10:06 
QuestionRegion based shape representation Pin
raouaa21-May-09 1:15
raouaa21-May-09 1:15 
QuestionString matching algorithm Pin
vSoares18-May-09 6:43
professionalvSoares18-May-09 6:43 
AnswerRe: String matching algorithm Pin
Yusuf18-May-09 6:53
Yusuf18-May-09 6:53 
GeneralRe: String matching algorithm Pin
vSoares18-May-09 7:01
professionalvSoares18-May-09 7:01 
GeneralRe: String matching algorithm Pin
harold aptroot18-May-09 7:43
harold aptroot18-May-09 7:43 
GeneralRe: String matching algorithm Pin
riced18-May-09 22:21
riced18-May-09 22:21 
AnswerRe: String matching algorithm Pin
PIEBALDconsult19-May-09 4:24
mvePIEBALDconsult19-May-09 4:24 
AnswerRe: String matching algorithm Pin
Fatbuddha 119-May-09 5:01
Fatbuddha 119-May-09 5:01 
QuestionView Port to Window mapping Pin
suvigna13-May-09 7:39
suvigna13-May-09 7:39 
AnswerRe: View Port to Window mapping Pin
Moreno Airoldi20-May-09 1:17
Moreno Airoldi20-May-09 1:17 
AnswerRe: View Port to Window mapping Pin
Luc Pattyn20-May-09 3:04
sitebuilderLuc Pattyn20-May-09 3:04 
QuestionZ Score Pin
RugbyLeague13-May-09 5:13
RugbyLeague13-May-09 5:13 
AnswerRe: Z Score Pin
Moreno Airoldi20-May-09 1:21
Moreno Airoldi20-May-09 1:21 
QuestionSpell-checker for names Pin
dybs10-May-09 6:25
dybs10-May-09 6:25 
AnswerRe: Spell-checker for names Pin
Luc Pattyn10-May-09 6:37
sitebuilderLuc Pattyn10-May-09 6:37 
GeneralRe: Spell-checker for names Pin
dybs10-May-09 10:38
dybs10-May-09 10:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.