NLP:自然语言处理技术中常用的文本特征表示方法(整数编码、one-hot编码法、BOW法、TF-IDF法、N-Gram法等)及其代码案例实现
目录
自然语言处理技术中常用的文本特征表示方法(整数编码、one-hot编码法、BOW法、TF-IDF法、N-Gram法等)及其代码案例实现
BOW词袋法(停用词):利用CountVectorizer函实现BOW词袋法对英文文本实现词频统计、输出词汇表
TF-IDF词频-逆向文件频率:基于sklearn库对英文文本计算TF-IDF实现文本特征表示
T1、基于sklearn实现
T2、基于jieba实现
文本特征提取方法。对于每一个训练文本,它只考虑每种词汇在该训练文本中出现的频率。
CountVectorizer会将文本中的词语转换为词频矩阵,它通过fit_transform函数计算各个词语出现的次数。
from sklearn.feature_extraction.text import CountVectorizer
sent1 = 'I am convinced that God does not play dice.'
sent2 = 'Intellectuals solve problems; geniuses prevent them.'
x_train = [sent1, sent2]
# T1、BOW词袋法(停用词):利用CountVectorizer函实现BOW词袋法对英文文本实现词频统计、输出词汇表
print('--------------T1.1、不采用停用词法---------------------')
# T1.1、不采用停用词法
# 创建词袋数据结构类:对CountVectorizer进行初始化
count_vec = CountVectorizer()
X_count_train = count_vec.fit_transform(x_train)
X_count_train= X_count_train.toarray() # 将原始训练文本转化为特征向量
print(X_count_train)
# print(count_vec.get_feature_names()) # 按照顺序输出特征词
# 输出词汇表:输出特征词及其索引
vocabulary = count_vec.vocabulary_
# print(vocabulary)
vocabulary_sort=dict(sorted(vocabulary.items(),key=lambda x:x[0],reverse=False)) # True False
print(vocabulary_sort)
print('--------------T1.2、采用停用词法---------------------')
# T1.2、采用停用词法
#从文件导入停用词表
stpwrdpath ="F:\File_Python\Resource\data_NLP\stopwords.txt"
with open(stpwrdpath, 'rb') as fp:
stopword = fp.read().decode('utf-8')
stpwrdlst = stopword.splitlines() # 将停用词表转换为list
# print(stpwrdlst)
# 创建词袋数据结构类:对CountVectorizer进行初始化,采用去除停用词
count_vec=CountVectorizer(stop_words=stpwrdlst)
X_count_train = count_vec.fit_transform(x_train)
X_count_train= X_count_train.toarray() # 将原始训练文本转化为特征向量
print(X_count_train)
# 输出词汇表:输出特征词及其索引
vocabulary = count_vec.vocabulary_
vocabulary_sort=dict(sorted(vocabulary.items(),key=lambda x:x[0],reverse=False)) # True False
print(vocabulary_sort)
--------------T1.1、不采用停用词法---------------------
[[1 1 1 1 0 1 0 1 1 0 0 0 1 0]
[0 0 0 0 1 0 1 0 0 1 1 1 0 1]]
{'am': 0, 'convinced': 1, 'dice': 2, 'does': 3, 'geniuses': 4, 'god': 5, 'intellectuals': 6, 'not': 7, 'play': 8, 'prevent': 9, 'problems': 10, 'solve': 11, 'that': 12, 'them': 13}
--------------T1.2、采用停用词法---------------------
[[1 1 0 1 0 1 0 0 0]
[0 0 1 0 1 0 1 1 1]]
{'convinced': 0, 'dice': 1, 'geniuses': 2, 'god': 3, 'intellectuals': 4, 'play': 5, 'prevent': 6, 'problems': 7, 'solve': 8}
原理及其更多代码可参考:https://blog.csdn.net/asialee_bird/article/details/81486700
# TF-IDF:基于sklearn库对英文文本计算TF-IDF实现文本特征表示
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
x_train = ['ML includes SVM','ML includes kNN','ML belongto AI']
x_test = ['SVM and kNN']
# 词汇表共计个词汇:[ML,includes,SVM,kNN,belongto,AI]
count_vec = CountVectorizer() # 将文本中的词语转换为词频矩阵,矩阵元素a[i][j] 表示j词在i类文本下的词频
x_train2vec = count_vec.fit_transform(x_train)
# 输出词汇表:输出特征词及其索引
vocabulary = count_vec.vocabulary_
print(vocabulary)
vocabulary_sort=dict(sorted(vocabulary.items(),key=lambda x:x[0],reverse=False)) # True False
print(vocabulary_sort)
tf_idf_transformer = TfidfTransformer() # 统计每个词语的tf-idf权值
x_train2vec2tfidf = tf_idf_transformer.fit_transform(x_train2vec) # 将文本转为词频矩阵并计算tf-idf
x_train_weight = x_train2vec2tfidf.toarray() #将tf-idf矩阵抽取出来,元素a[i][j]表示j词在i类文本中的tf-idf权重
#对测试集进行tf-idf权重计算
x_test2vec = count_vec.transform(x_test)
x_test2vec2tfidf = tf_idf_transformer.transform(x_test2vec)
x_test_weight = x_test2vec2tfidf.toarray() # 测试集TF-IDF权重矩阵
print('输出x_train文本向量:')
print(x_train_weight)
print('输出x_test文本向量:')
print(x_test_weight)
{'ml': 4, 'includes': 2, 'svm': 5, 'knn': 3, 'belongto': 1, 'ai': 0}
{'ai': 0, 'belongto': 1, 'includes': 2, 'knn': 3, 'ml': 4, 'svm': 5}
输出x_train文本向量:
[[0. 0. 0.54783215 0. 0.42544054 0.72033345]
[0. 0. 0.54783215 0.72033345 0.42544054 0. ]
[0.65249088 0.65249088 0. 0. 0.38537163 0. ]]
输出x_test文本向量:
[[0. 0. 0. 0.70710678 0. 0.70710678]]
import jieba.analyse
text='关键词是能够表达文档中心内容的词语,常用于计算机系统标引论文内容特征、
信息检索、系统汇集以供读者检阅。关键词提取是文本挖掘领域的一个分支,是文本检索、
文档比较、摘要生成、文档分类和聚类等文本挖掘研究的基础性工作'
keywords=jieba.analyse.extract_tags(text, topK=5, withWeight=False, allowPOS=())
print(keywords)