K-均值算法

时间:2009-06-28 21:22:24

标签: java algorithm machine-learning grouping unsupervised-learning

我正在尝试用Java编写k-means算法。我已经计算了许多数组,每个数组都包含许多系数。我需要使用k-means算法来对所有这些数据进行分组。你知道这个算法的任何实现吗?

由于

8 个答案:

答案 0 :(得分:5)

我自己没有研究过代码,但this JavaWorld article中提供了一个多线程的K-means实现,看起来很有启发性。

答案 1 :(得分:4)

分类,聚类和分组是IR的发展良好的领域。有一个非常好的(Java)库/软件(开源)here称为WEKA。有几种算法可用于聚类。虽然有一个学习曲线,但是当你遇到更难的问题时它可能会有用。

答案 2 :(得分:3)

"Programming Collective Intelligence"中有一个非常好的K-means聚类的Python实现。我强烈推荐它。

我意识到你必须翻译成Java,但它看起来并不太难。

答案 3 :(得分:3)

OpenCV是我曾经不得不使用的最可怕的库之一。 另一方面,Matlab非常巧妙地做到了。

如果你必须自己编写代码,那么算法的效率非常简单。

  1. 选择群集数量(k)
  2. 制作k点(它们将成为质心)
  3. 随机化所有这些点位置
  4. 计算从每个点到所有质心的欧几里德距离
  5. 将每个点的“成员资格”分配到最近的质心
  6. 通过平均属于给定群集的所有点的位置来建立新的质心
  7. 转到4直到达到收敛,或者所做的更改无关紧要。

答案 4 :(得分:3)

真的,KMeans是一个非常简单的算法。为什么不自己动手编码呢?我在Qt中完成了它,然后将代码移植到普通的旧STL中,没有太多问题。

我开始成为Joel想法的粉丝:没有外部依赖,所以请随时告诉我你无法控制的大型软件有什么好处,而这个问题上的其他人已经提到它不是好的软件/

谈话很便宜,真人向世界展示他们的代码: http://github.com/elcuco/data_mining_demo

我应该稍微清理代码以使其更通用,并且当前版本不会移植到STL,但它是一个开始!

答案 5 :(得分:2)

非常古老的问题,但我注意到没有提及Java Machine Learning LibraryK-Means的实施,并且包含some documentation关于它的用法。

该项目不是非常活跃,但最后一个版本是相对较新的(2012年7月)

答案 6 :(得分:1)

似乎每个发布的人都忘了提到事实上的图像处理库:OpenCV http://sourceforge.net/projects/opencvlibrary/。您必须围绕C OpenCV代码编写JNI包装器以使KMeans工作,但额外的好处是

  1. 您会知道KMeans算法已经过大量优化  
  2. OpenCV广泛使用您的GPU,因此它可以快速运行

主要的缺点是你必须编写一个JNI包装器。我曾经需要一个模板匹配例程,面对许多替代方案,但我发现OpenCV是迄今为止最好的,尽管我被迫为它编写了一个JNI包装器。

答案 7 :(得分:0)

