繁体   English   中英

在文本分析的情况下,当我应用 fit() 方法时,究竟会发生什么? transform() 对文本数据做了什么?

[英]In case of text analysis, when I apply fit() method, what exactly happens? And what does transform() do on the text data?

在文本分析的情况下,当我应用 fit() 方法时,究竟会发生什么? transform() 对数据做了什么?

我可以理解数字数据类型,但无法将其可视化为文本数据。

我有一个文本数组

sents_processed[0:5]
['so there is no way for me plug in here in us unless go by converter',
 'good case excellent value',
 'great for jawbone',
 'tied charger for conversations lasting more than minutes major problems',
 'mic is great']

现在要对其进行矢量化,我使用 CountVectorizer class:

from sklearn.feature_extraction.text import CountVectorizer

vectorizer = CountVectorizer(analyzer= 'word', tokenizer= None, preprocessor= None, stop_words= None, max_features= 4500)
data_features = vectorizer.fit_transform(sents_processed)
print(data_features.toarray())
[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

我知道我会得到 4500 长度的向量。 但是,我无法想象究竟拟合方法会在幕后做什么,以及如何通过 transform function 转换数据? 特别是给定的数据是文本类型。

我们举一个简单的例子:

from sklearn.feature_extraction.text import CountVectorizer
text = ['this is a sentence', 'this is another sentence', 'not a sentence']

这里我有三句话

vector = CountVectorizer(analyzer= 'word', tokenizer= None, max_features= 4500)
dt = vector.fit_transform(text)

这个过程的第一步是创建一个词汇表。 它为所有句子中的每个单词分配一个数字

print(vector.vocabulary_) = {'this': 4, 'is': 1, 'sentence': 3, 'another': 0, 'not': 2}

现在它处理单词的相应索引而不是单词本身。 现在方法 <vector.fit_transform()> 根据词汇表中提供的索引将这些句子转换为数字

data_features = vectorizer.fit_transform(text)
print(data_features.toarray())
= [[0 1 0 1 1]
 [1 1 0 1 1]
 [0 0 1 1 0]]

如果您只是分析数组,它只会显示句子。 在五个单词的词汇表中以数组形式表示一个句子,首先我们有一个由五个(词汇量大小)零组成的数组,表示一个空句子

[0, 0, 0, 0, 0].

现在,如果我们拿起我们的第一句话并将 1 放入上述数组中对应的索引处,我们将得到该数组

[0            1(is)       0          1(sentence)           1(this)]
[1(another)   1(is)       0          1(sentence)           1(this)]
[0            0           1(not)     1(sentence)           0      ]

如果该词出现在该句子中,则为 1,否则为 0

您只需仔细查看并了解它是如何出现的,或者您可以阅读有关 word Embedding 的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM