溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何進(jìn)行mahout的安裝使用

發(fā)布時(shí)間:2021-12-27 14:36:58 來(lái)源:億速云 閱讀:191 作者:柒染 欄目:云計(jì)算

這篇文章給大家介紹如何進(jìn)行mahout的安裝使用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

  • 安裝mahout

在 http://mahout.apache.org/ 官網(wǎng)上下載mahout,小編下載的版本是 mahout-distribution-0.9.tar.gz  

解壓到mahout文件夾下

修改環(huán)境變量

在 /etc/profile 中添加 :

export CLASSPATH=$CLASSPATH:.:$JAVA_HOME/lib:$MAHOUT_HOME/lib:$JAVA_HOME/jre/lib
export MAHOUT_HOME=/home/Shin/hadoop-1.2.1/mahout/mahout-distribution-0.9
export MAHOUT_CONF_DIR=/home/Shin/hadoop-1.2.1/mahout/mahout-distribution-0.9/conf
export PATH=$PATH:/home/Shin/hadoop-1.2.1/mahout/mahout-distribution-0.9/bin
export HADOOP_CONF_DIR=/home/Shin/hadoop-1.2.1/conf

之后source一下 /etc/profile

小編是通過(guò)使用在java中調(diào)用linux命令的方式來(lái)調(diào)用mahout中的隨機(jī)森林

  • java調(diào)用linux命令

有如下API:

Process exec(String command) // 在單獨(dú)的進(jìn)程中執(zhí)行指定的字符串命令
Process exec(String command, String[] envp) // 在指定環(huán)境的單獨(dú)進(jìn)程中執(zhí)行指定的字符串命令
Process exec(String command, String[] envp, File dir) // 在有指定環(huán)境和工作目錄的獨(dú)立進(jìn)程中執(zhí)行指定的字符串命令
Process exec(String[] cmdarray) //在單獨(dú)的進(jìn)程中執(zhí)行指定命令和變量
Process exec(String[] cmdarray, String[] envp) // 在指定環(huán)境的獨(dú)立進(jìn)程中執(zhí)行指定命令和變量
Process exec(String[] cmdarray, String[] envp,File dir) //在指定環(huán)境和工作目錄的獨(dú)立進(jìn)程中執(zhí)行指定的命令和變量
  • mahout調(diào)用隨機(jī)森林的命令參數(shù)


  • 參照

  • http://mahout.apache.org/users/classification/partial-implementation.html



// 生成數(shù)據(jù)集描述文件
String[] commmandStrings1 = new String[] {
"/home/Shin/hadoop-1.2.1/bin/hadoop",
"jar",
"/home/Shin/hadoop-1.2.1/mahout/mahout-distribution-0.9/mahout-core-0.9-job.jar",
"org.apache.mahout.classifier.df.tools.Describe", "-p",
"citydata/city.txt", "-f", "citydata/city+.info", "-d",
"15", "N", "C", "L" };
參數(shù):
-p:訓(xùn)練數(shù)據(jù)路徑
-f:輸出描述文件路徑
-d:數(shù)據(jù)屬性描述,具體如下
N: NUMERICAL
C: CATEGORICAL
L: LABEL
I: IGNORED

數(shù)據(jù):(citytest.txt)
115 0.5 0.85 11 49 230 257 155 231 0 0 20 54 2 2 臭氧1小時(shí) 輕度污染
45 0.4 0.583 8 23 144 226 113 196 19 49 19 35 2 3 null 優(yōu)
31 0.394 0.478 8 16 68 96 46 80 31 28 8 11 3 3 null 優(yōu)
27 0.779 0.84 16 25 86 87 61 82 26 40 11 17 6 10 null 優(yōu)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 null null
95 1.441 1.203 11 37 92 177 102 102 135 79 71 35 39 40 細(xì)顆粒物(PM2.5) 良
51 0.559 0.563 7 12 74 74 68 68 52 64 15 17 22 22 顆粒物(PM10) 良
43 0.286 0.365 5 20 52 59 45 45 43 48 12 16 9 9 null 優(yōu)
68 1.178 1.748 14 58 105 129 74 90 85 92 26 38 13 10 顆粒物(PM10) 良
55 0.435 0.82 11 32 76 94 57 80 53 107 39 59 3 2 細(xì)顆粒物(PM2.5) 良

