mahout自帶的例子 -------------------------分類
阿新 • • 發佈:2019-01-30
介紹
這二十個新聞組資料集合是收集大約20,000新聞組文件,均勻的分佈在20個不同的集合。這20個新聞組集合採集最近流行的資料集合到文字程式中作為實驗,根據機器學習技術。例如文字分類,文字聚集。我們將使用Mahout的Bayes Classifier創造一個模型,它將一個新文件分類到這20個新聞組集合
首要條件
。Mahout已經下載
。Maven是可用的
。已經設定瞭如下環境變數:
HADOOP_HOME hadoop的安裝路徑
MAHOUT_HOME mahout的安裝路徑
安裝Mahout
如果已經下載了distribution的Mahout,使用unzip/untar解壓,進入解壓後的目錄,
1.進入trunk目錄,編譯並且建立hadoop job
Java程式碼
基於Mahout 0.2+:
1.建立目錄並且下載20newsgroup的資料
Java程式碼
2.下載20news-bydate.tar.gz從[url=20newsgroups dataset]20newsgroups dataset[/url]
3.解壓
Java程式碼
4.生成input的資料
Java程式碼
5.生成test的資料
Java程式碼
在hadoop叢集上執行20newsgroups例子
設定hadoop叢集
1.編輯hadoop-site.xml,新增本地設定Hadoop quickstart
Java程式碼
2.格式HDFS
Java程式碼
3.啟動hadoop
Java程式碼
4.上傳檔案到HDFS
Java程式碼
Train貝葉斯分類基於Tri-grams
下面將在hadoop執行4個map reduce工作,為了Train這個分類器並且將執行一段時間如果在只有一個節點的機器上
Java程式碼
你可以監控這個job的狀態,從Job Tracker的機器上開啟瀏覽器訪問:http://localhost:50030/jobtracker.jsp
在input目錄執行Test分類器
Java程式碼
輸出的結果:
08/11/07 16:52:39 INFO bayes.TestClassifier: Done loading model: # labels: 20
08/11/07 16:52:39 INFO bayes.TestClassifier: Done generating Model
08/11/07 16:52:57 INFO bayes.TestClassifier: alt.atheism96.9962453066333775/799.0
08/11/07 16:53:15 INFO bayes.TestClassifier: comp.graphics99.28057553956835966/973.0
08/11/07 16:53:45 INFO bayes.TestClassifier: comp.os.ms-windows.misc96.95431472081218955/985.0
08/11/07 16:53:59 INFO bayes.TestClassifier: comp.sys.ibm.pc.hardware99.59266802443992978/982.0
08/11/07 16:54:10 INFO bayes.TestClassifier: comp.sys.mac.hardware99.47970863683663956/961.0
08/11/07 16:54:28 INFO bayes.TestClassifier: comp.windows.x99.59183673469387976/980.0
08/11/07 16:54:38 INFO bayes.TestClassifier: misc.forsale98.45679012345678957/972.0
08/11/07 16:54:50 INFO bayes.TestClassifier: rec.autos99.4949494949495985/990.0
08/11/07 16:55:04 INFO bayes.TestClassifier: rec.motorcycles100.0994/994.0
08/11/07 16:55:16 INFO bayes.TestClassifier: rec.sport.baseball99.89939637826961993/994.0
08/11/07 16:55:36 INFO bayes.TestClassifier: rec.sport.hockey99.89989989989989998/999.0
08/11/07 16:55:54 INFO bayes.TestClassifier: sci.crypt99.39455095862765985/991.0
08/11/07 16:56:05 INFO bayes.TestClassifier: sci.electronics98.98063200815494971/981.0
08/11/07 16:56:27 INFO bayes.TestClassifier: sci.med99.79797979797979988/990.0
08/11/07 16:56:44 INFO bayes.TestClassifier: sci.space99.3920972644377981/987.0
08/11/07 16:57:06 INFO bayes.TestClassifier: soc.religion.christian99.49849548645938992/997.0
08/11/07 16:57:24 INFO bayes.TestClassifier: talk.politics.guns99.45054945054945905/910.0
08/11/07 16:57:51 INFO bayes.TestClassifier: talk.politics.mideast98.82978723404256929/940.0
08/11/07 16:58:13 INFO bayes.TestClassifier: talk.politics.misc89.93548387096774697/775.0
08/11/07 16:58:25 INFO bayes.TestClassifier: talk.religion.misc61.78343949044586388/628.0
08/11/07 16:58:25 INFO bayes.TestClassifier: =======================================================
Summary
-------------------------------------------------------
Correctly Classified Instances : 18369 97.5621%
Incorrectly Classified Instances : 459 2.4379%
Total Classified Instances : 18828
=======================================================
Confusion Matrix
-------------------------------------------------------
a b c d e f g h i j k l m n o p q r s t <--Classified as
994 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | 994 a = rec.motorcycles
0 976 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 2 1 | 980 b = comp.windows.x
7 0 929 1 0 0 0 0 0 0 0 0 1 0 2 0 0 0 0 0 | 940 c = talk.politics.mideast
0 0 0 905 0 0 1 0 0 0 0 0 0 0 0 0 3 0 1 0 | 910 d = talk.politics.guns
4 1 4 27 388 1 0 1 0 5 1 1 2 2 149 7 2 33 0 0 | 628 e = talk.religion.misc
3 0 0 0 0 985 0 1 0 0 0 0 0 1 0 0 0 0 0 0 | 990 f = rec.autos
0 0 0 0 0 0 993 1 0 0 0 0 0 0 0 0 0 0 0 0 | 994 g = rec.sport.baseball
0 0 0 0 0 0 1 998 0 0 0 0 0 0 0 0 0 0 0 0 | 999 h = rec.sport.hockey
0 0 0 0 0 0 0 0 956 0 2 0 0 0 0 0 0 0 2 1 | 961 i = comp.sys.mac.hardware
0 0 0 0 0 0 0 0 0 981 0 0 5 0 0 1 0 0 0 0 | 987 j = sci.space
0 0 0 0 0 0 0 0 0 0 978 0 1 0 0 0 0 0 2 1 | 982 k = comp.sys.ibm.pc.hardware
1 0 3 36 0 1 2 1 0 5 0 697 4 0 3 3 19 0 0 0 | 775 l = talk.politics.misc
0 2 0 0 0 0 0 0 0 0 2 0 966 0 0 0 0 0 2 1 | 973 m = comp.graphics
1 0 0 0 0 0 0 0 0 0 6 0 0 971 0 0 0 0 3 0 | 981 n = sci.electronics
1 0 0 0 0 0 0 0 1 0 0 0 0 0 992 1 0 1 0 1 | 997 o = soc.religion.christian
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 988 0 0 0 1 | 990 p = sci.med
0 0 0 2 0 0 0 0 0 0 0 0 2 1 0 0 985 0 1 0 | 991 q = sci.crypt
0 0 0 1 1 0 0 0 0 1 0 0 1 0 19 0 1 775 0 0 | 799 r = alt.atheism
1 0 0 0 0 3 1 2 0 0 3 0 0 5 0 0 0 0 957 0 | 972 s = misc.forsale
0 0 0 8 0 0 0 0 0 0 6 0 6 0 0 0 0 0 10 955 | 985 t = comp.os.ms-windows.misc
附加的Naive Bayes
Train一個CBayes分類器使用bi-grams
Java程式碼
Test一個CBayes分類器使用bi-grams
Java程式碼
這二十個新聞組資料集合是收集大約20,000新聞組文件,均勻的分佈在20個不同的集合。這20個新聞組集合採集最近流行的資料集合到文字程式中作為實驗,根據機器學習技術。例如文字分類,文字聚集。我們將使用Mahout的Bayes Classifier創造一個模型,它將一個新文件分類到這20個新聞組集合
首要條件
。Mahout已經下載
。Maven是可用的
。已經設定瞭如下環境變數:
HADOOP_HOME hadoop的安裝路徑
MAHOUT_HOME mahout的安裝路徑
安裝Mahout
如果已經下載了distribution的Mahout,使用unzip/untar解壓,進入解壓後的目錄,
1.進入trunk目錄,編譯並且建立hadoop job
Java程式碼
- mvn install
基於Mahout 0.2+:
1.建立目錄並且下載20newsgroup的資料
Java程式碼
- $ mkdir $MAHOUT_HOME/examples/bin/work/
- $ cd $MAHOUT_HOME/examples/bin/work/
2.下載20news-bydate.tar.gz從[url=20newsgroups dataset]20newsgroups dataset[/url]
3.解壓
Java程式碼
- tar zxf 20news-bydate.tar.gz
4.生成input的資料
Java程式碼
- $> $MAHOUT_HOME/bin/mahout org.apache.mahout.classifier.bayes.PrepareTwentyNewsgroups \
- -p examples/bin/work/20news-bydate/20news-bydate-train \
- -o examples/bin/work/20news-bydate/bayes-train-input \
- -a org.apache.mahout.vectorizer.DefaultAnalyzer \
- -c UTF-8
5.生成test的資料
Java程式碼
- $> $MAHOUT_HOME/bin/mahout org.apache.mahout.classifier.bayes.PrepareTwentyNewsgroups \
- -p examples/bin/work/20news-bydate/20news-bydate-test \
- -o examples/bin/work/20news-bydate/bayes-test-input \
- -a org.apache.mahout.vectorizer.DefaultAnalyzer \
- -c UTF-8
在hadoop叢集上執行20newsgroups例子
設定hadoop叢集
1.編輯hadoop-site.xml,新增本地設定Hadoop quickstart
Java程式碼
- emacs $HADOOP_HOME/conf/hadoop-site.xml
2.格式HDFS
Java程式碼
- $ $HADOOP_HOME/bin/hadoop namenode -format
3.啟動hadoop
Java程式碼
- $ $HADOOP_HOME/bin/start-all.sh
4.上傳檔案到HDFS
Java程式碼
- $ $HADOOP_HOME/bin/hadoop dfs -put $MAHOUT_HOME/examples/bin/work/20news-bydate/bayes-train-input 20news-input
Train貝葉斯分類基於Tri-grams
下面將在hadoop執行4個map reduce工作,為了Train這個分類器並且將執行一段時間如果在只有一個節點的機器上
Java程式碼
- $> $MAHOUT_HOME/bin/mahout trainclassifier \
- -i 20news-input/bayes-train-input \
- -o newsmodel \
- -type bayes \
- -ng 3 \
- -source hdfs
你可以監控這個job的狀態,從Job Tracker的機器上開啟瀏覽器訪問:http://localhost:50030/jobtracker.jsp
在input目錄執行Test分類器
Java程式碼
- $> $MAHOUT_HOME/bin/mahout testclassifier \
- -m newsmodel \
- -d 20news-input \
- -type bayes \
- -ng 3 \
- -source hdfs \
- -method mapreduce
輸出的結果:
08/11/07 16:52:39 INFO bayes.TestClassifier: Done loading model: # labels: 20
08/11/07 16:52:39 INFO bayes.TestClassifier: Done generating Model
08/11/07 16:52:57 INFO bayes.TestClassifier: alt.atheism96.9962453066333775/799.0
08/11/07 16:53:15 INFO bayes.TestClassifier: comp.graphics99.28057553956835966/973.0
08/11/07 16:53:45 INFO bayes.TestClassifier: comp.os.ms-windows.misc96.95431472081218955/985.0
08/11/07 16:53:59 INFO bayes.TestClassifier: comp.sys.ibm.pc.hardware99.59266802443992978/982.0
08/11/07 16:54:10 INFO bayes.TestClassifier: comp.sys.mac.hardware99.47970863683663956/961.0
08/11/07 16:54:28 INFO bayes.TestClassifier: comp.windows.x99.59183673469387976/980.0
08/11/07 16:54:38 INFO bayes.TestClassifier: misc.forsale98.45679012345678957/972.0
08/11/07 16:54:50 INFO bayes.TestClassifier: rec.autos99.4949494949495985/990.0
08/11/07 16:55:04 INFO bayes.TestClassifier: rec.motorcycles100.0994/994.0
08/11/07 16:55:16 INFO bayes.TestClassifier: rec.sport.baseball99.89939637826961993/994.0
08/11/07 16:55:36 INFO bayes.TestClassifier: rec.sport.hockey99.89989989989989998/999.0
08/11/07 16:55:54 INFO bayes.TestClassifier: sci.crypt99.39455095862765985/991.0
08/11/07 16:56:05 INFO bayes.TestClassifier: sci.electronics98.98063200815494971/981.0
08/11/07 16:56:27 INFO bayes.TestClassifier: sci.med99.79797979797979988/990.0
08/11/07 16:56:44 INFO bayes.TestClassifier: sci.space99.3920972644377981/987.0
08/11/07 16:57:06 INFO bayes.TestClassifier: soc.religion.christian99.49849548645938992/997.0
08/11/07 16:57:24 INFO bayes.TestClassifier: talk.politics.guns99.45054945054945905/910.0
08/11/07 16:57:51 INFO bayes.TestClassifier: talk.politics.mideast98.82978723404256929/940.0
08/11/07 16:58:13 INFO bayes.TestClassifier: talk.politics.misc89.93548387096774697/775.0
08/11/07 16:58:25 INFO bayes.TestClassifier: talk.religion.misc61.78343949044586388/628.0
08/11/07 16:58:25 INFO bayes.TestClassifier: =======================================================
Summary
-------------------------------------------------------
Correctly Classified Instances : 18369 97.5621%
Incorrectly Classified Instances : 459 2.4379%
Total Classified Instances : 18828
=======================================================
Confusion Matrix
-------------------------------------------------------
a b c d e f g h i j k l m n o p q r s t <--Classified as
994 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | 994 a = rec.motorcycles
0 976 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 2 1 | 980 b = comp.windows.x
7 0 929 1 0 0 0 0 0 0 0 0 1 0 2 0 0 0 0 0 | 940 c = talk.politics.mideast
0 0 0 905 0 0 1 0 0 0 0 0 0 0 0 0 3 0 1 0 | 910 d = talk.politics.guns
4 1 4 27 388 1 0 1 0 5 1 1 2 2 149 7 2 33 0 0 | 628 e = talk.religion.misc
3 0 0 0 0 985 0 1 0 0 0 0 0 1 0 0 0 0 0 0 | 990 f = rec.autos
0 0 0 0 0 0 993 1 0 0 0 0 0 0 0 0 0 0 0 0 | 994 g = rec.sport.baseball
0 0 0 0 0 0 1 998 0 0 0 0 0 0 0 0 0 0 0 0 | 999 h = rec.sport.hockey
0 0 0 0 0 0 0 0 956 0 2 0 0 0 0 0 0 0 2 1 | 961 i = comp.sys.mac.hardware
0 0 0 0 0 0 0 0 0 981 0 0 5 0 0 1 0 0 0 0 | 987 j = sci.space
0 0 0 0 0 0 0 0 0 0 978 0 1 0 0 0 0 0 2 1 | 982 k = comp.sys.ibm.pc.hardware
1 0 3 36 0 1 2 1 0 5 0 697 4 0 3 3 19 0 0 0 | 775 l = talk.politics.misc
0 2 0 0 0 0 0 0 0 0 2 0 966 0 0 0 0 0 2 1 | 973 m = comp.graphics
1 0 0 0 0 0 0 0 0 0 6 0 0 971 0 0 0 0 3 0 | 981 n = sci.electronics
1 0 0 0 0 0 0 0 1 0 0 0 0 0 992 1 0 1 0 1 | 997 o = soc.religion.christian
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 988 0 0 0 1 | 990 p = sci.med
0 0 0 2 0 0 0 0 0 0 0 0 2 1 0 0 985 0 1 0 | 991 q = sci.crypt
0 0 0 1 1 0 0 0 0 1 0 0 1 0 19 0 1 775 0 0 | 799 r = alt.atheism
1 0 0 0 0 3 1 2 0 0 3 0 0 5 0 0 0 0 957 0 | 972 s = misc.forsale
0 0 0 8 0 0 0 0 0 0 6 0 6 0 0 0 0 0 10 955 | 985 t = comp.os.ms-windows.misc
附加的Naive Bayes
Train一個CBayes分類器使用bi-grams
Java程式碼
- $> $MAHOUT_HOME/bin/mahout trainclassifier \
- -i 20news-input \
- -o newsmodel \
- -type cbayes \
- -ng 2 \
- -source hdfs
Test一個CBayes分類器使用bi-grams
Java程式碼
- $> $MAHOUT_HOME/bin/mahout testclassifier \
- -m newsmodel \
- -d 20news-input \
- -type cbayes \
- -ng 2 \
- -source hdfs \
- -method mapreduce