特征选择
来源:互联网 发布:奇异矩阵 线性相关 编辑:程序博客网 时间:2024/04/29 16:02
文本特征提取的方法很多,如基于文本频率的特征提取法、信息增益法、卡方检验法和互信息法等。本文采用基于文本特征频率的提取法,计算文档频率DF,即计算出现某个term在同类文档中的DF。根据计算出来的DF值来选取能代表某类文档的特征词。
直接上代码:
#!/usr/bin/env python# -*- coding: utf-8 -*-"""功能:时间:"""import codecsimport osimport re# 目录base = "C:\\Users\\Administrator\\Desktop\\delstopwords3"for j in range(1, 10): alltext = [] allwords = [] path = os.path.join(base, str(j)) textnames = os.listdir(path) for name in textnames: temp = [] f = codecs.open(os.path.join(path, name), "r") for line in f: try: temp.append(line.strip().decode("utf-8")) except: print u"某个词编码有问题..." continue alltext.append(temp) allwords += temp print u"文本个数:", len(alltext) print u"词汇个数:", len(allwords) print u"词汇种数:", len(set(allwords)) word_df = [] for word in set(allwords): count = 0 for text in alltext: if word in text: count += 1 word_df.append([word, str(count)]) # 存储形式[word,DF] # 输出,之所以不写入文本,是因为总有一些奇怪的字符会报错。。。 word_df.sort(key=lambda x: int(x[1]), reverse=True) # 词频从大到小排序 f_df = codecs.open('%d.txt' % j, "a", encoding="utf-8") f_df.truncate() for item in word_df: f_df.write(item[0] + "\t" + item[1] + "\n") f_df.close()print u"程序结束!"
0 0
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 特征选择
- 计算机杂谈
- cocos2dx 启动渲染过程
- 全排列-字典序
- 依赖库Underscore系列(2)--数组
- Packing data with Python
- 特征选择
- 用AFN发送字符串形式的Json数据给服务器
- 08-CoreData学习总结
- linkedList 总结
- android技术晋升之道
- 用android-support-multidex解决Dex超出方法数的限制问题
- K-means算法
- sql 查询男女
- 已经划分成组 的 字符串逆至