// 生成決策森林 
String[] commmandStrings2 = new String[] {
"/home/Shin/hadoop-1.2.1/bin/hadoop",
"jar",
"/home/Shin/hadoop-1.2.1/mahout/mahout-distribution-0.9/mahout-examples-0.9-job.jar",
"org.apache.mahout.classifier.df.mapreduce.BuildForest",
"-Dmapred.max.split.size=1874231", "-d",
"citydata/city.txt", "-ds", "citydata/city+.info", "-sl",
"5", "-p", "-t", "100", "-o", "nsl-forest" };
-d:訓(xùn)練數(shù)據(jù)文件路徑
-ds:數(shù)據(jù)描述文件路徑
-sl:每個(gè)數(shù)的每個(gè)節(jié)點(diǎn)隨機(jī)選多少個(gè)屬性
-p:代表使用部分執(zhí)行
-t:創(chuàng)建樹(shù)的數(shù)量
-o:輸出決策森林文件路徑

// 使用決策森林分類新數(shù)據(jù)
String[] commmandStrings3 = new String[] {
"/home/Shin/hadoop-1.2.1/bin/hadoop",
"jar",
"/home/Shin/hadoop-1.2.1/mahout/mahout-distribution-0.9/mahout-examples-0.9-job.jar",
"org.apache.mahout.classifier.df.mapreduce.TestForest",
"-i", "citydata/citytest.txt", "-ds",
"citydata/city+.info", "-m", "nsl-forest", "-a", "-mr",
"-o", "predictions" };
-i:測(cè)試文件路徑
-ds:數(shù)據(jù)描述文件路徑
-m:決策森林文件所在路徑
-mr:使用Hadoop分布計(jì)算
-o:輸出文件路徑
Process process1, process2, process3
// Process exec(String[] cmdarray)
process1 = Runtime.getRuntime().exec(commmandStrings1);
process1.waitFor();
process2 = Runtime.getRuntime().exec(commmandStrings2);
process2.waitFor();
process3 = Runtime.getRuntime().exec(commmandStrings3);
process3.waitFor();

由于是編程調(diào)用linux,其原本在控制臺(tái)的輸出沒(méi)有可以顯示的地方,要獲得控制臺(tái)的輸出,通常使用

process.getInputStream()

可是,使用這個(gè)API后,eclipse編輯器控制臺(tái)沒(méi)有得到任何輸出,嘗試使用其他linux命令,Eg:bin/hadoop dfs -rmr test.txt  則可以得到輸出。

偶然間使用

process.getErrorStream()

竟然能得到mahout的輸出

InputStreamReader ir = new InputStreamReader(process.getErrorStream());
LineNumberReader input = new LineNumberReader(ir);
while ((line = input.readLine()) != null) {
    相關(guān)操作
}

mahout預(yù)測(cè)結(jié)果輸出

predictions/citytest.txt.out

預(yù)測(cè)結(jié)果可以在終端下可以用查看

bin/hadoop dfs -cat predictions/*

預(yù)測(cè)結(jié)果是double類型的0.0,1.0,2.0    但是,在eclipse下查看是空的,將其load到本地查看是亂碼,放到windows下顯示正常,但是浮點(diǎn)數(shù)間有空格,后來(lái)在終端下用vim編輯可以看到數(shù)據(jù)真正的展示形式  ^@0 ^@ . ^@0

解決方案如下

FileReader in = new FileReader("citytest.txt.out");
BufferedReader bin = new BufferedReader(in);
FileWriter out = new FileWriter("cityout.txt");
String str;
while ((str = bin.readLine()) != null) {
	char a = str.charAt(1);
	char b = str.charAt(3);
	char c = str.charAt(5);
	String s = Character.toString(a) + Character.toString(b)+ Character.toString(c);
	out.write(s + "\n");
    }
in.close();
out.close();

關(guān)于如何進(jìn)行mahout的安裝使用就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI