【问题标题】:tf-idf keep feature matrix shape when transforming test datatf-idf 在转换测试数据时保持特征矩阵形状
【发布时间】:2020-10-15 13:53:20
【问题描述】:
我正在使用tf-idf(text frequency-inverse document frequency) 将我的文本输入转换为特征矩阵。假设每 10 个不同的单词取五个句子。
所以我使用了input_data(5,50)。
什么时候用10个词预测句子的模型,我的特征矩阵的大小是(1,10)对。所以模型抛出ValueError--Cannot feed value of shape.
我应该采用哪种方式或方法来消除此错误..
【问题讨论】:
标签:
python
machine-learning
deep-learning
neural-network
【解决方案1】:
这由TfidfVectorizer 处理。不确定你做错了什么(因为没有代码),但是通过拟合一组字符串(句子)并转换看不见的数据,转换器没有看到的单词会被忽略。这是一个例子:
from sklearn.feature_extraction.text import TfidfVectorizer
l = ['Some test string', 'just another string']
tfidf = TfidfVectorizer()
t = tfidf.fit(l)
t.transform(l).todense()
matrix([[0. , 0. , 0.6316672 , 0.44943642, 0.6316672 ],
[0.6316672 , 0.6316672 , 0. , 0.44943642, 0. ]])
tfidf.transform(['String with unseen words']).todense()
matrix([[0., 0., 0., 1., 0.]])