//Aim:To implement Kmeans clustering algorithm.
//Program
import java.util.*;
class k_means
{
static int count1,count2,count3;
static int d[];
static int k[][];
static int tempk[][];
static double m[];
static double diff[];
static int n,p;

static int cal_diff(int a) // This method will determine the cluster in which an element go at a particular step.
{
int temp1=0;
for(int i=0;i<p;++i)
{
if(a>m[i])
diff[i]=a-m[i];
else
diff[i]=m[i]-a;
}
int val=0;
double temp=diff[0];
for(int i=0;i<p;++i)
{
if(diff[i]<temp)
{
temp=diff[i];
val=i;
}
}//end of for loop
return val;
}

static void cal_mean() // This method will determine intermediate mean values
{
for(int i=0;i<p;++i)
m[i]=0; // initializing means to 0
int cnt=0;
for(int i=0;i<p;++i)
{
cnt=0;
for(int j=0;j<n-1;++j)
{
if(k[i][j]!=-1)
{
m[i]+=k[i][j];
++cnt;
}}
m[i]=m[i]/cnt;
}
}

static int check1() // This checks if previous k ie. tempk and current k are same.Used as terminating case.
{
for(int i=0;i<p;++i)
for(int j=0;j<n;++j)
if(tempk[i][j]!=k[i][j])
{
return 0;
}
return 1;
}

public static void main(String args[])
{
Scanner scr=new Scanner(System.in);
/* Accepting number of elements */
System.out.println("Enter the number of elements ");
n=scr.nextInt();
d=new int[n];
/* Accepting elements */
System.out.println("Enter "+n+" elements: ");
for(int i=0;i<n;++i)
d[i]=scr.nextInt();
/* Accepting num of clusters */
System.out.println("Enter the number of clusters: ");
p=scr.nextInt();
/* Initialising arrays */
k=new int[p][n];
tempk=new int[p][n];
m=new double[p];
diff=new double[p];
/* Initializing m */
for(int i=0;i<p;++i)
m[i]=d[i];

int temp=0;
int flag=0;
do
{
for(int i=0;i<p;++i)
for(int j=0;j<n;++j)
{
k[i][j]=-1;
}
for(int i=0;i<n;++i) // for loop will cal cal_diff(int) for every element.
{
temp=cal_diff(d[i]);
if(temp==0)
k[temp][count1++]=d[i];
else
if(temp==1)
k[temp][count2++]=d[i];
else
if(temp==2)
k[temp][count3++]=d[i]; 
}
cal_mean(); // call to method which will calculate mean at this step.
flag=check1(); // check if terminating condition is satisfied.
if(flag!=1)
/*Take backup of k in tempk so that you can check for equivalence in next step*/
for(int i=0;i<p;++i)
for(int j=0;j<n;++j)
tempk[i][j]=k[i][j];

System.out.println("\n\nAt this step");
System.out.println("\nValue of clusters");
for(int i=0;i<p;++i)
{
System.out.print("K"+(i+1)+"{ ");
for(int j=0;k[i][j]!=-1 && j<n-1;++j)
System.out.print(k[i][j]+" ");
System.out.println("}");
}//end of for loop
System.out.println("\nValue of m ");
for(int i=0;i<p;++i)
System.out.print("m"+(i+1)+"="+m[i]+"  ");

count1=0;count2=0;count3=0;
}
while(flag==0);

System.out.println("\n\n\nThe Final Clusters By Kmeans are as follows: ");
for(int i=0;i<p;++i)
{
System.out.print("K"+(i+1)+"{ ");
for(int j=0;k[i][j]!=-1 && j<n-1;++j)
System.out.print(k[i][j]+" ");
System.out.println("}");
}
}
}
/*
Enter the number of elements
8
Enter 8 elements:
2 3 6 8 12 15 18 22
Enter the number of clusters:
3

At this step
Value of clusters
K1{ 2 }
K2{ 3 }
K3{ 6 8 12 15 18 22 }
Value of m
m1=2.0  m2=3.0  m3=13.5

At this step
Value of clusters
K1{ 2 }
K2{ 3 6 8 }
K3{ 12 15 18 22 }
Value of m
m1=2.0  m2=5.666666666666667  m3=16.75

At this step
Value of clusters
K1{ 2 3 }
K2{ 6 8 }
K3{ 12 15 18 22 }
Value of m
m1=2.5  m2=7.0  m3=16.75

At this step
Value of clusters
K1{ 2 3 }
K2{ 6 8 }
K3{ 12 15 18 22 }
Value of m
m1=2.5  m2=7.0  m3=16.75

The Final Clusters By Kmeans are as follows:
K1{ 2 3 }
K2{ 6 8 }
K3{ 12 15 18 22 } */