Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用TfidfVectorizer理解字符级特征提取_Python_Machine Learning_Nlp_Tfidfvectorizer - Fatal编程技术网

Python 使用TfidfVectorizer理解字符级特征提取

Python 使用TfidfVectorizer理解字符级特征提取,python,machine-learning,nlp,tfidfvectorizer,Python,Machine Learning,Nlp,Tfidfvectorizer,我有一组样本,每个样本都有三个单词:名字、中间名和姓氏 text = ['James Jackson Jammy', 'Steve Smith Something', 'Chamak Chalo Chanta', 'Polo Rolo Colo'] 尝试用神经网络的输入进行解析,我想为名称ex for james->ja,am,me,es提取字符级tf,并将其保存为数组,以将其交给神经网络进行分类 使用,我试图从语料库中提取特定单词的tf # c

我有一组样本,每个样本都有三个单词:名字、中间名和姓氏

text = ['James Jackson Jammy',
        'Steve Smith Something',
        'Chamak Chalo Chanta',
        'Polo Rolo Colo']
尝试用神经网络的输入进行解析,我想为名称ex for james->ja,am,me,es提取字符级tf,并将其保存为数组,以将其交给神经网络进行分类

使用,我试图从语料库中提取特定单词的tf

# creating the corps
corpus =[]
# splitting the text in to words
corpus = ' '.join(text[i] for i in range(len(text))).split()
# copy only unique words 
corpus = set(corpus)
# creating the character vector (2 letters)
vectorizer = TfidfVectorizer(ngram_range=(1,2), analyzer= 'char')
X = vectorizer.fit(corpus)
# checking the vector of the one word
# ab = X.transform(['Chamak Chalo Chanta'])
ab = X.transform(['Chamak'])
print(ab.shape)
print(ab)   
当我检查输出时,我得到如下结果

  (1,55)

  (0, 28)   0.38126785705606514
  (0, 27)   0.23541325871187607
  (0, 23)   0.3274372645024392
  (0, 16)   0.28924385126550206
  (0, 15)   0.23541325871187607
  (0, 7)    0.28924385126550206
  (0, 6)    0.23541325871187607
  (0, 4)    0.28924385126550206
  (0, 2)    0.38126785705606514
  (0, 0)    0.4298956344860669
它说形状是1,55,我不明白它显示的是什么向量。值0,0…0,28有什么意义吗。对于单词‘chamak’,我希望它应该显示‘ch’、‘ha’、‘am’、‘ma’、‘ak’的tf值,但值是55而不是5

当我使用ngram_range=1,3时,输出为

(1, 91)
(0, 49) 0.30927373541425635
(0, 48) 0.30927373541425635
(0, 47) 0.1909605977541359
(0, 42) 0.26560787654230167
(0, 29) 0.30927373541425635
(0, 27) 0.23462645662609066
(0, 26) 0.1909605977541359
(0, 14) 0.23462645662609066
(0, 13) 0.23462645662609066
(0, 12) 0.1909605977541359
(0, 7)  0.30927373541425635
(0, 6)  0.23462645662609066
(0, 3)  0.30927373541425635
(0, 0)  0.34871921735651773
当我把射程增加到3,而不是像cha,ham,amp这样的3。。它应该减少,但为什么会增加


我对这个概念的理解出错了,但我做错了吗?我可以使用向量输入神经网络吗?对于我想要的输出,ch、am、ma、ap、pa、ak 6个向量的tf是我打印出来的向量是正确的吗

形状是1,55,因为55是整个n-gram词汇的大小。在一个包含1个文本的列表上调用transform时,输出形状仍将是1,55,如果在2个文本上调用它,则输出形状将是2,55。输出中的元组意味着词汇表中索引0,x处的gram是单词中的gram。浮点数与文档频率相反


此外,我认为您误解了ngram_范围参数的工作方式。当你输入1,3而不是1,2时,你会问为什么它会增加而不会减少。这是因为当您输入1,3时,它会在词汇表中同时存储单字、双字和三字。

收到了,非常感谢。我已将范围修改为ngrams=2,2,以获得所需答案。