Hello everybody, I want to create a simple R script that calculates the center of a cluster, I X={X1,X2,...,X2} is my data matrix and U={U_ik} is the partnership of element Xi in cluster K, I want to do the following in a fastest way:
so here each Xi is multiplied by a weight and summed up and then divided by the sum of weights , so it is weighted mean, but here our X is a vector. There in R the function weighted.mean(), but it needs only numerical values. I have written a script well it works for one but for a big number of X it is slow the code is
U : membership matrix.
X : Our data.
m : parameter
K : cluster number K
function(U,X,m,k)
{
Nominator <- matrix(0,length(X[,1]),1)
Denominator <-0
for(i in 1:length(U[1,])) #We go trought the elements of the cluster k
{
if(U[k,i]!=0)
{
Nominator <- Nominator+ (U[k,i]^m)* X[,i]
Denominator <- Denominator + (U[k,i]^m)
}
}
Nominator/Denominator
}
btw, if all your U matrix elements were only 0 or 1, why do you take to the power of m then?
You are doing it wrong: just figured that your code doesn't do what your formula says. you have to take X^m not U^m !
Here X values may be 0 or 1 but U values are real values. In fact they represent the membership degree of element Xj in cluster Ci