mahout中的kmeans簡單例項

2021-05-27 02:20:10 字數 2197 閱讀 7537

在mahout_in_action這本書中,有個kmeans的簡單例項,可書中只給了源**,而並沒有指出要匯入哪些包才能正確執行

這本書在內容開始提到書中所有**都是基於mahout0.4版本的,可是我發現這個kmeans的例子,卻是基於mahout0.3的,有幾個函式0.4版中是沒有的

我不知道是不是因為我直接用的編譯好的包,但我下mahout0.4的原始碼看了,也沒有,下面我會在**中標註出哪幾個函式是0.4中沒有的

public static final double points = , , ,

, , , , , };

public static void writepointstofile(listpoints,

string filename, filesystem fs, configuration conf)

throws ioexception

writer.close();

}public static listgetpoints(double raw)

return points;

}public static void main(string args) throws exception

testdata = new file("testdata/points");

if (!testdata.exists())

configuration conf = new configuration();

filesystem fs = filesystem.get(conf);

writepointstofile(vectors, "testdata/points/file1", fs, conf);

path path = new path("testdata/clusters/part-00000");

sequencefile.writer writer = new sequencefile.writer(fs, conf,

path, text.class, cluster.class);

for (int i = 0; i < k; i++)

writer.close();

kmeansdriver.runjob("testdata/points", "testdata/clusters", 中改為kmeansdriver.run(引數),沒有runjob這個函式了

"output", euclideandistancemeasure.class.getname(), 0.001,

10, 1);

sequencefile.reader reader = new sequencefile.reader(fs,

new path("output/points/part-00000"), conf);

text key = new text();

text value = new text();

while (reader.next(key, value))

reader.close();

}

根據錯誤提示,找到hadoop-core-0.20.2.jar,mahout-core-0.3.jar,mahout-math-0.3.jar匯入,此時**應該不會報錯了,在執行過程中,根據提示,依次將下列包導到工程中。我在測試的過程中,先後匯入了mahout-collections-0.3.jar,slf4j-api-1.5.8.jar,slf4j-jcl-1.5.8.jar,commons-logging-1.1.1.jar,commons-cli-2.0.jar,commons-httpclient-3.1.jar,這些包都可以在mahout安裝目錄或安裝目錄下的lib目錄下找到,最後執行結果如下:

vector:0 belongs to cluster 0

vector:1 belongs to cluster 0

vector:2 belongs to cluster 0

vector:3 belongs to cluster 0

vector:4 belongs to cluster 0

vector:5 belongs to cluster 1

vector:6 belongs to cluster 1

vector:7 belongs to cluster 1

vector:8 belongs to cluster 1

Mahout 中 kmeans的引數

distancemeasure measure 資料點間的距離計算方法,引數可缺,預設是 squaredeuclidean 算方法 提供引數值 chebyshevdistancemeasure 切比雪夫距離 cosinedistancemeasure 余弦距離 euclideandistanceme...

mahout使用KMeans演算法

mahout提供了記憶體中和分布式的兩種kmeans聚類實現。下面是記憶體中kmeans的 示例,示例 使用了最簡單的一維向量作為輸入 tests kmeans cluster algorithm in memory,note the test uses only 1 d vector i.e.a ...

mahout之聚類演算法 KMeans分析

一,k means聚類演算法原理 k means 演算法接受引數 k 然後將事先輸入的n個資料物件劃分為 k個聚類以便使得所獲得的聚類滿足 同一聚類中的物件相似度較高 而不同聚類中的物件相似度較小。聚類相似度是利用各聚類中物件的均值所獲得乙個 中心物件 引力中心 來進行計算的。k means演算法是...