`
wbj0110
  • 浏览: 1591186 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Twenty Newsgroups Classification任务之二seq2sparse(1)

阅读更多

seq2sparse对应于mahout中的org.apache.mahout.vectorizer.SparseVectorsFromSequenceFiles,从昨天跑的算法中的任务监控界面可以看到这一步包含了7个Job信息,分别是:(1)DocumentTokenizer(2)WordCount(3)MakePartialVectors(4)MergePartialVectors(5)VectorTfIdf Document Frequency Count(6)MakePartialVectors(7)MergePartialVectors。打印SparseVectorsFromSequenceFiles的参数帮助信息可以看到如下的信息:

 

[java] view plaincopy
 
  1. Usage:                                                                            
  2.  [--minSupport <minSupport> --analyzerName <analyzerName> --chunkSize             
  3. <chunkSize> --output <output> --input <input> --minDF <minDF> --maxDFSigma        
  4. <maxDFSigma> --maxDFPercent <maxDFPercent> --weight <weight> --norm <norm>        
  5. --minLLR <minLLR> --numReducers <numReducers> --maxNGramSize <ngramSize>          
  6. --overwrite --help --sequentialAccessVector --namedVector --logNormalize]         
  7. Options                                                                           
  8.   --minSupport (-s) minSupport        (Optional) Minimum Support. Default         
  9.                                       Value: 2                                    
  10.   --analyzerName (-a) analyzerName    The class name of the analyzer              
  11.   --chunkSize (-chunk) chunkSize      The chunkSize in MegaBytes. 100-10000 MB    
  12.   --output (-o) output                The directory pathname for output.          
  13.   --input (-i) input                  Path to job input directory.                
  14.   --minDF (-md) minDF                 The minimum document frequency.  Default    
  15.                                       is 1                                        
  16.   --maxDFSigma (-xs) maxDFSigma       What portion of the tf (tf-idf) vectors     
  17.                                       to be used, expressed in times the          
  18.                                       standard deviation (sigma) of the           
  19.                                       document frequencies of these vectors.      
  20.                                       Can be used to remove really high           
  21.                                       frequency terms. Expressed as a double      
  22.                                       value. Good value to be specified is 3.0.   
  23.                                       In case the value is less then 0 no         
  24.                                       vectors will be filtered out. Default is    
  25.                                       -1.0.  Overrides maxDFPercent               
  26.   --maxDFPercent (-x) maxDFPercent    The max percentage of docs for the DF.      
  27.                                       Can be used to remove really high           
  28.                                       frequency terms. Expressed as an integer    
  29.                                       between 0 and 100. Default is 99.  If       
  30.                                       maxDFSigma is also set, it will override    
  31.                                       this value.                                 
  32.   --weight (-wt) weight               The kind of weight to use. Currently TF     
  33.                                       or TFIDF                                    
  34.   --norm (-n) norm                    The norm to use, expressed as either a      
  35.                                       float or "INF" if you want to use the       
  36.                                       Infinite norm.  Must be greater or equal    
  37.                                       to 0.  The default is not to normalize      
  38.   --minLLR (-ml) minLLR               (Optional)The minimum Log Likelihood        
  39.                                       Ratio(Float)  Default is 1.0                
  40.   --numReducers (-nr) numReducers     (Optional) Number of reduce tasks.          
  41.                                       Default Value: 1                            
  42.   --maxNGramSize (-ng) ngramSize      (Optional) The maximum size of ngrams to    
  43.                                       create (2 = bigrams, 3 = trigrams, etc)     
  44.                                       Default Value:1                             
  45.   --overwrite (-ow)                   If set, overwrite the output directory      
  46.   --help (-h)                         Print out help                              
  47.   --sequentialAccessVector (-seq)     (Optional) Whether output vectors should    
  48.                                       be SequentialAccessVectors. If set true     
  49.                                       else false                                  
  50.   --namedVector (-nv)                 (Optional) Whether output vectors should    
  51.                                       be NamedVectors. If set true else false     
  52.   --logNormalize (-lnorm)             (Optional) Whether output vectors should    
  53.                                       be logNormalize. If set true else false   

在昨天算法的终端信息中该步骤的调用命令如下:

 

 

[python] view plaincopy
 
  1. ./bin/mahout seq2sparse -i /home/mahout/mahout-work-mahout/20news-seq -o /home/mahout/mahout-work-mahout/20news-vectors -lnorm -nv -wt tfidf  

我们只看对应的参数,首先是-lnorm 对应的解释为输出向量是否要使用log函数进行归一化(设置则为true),-nv解释为输出向量被设置为named 向量,这里的named是啥意思?(暂时不清楚),-wt tfidf解释为使用权重的算法,具体参考http://zh.wikipedia.org/wiki/TF-IDF 。

 

第(1)步在SparseVectorsFromSequenceFiles的253行的:

 

[java] view plaincopy
 
  1. DocumentProcessor.tokenizeDocuments(inputDir, analyzerClass, tokenizedPath, conf);  

这里进入可以看到使用的Mapper是:SequenceFileTokenizerMapper,没有使用Reducer。Mapper的代码如下:

 

 

[java] view plaincopy
 
  1. protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {  
  2.     TokenStream stream = analyzer.reusableTokenStream(key.toString(), new StringReader(value.toString()));  
  3.     CharTermAttribute termAtt = stream.addAttribute(CharTermAttribute.class);  
  4.     StringTuple document = new StringTuple();  
  5.     stream.reset();  
  6.     while (stream.incrementToken()) {  
  7.       if (termAtt.length() > 0) {  
  8.         document.add(new String(termAtt.buffer(), 0, termAtt.length()));  
  9.       }  
  10.     }  
  11.     context.write(key, document);  
  12.   }  

该Mapper的setup函数主要设置Analyzer的,关于Analyzer的api参考:http://lucene.apache.org/core/3_0_3/api/core/org/apache/lucene/analysis/Analyzer.html ,其中在map中用到的函数为reusableTokenStream(String fieldName, Reader reader) :Creates a TokenStream that is allowed to be re-used from the previous time that the same thread called this method.
编写下面的测试程序:

 

 

[java] view plaincopy
 
  1. package mahout.fansy.test.bayes;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.StringReader;  
  5.   
  6. import org.apache.hadoop.conf.Configuration;  
  7. import org.apache.hadoop.io.Text;  
  8. import org.apache.lucene.analysis.Analyzer;  
  9. import org.apache.lucene.analysis.TokenStream;  
  10. import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;  
  11. import org.apache.mahout.common.ClassUtils;  
  12. import org.apache.mahout.common.StringTuple;  
  13. import org.apache.mahout.vectorizer.DefaultAnalyzer;  
  14. import org.apache.mahout.vectorizer.DocumentProcessor;  
  15.   
  16. public class TestSequenceFileTokenizerMapper {  
  17.   
  18.     /** 
  19.      * @param args 
  20.      */  
  21.     private static Analyzer analyzer = ClassUtils.instantiateAs("org.apache.mahout.vectorizer.DefaultAnalyzer",  
  22. Analyzer.class);  
  23.     public static void main(String[] args) throws IOException {  
  24.         testMap();  
  25.     }  
  26.       
  27.     public static void testMap() throws IOException{  
  28.         Text key=new Text("4096");  
  29.         Text value=new Text("today is also late.what about tomorrow?");  
  30.         TokenStream stream = analyzer.reusableTokenStream(key.toString(), new StringReader(value.toString()));  
  31.         CharTermAttribute termAtt = stream.addAttribute(CharTermAttribute.class);  
  32.         StringTuple document = new StringTuple();  
  33.         stream.reset();  
  34.         while (stream.incrementToken()) {  
  35.           if (termAtt.length() > 0) {  
  36.             document.add(new String(termAtt.buffer(), 0, termAtt.length()));  
  37.           }  
  38.         }  
  39.         System.out.println("key:"+key.toString()+",document"+document);  
  40.     }  
  41.   
  42. }  

得出的结果如下:

 

 

[plain] view plaincopy
 
  1. key:4096,document[today, also, late.what, about, tomorrow]  

其中,TokenStream有一个stopwords属性,值为:[but, be, with, such, then, for, no, will, not, are, and, their, if, this, on, into, a, or, there, in, that, they, was, is, it, an, the, as, at, these, by, to, of],所以当遇到这些单词的时候就不进行计算了。

http://blog.csdn.net/fansy1990/article/details/10478515

 

http://soledede.com/

 

大家可以加我个人微信号:scccdgf

 

 

或者关注soledede的微信公众号:soledede
微信公众号:
分享到:
评论

相关推荐

    Twenty Newsgroups 数据集

    "Twenty Newsgroups" 数据集是一个广泛用于文本分类和自然语言处理任务的经典数据集,它由约20000篇电子新闻文章组成,这些文章分别来自20个不同的新闻组。这个数据集最初是在1998年由卡内基梅隆大学的Andrew Moore...

    newsgroups数据集下载,机器学习必用

    找到D:\software-1\anaconda\Lib\site-packages\sklearn\datasets\_twenty_newsgroups.py下的_twenty_newsgroups.py文件,找到函数_download_20newsgroups,注释#logger.info("Downloading dataset from %s (14 MB)...

    PJBlog2 Twenty风格

    标题 "PJBlog2 Twenty风格" 暗示我们正在讨论的是一个针对PJBlog2平台的特定主题或模板,名为"Twenty"。PJBlog2是一款基于ASP.NET开发的个人博客系统,它允许用户轻松创建和管理自己的博客网站。这个风格可能为用户...

    Twenty Lectures on Algorithmic Game Theory

    Twenty Lectures on Algorithmic Game Theory By Tim Roughgarden 2016 | 250 Pages | ISBN: 131662479X , 1107172667 | EPUB | 2 MB Computer science and economics have engaged in a lively interaction over ...

    20newsgroup数据集-机器学习-标准数据集(all)下载方式 from sklearn.datasets import fetch_20newsgrou

    1. `subset`: 可以选择加载全部数据(`'all'`),或者特定子集,如训练集(`'train'`)、测试集(`'test'`)或验证集(`'unsupervised'`)。 2. `shuffle`: 如果为`True`,则在加载数据前先随机打乱数据顺序。 3. `remove`: ...

    twenty-twenty-one-child-theme

    标题 "twenty-twenty-one-child-theme" 指的是一个基于 WordPress 的子主题,它构建于官方的 Twenty Twenty-One 主题之上。Twenty Twenty-One 是 WordPress 在2021年推出的默认主题,以其简洁的设计和对无障碍功能的...

    tf_seq2seq_chatbot:[未维护]

    tensorflow seq2seq聊天机器人注意:不维护存储库。 如果您想承担维护费用,请随时与我联系。 基于在tensorflow中实现的热方法构建通用对话聊天。 由于到目前为止效果不佳,因此请考虑其他实现。 当前结果非常糟糕:...

    视频One to twenty-three.zip

    《张飞硬件视频1到二十三部》是针对嵌入式硬件开发的全面教程,旨在帮助初学者快速掌握硬件开发的基础知识和技能。这23部视频由知名的硬件专家张飞主讲,以其深入浅出的讲解风格和实战导向的教学方法,使复杂的硬件...

    Effective learning- Twenty rules of formulating k.pdf

    《高效学习-制定知识的二十条规则》是由Piotr Wozniak博士撰写的一篇文章,发表于1999年2月。它旨在帮助读者解决在试图加速学习过程中可能遇到的最大难题之一:知识的制定。文章明确指出,知识制定的方式会直接影响...

    beautiful-twenty.rar

    标题中的"beautiful-twenty.rar"可能是指一个名为“美丽20”的项目或资源包,而“.rar”是常见的压缩文件格式,用于集合多个文件在一个单一的可下载包中。这个压缩包似乎包含了与3D建模和游戏开发相关的素材。 在...

    twenty20

    twenty20

    twenty news DataSet

    20news数据集。... The 20 newsgroups collection has become a popular data set for experiments in text applications of machine learning techniques, such as text classification and text clustering.

    game twenty four.exe

    game twenty four.exe

    Twenty-First Century Intelligence

    Twenty-First Century Intelligence

    Trade Gothic-Bold Cond Twenty

    Trade Gothic-Bold Cond Twenty

    html5单页模版Twenty是一款清爽风格的城市交通工具html5单页网站模版。.zip

    1. **响应式设计**:Twenty模板采用了响应式布局,确保在不同设备(如桌面、平板、手机)上都能提供良好的视觉效果。它通过媒体查询(Media Queries)来调整内容的展示方式,以适应不同屏幕尺寸。 2. **语义化标签*...

    html5单页模版Twenty

    在功能方面,Twenty模板可能包含了一些互动组件,例如滑动显示、轮播图、下拉菜单等,这些都是通过JavaScript和CSS3实现的。JavaScript在HTML5环境中扮演着增强用户体验的角色,它可以实现动态更新内容、表单验证、...

    SUMS78 Twenty-One Lectures on Complex Analysis.zip

    复数是由实部和虚部组成的数,可以用形式 \( a + bi \) 来表示,其中 \( a \) 和 \( b \) 是实数,\( i \) 是虚数单位,满足 \( i^2 = -1 \)。复分析主要研究复平面上的函数,包括它们的导数、积分、级数、解析延拓...

    二年级英语下册 Unit 2 You are Late today(1)教案 新世纪版.doc

    【二年级英语下册 Unit 2 You are Late today(1)教案 新世纪版】是一份针对小学二年级学生的英语教学方案,旨在培养学生的语言技能,尤其是与时间相关的词汇和表达方式。以下是教案中的主要知识点: 1. **教育目标*...

    小学英语汇总教学之日期月份序数词PPT学习教案.pptx

    在小学英语教学中,日期和月份的序数词是重要的基础知识之一,这有助于学生掌握基本的时间表达法。这篇PPT学习教案旨在帮助小学生系统地学习和掌握这些概念。 首先,我们要了解月份的英文名称和其对应的缩写形式。...

Global site tag (gtag.js) - Google Analytics