Mahout 命令

本文介绍使用Apache Mahout进行文本分类的全流程,包括从原始文本到序列文件的转换、生成TF-IDF向量、划分训练与测试集、训练及测试Naive Bayes分类器。

转载于:http://bit1129.iteye.com/blog/2213708

1. mahout seqdirectory

 生成序列文件

Java代码  
  1. $ mahout seqdirectory   
  2.     --input (-i) input               Path to job input directory(原始文本文件).  
  3.     --output (-o) output             The directory pathname for output.(<Text,Text>Sequence File)  
  4.     -ow  

 

 

功能: 将原始文本数据集转换为< Text, Text > SequenceFile

 

 

 

2. mahout seq2sparke

 将序列文件转化为向量

功能: Convert and preprocesses the dataset(<Text,Text> SequenceFile) into a < Text, VectorWritable > SequenceFile containing term frequencies for each document.

即根据Sequence File转换为tfidf向量文件

说明:If we wanted to use different parsing methods or transformations on the term frequency vectors we could supply different options here e.g.: -ng 2 for bigrams or -n 2 for L2 length normalization

 

 

Java代码  收藏代码
  1. mahout seq2sparse                           
  2.   --output (-o) output             The directory pathname for output.          
  3.   --input (-i) input               Path to job input directory.                
  4.   --weight (-wt) weight            The kind of weight to use. Currently TF     
  5.                                        or TFIDF. Default: TFIDF                    
  6.   --norm (-n) norm                 The norm to use, expressed as either a      
  7.                                        float or "INF" if you want to use the       
  8.                                        Infinite norm.  Must be greater or equal    
  9.                                        to 0.  The default is not to normalize      
  10.   --overwrite (-ow)                If set, overwrite the output directory      
  11.   --sequentialAccessVector (-seq)  (Optional) Whether output vectors should    
  12.                                        be SequentialAccessVectors. If set true     
  13.                                        else false                                  
  14.   --namedVector (-nv)              (Optional) Whether output vectors should    
  15.                                        be NamedVectors. If set true else false  

 

-i Sequence File文件目录

-o 向量文件输出目录

-wt 权重类型,支持TF或者TFIDF两种选项,默认TFIDF

-n 使用的正规化,使用浮点数或者"INF"表示,

-ow 指定该参数,将覆盖已有的输出目录

-seq 指定该参数,那么输出的向量是SequentialAccessVectors

-nv 指定该参数,那么输出的向量是NamedVectors

 

3. mahout split

 将预处理的tfidf向量集转换为training和testing向量集

功能:Split the preprocessed dataset into training and testing sets.


 

Java代码  收藏代码
  1. $ mahout split   
  2.     -i ${WORK_DIR}/20news-vectors/tfidf-vectors   
  3.     --trainingOutput ${WORK_DIR}/20news-train-vectors   
  4.     --testOutput ${WORK_DIR}/20news-test-vectors    
  5.     --randomSelectionPct 40   
  6.     --overwrite --sequenceFiles -xm sequential  

 

说明:如上是将向量数据集分为训练数据和检测数据,以随机40-60拆分

 

3. mahout trainnb

 

功能:训练分类器

 

Java代码  收藏代码
  1. mahout trainnb  
  2.   --input (-i) input               Path to job input directory.                   
  3.   --output (-o) output             The directory pathname for output.                      
  4.   --alphaI (-a) alphaI             Smoothing parameter. Default is 1.0  
  5.   --trainComplementary (-c)        Train complementary? Default is false.                          
  6.   --labelIndex (-li) labelIndex    The path to store the label index in           
  7.   --overwrite (-ow)                If present, overwrite the output directory     
  8.                                        before running job                             
  9.   --help (-h)                      Print out help                                 
  10.   --tempDir tempDir                Intermediate output directory                  
  11.   --startPhase startPhase          First phase to run                             
  12.   --endPhase endPhase              Last phase to run  

 

-i 输入路径

-o 输出路径

-a

-c 补偿性训练

-li label index文件的目录

-ow 指定该参数,删除输出目录

tempDir MapReduce作业的中间结果

startPhase 运行的第一个阶段

endPhase 运行的最后一个阶段

 

4. mahout testnb

 

功能:检验Bayes分类器

Java代码  收藏代码
  1. mahout testnb     
  2.   --input (-i) input               Path to job input directory.                    
  3.   --output (-o) output             The directory pathname for output.              
  4.   --overwrite (-ow)                If present, overwrite the output directory      
  5.                                        before running job  
  6.   
  7.   --model (-m) model               The path to the model built during training     
  8.   --testComplementary (-c)         Test complementary? Default is false.                            
  9.   --runSequential (-seq)           Run sequential?                                 
  10.   --labelIndex (-l) labelIndex     The path to the location of the label index     
  11.   --help (-h)                      Print out help                                  
  12.   --tempDir tempDir                Intermediate output directory                   
  13.   --startPhase startPhase          First phase to run                              
  14.   --endPhase endPhase              Last phase to run  

-i 输入路径

-o 输出路径

-ow 覆盖输出目录

-c


测试分类器模型时,如果觉得模型效果不够满意,可以对过程进行调整,然后重新生成模型。
可调整的方面包括:
  1. 积累更多,更有具代表性的样本;
  2. 在文本预处理阶段选择更好的分词算法;
  3. 在训练分类器时,对训练参数进行调整。
不断重复以上过程,直到得到满意的模型为止。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值