[英]Steps to perform document clustering using k-means algorithm in java
我需要在java中使用k-means算法執行文檔聚類的步驟。 對我來說,輕松提供這些步驟非常有用。 提前致謝。
您需要計算每個文檔中的單詞並創建一個通常稱為單詞包的功能。 在此之前,您需要刪除停用詞(非常常見但不提供諸如a等信息)。 您通常可以從文檔中獲取前n個常用詞。 計算這些單詞的頻率並將它們存儲在n維向量中。
對於距離測量,您可以使用余弦矢量。
這是一個簡單的算法,用於1維數據點的2均值。 您可以輕松地將其擴展到k均值和n維數據點。 如果你想要暗淡實現,請告訴我。
double[] x = {1,2,2.5,3,3.5,4,4.5,5,7,8,8.5,9,9.5,10};
double[] center = new int[2]; double[] precenter = new int[2]; ArrayList[] cluster = new ArrayList[2];
//generate 2 random number from 0 to x.length without replacement int rand = new int[2]; Random rand = new Random(); rand[0] = rand.nextInt(x.length + 1); rand[1] = rand.nextInt(x.length + 1);
while(rand[0] == rand[1] ){ rand[1] = rand.nextInt(x.length + 1); } center[0] = x[rand[0]]; center[1] = x[rand[1]]; //there is a better way to generate k random number (w/o replacement) just search.
do{ cluster[0].clear(); cluster[1].clear(); for(int i = 0; i < x.length; ++i){ if(abs(x[i]-center1[0]) <= abs(x[i]-center1[1])){ cluster[0].add(x[i]); } else{ cluster[0].add(x[i]); } precenter[0] = center[0]; precenter[1] = center[1];
center[0] = mean(cluster[0]); center[1] = mean(cluster[1]); } } while(precenter[0] != center[0] && precenter[1] != center[1]);
double mean(ArrayList list){ double mean = 0; double sum = 0; for(int index=0;index }
簇[0]和簇[1]包含簇中的點和中心[0],中心[1]是2個均值。
你需要做一些調試,因為我已經在R中編寫了代碼並將它轉換為java給你:)
您想基於什么來集群文檔? 如果它是相似的,你首先需要做一些自然語言處理,然后你需要一個度量(某種分配算法)來將文檔放入集群(crp工作並且相對簡單)。
最難的部分是NLP(語言處理),如果你沒有根據“長度”這樣的東西聚類它們。 我可以提供所有這些的更多信息,但如果你不需要,我不會潛入兔子洞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.