scikit-learn:0.3. 从文本文件中提取特征(tf、tf-idf)、训练一个分类器

来源:互联网 发布:dsa数据 编辑:程序博客网 时间:2024/06/06 02:30

上一篇讲了如何加载数据。

本篇参考:http://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html

主要讲解如下部分:

Extracting features from text files

Training a classifier




跑模型之前,需要将文本文件的内容转换为数字特征向量。常见的是tf、tf-idf。


1、tf:

首先解决high-dimensional sparse datasetsscipy.sparse matrices就是解决这个问题,scikit-learn 已经内置了该数据结构(built-in support for these structures)。

[python] view plain copy
  1. from sklearn.feature_extraction.text import CountVectorizer  
  2. count_vect = CountVectorizer()  
  3. X_train_counts = count_vect.<strong>fit_transform</strong>(rawData.data)  
  4.   
  5. X_train_counts  
  6. Out[43]:   
  7. <6x11 sparse matrix of type '<type 'numpy.int64'>'  
  8.     with 18 stored elements in Compressed Sparse Row format>  
  9.   
  10. X_train_counts.shape  
  11. Out[44]: (611)  
  12.   
  13. print count_vect.vocabulary_.get(u'like')  
  14. print count_vect.vocabulary_.get(u'good')  
  15. 3  
  16. 1  
  17.   
  18. print rawData_counts  
  19.   (08)        1  
  20.   (00)        1  
  21.   (03)        1  
  22.   (18)        1  
  23.   (13)        1  
  24.   (110)       1  
  25.   (19)        1  
  26.   (28)        1  
  27.   (24)        1  
  28.   (38)        1  
  29.   (36)        1  
  30.   (31)        1  
  31.   (48)        1  
  32.   (42)        1  
  33.   (58)        1  
  34.   (51)        1  
  35.   (55)        1  
  36.   (57)        1  




2、tf-idf:

[python] view plain copy
  1. from sklearn.feature_extraction.text import TfidfTransformer  
  2. tfidf_transformer = TfidfTransformer()  
  3. X_train_tfidf = tfidf_transformer.<strong>fit_transform</strong>(rawData_counts)  
  4. X_train_tfidf.shape  
  5. Out[53]: (611)  
  6.   
  7. X_train_tfidf  
  8. Out[54]:   
  9. <6x11 sparse matrix of type '<type 'numpy.float64'>'  
  10.     with 18 stored elements in Compressed Sparse Row format>  
  11.   
  12. print X_train_tfidf  
  13.   (03)        0.599738830611  
  14.   (00)        0.731376058697  
  15.   (08)        0.324657351406  
  16.   (19)        0.590335838052  
  17.   (110)       0.590335838052  
  18.   (13)        0.484083832074  
  19.   (18)        0.262049690228  
  20.   (24)        0.913996360826  
  21.   (28)        0.405722383406  
  22.   (31)        0.599738830611  
  23.   (36)        0.731376058697  
  24.   (38)        0.324657351406  
  25.   (42)        0.913996360826  
  26.   (48)        0.405722383406  
  27.   (57)        0.590335838052  
  28.   (55)        0.590335838052  
  29.   (51)        0.484083832074  
  30.   (58)        0.262049690228  



3、训练一个分类器:

以naive bayes为例:

[python] view plain copy
  1. from sklearn.naive_bayes import MultinomialNB  
  2. clf = MultinomialNB().fit(X_train_tfidf, rawData.target)  


4、预测:

新文件来了,需要进行完全相同的特征提取过程。不同之处是,我们使用“transform instead of fit_transform on the transformers,因为我们已经在训练集上fit了:

[python] view plain copy
  1. from sklearn.naive_bayes import MultinomialNB  
  2. clf = MultinomialNB().fit(X_train_tfidf, rawData.target)  
  3. docs_new = ['i like this''haha, start.']  
  4. X_new_counts = count_vect.<strong>transform</strong>(docs_new)  
  5. X_new_tfidf = tfidf_transformer.transform(X_new_counts)  
  6. predicted = clf.predict(X_new_tfidf)  
[python] view plain copy
  1. for doc, category in zip(docs_new, predicted):  
  2.     print('%r => %s' % (doc, rawData.target_names[category]))  
  3. 'i like this' => category_2_folder  
  4. 'haha, start.' => category_1_folder  
看来简单预测还是比较准确的啊。。。。








Extracting features from text files
1 0