当前位置:   article > 正文

关于自然语言中特征提取_自然语言处理 提取词语特征

自然语言处理 提取词语特征

自然语言处理(NLP)中,特征提取是将文本数据转换为数值特征的过程,以便于机器学习算法进行处理和分析。特征提取在文本分类、情感分析、信息检索等任务中非常重要。下面介绍几种常用的文本特征提取方法及其示例代码。

1.词袋模型(Bag of Words)

词袋模型将文本表示为一个固定大小的向量,每个维度对应词汇表中的一个词,值表示该词在文本中出现的频次或者用TF-IDF进行加权。

from sklearn.feature_extraction.text import CountVectorizer

# 创建一个CountVectorizer对象
vectorizer = CountVectorizer()

# 示例文本
corpus = [
    'This is the first document.',
    'This document is the second document.',
    'And this is the third one.',
    'Is this the first document?',
]

# 将文本转换为词袋模型的特征向量
X = vectorizer.fit_transform(corpus)

# 打印特征向量的稀疏矩阵表示
print("词袋模型特征向量:")
print(X.toarray())

# 打印词汇表
print("词汇表:")
print(vectorizer.get_feature_names_out())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

2. TF-IDF特征(Term Frequency - Inverse Document Frequency)

TF-IDF是一种用于评估一个词对于一个文档集或一个语料库中的某个文档的重要程度的统计方法。

from sklearn.feature_extraction.text import TfidfVectorizer

# 创建一个TfidfVectorizer对象
vectorizer = TfidfVectorizer()

# 示例文本(与前面例子相同)
corpus = [
    'This is the first document.',
    'This document is the second document.',
    'And this is the third one.',
    'Is this the first document?',
]

# 将文本转换为TF-IDF特征向量
X = vectorizer.fit_transform(corpus)

# 打印TF-IDF特征向量的稀疏矩阵表示
print("TF-IDF特征向量:")
print(X.toarray())

# 打印词汇表
print("词汇表:")
print(vectorizer.get_feature_names_out())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

3. Word Embeddings词嵌入

词嵌入是将每个单词映射到一个低维向量空间中的技术,它们捕捉了单词之间的语义关系。

from gensim.models import Word2Vec
from nltk.tokenize import word_tokenize

# 示例文本
corpus = [
    'This is the first document.',
    'This document is the second document.',
    'And this is the third one.',
    'Is this the first document?',
]

# 使用NLTK进行词汇分词
tokenized_corpus = [word_tokenize(doc.lower()) for doc in corpus]

# 训练Word2Vec模型
model = Word2Vec(tokenized_corpus, vector_size=100, window=5, min_count=1, sg=0)

# 获取文档中每个单词的词向量的平均值作为文档的特征向量
def document_vector(doc):
    doc_vec = np.zeros_like(model.wv['word'])  # 初始化为与词向量相同维度的零向量
    num_words = 0
    for word in doc:
        if word in model.wv:
            doc_vec += model.wv[word]
            num_words += 1
    if num_words > 0:
        doc_vec /= num_words
    return doc_vec

# 计算每个文档的特征向量
document_vectors = [document_vector(doc) for doc in tokenized_corpus]

# 打印每个文档的特征向量
for i, vec in enumerate(document_vectors):
    print(f"文档{i+1}的特征向量:")
    print(vec)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/902125
推荐阅读
相关标签
  

闽ICP备14008679号