【发布时间】:2019-04-09 22:01:04
【问题描述】:
我有两个文本字段和其他类似这种格式的功能的数据框:
message feature_1 feature_2 score text
'This is the text' 4 7 10 extra text
'This is more text' 3 2 8 and this is another text
现在我的目标是预测分数,当尝试将此数据帧转换为特征矩阵以将其输入到我的机器学习模型中时,这是我所做的:
# Create vectorizer for function to use
vectorizer = TfidfVectorizer()
# combine the numerical features with the TFIDF generated matrix
X = sp.sparse.hstack( (vectorizer.fit_transform(df.message),
df[['feature_1', 'feature_2']].values, vectorizer.fit_transform(df.text)),
format='csr')
现在打印我的 X 矩阵的形状时,我得到了 2x13,但是当我像这样检查 X_columsn 时:
X_columns = vectorizer.get_feature_names() + df[['feature_1', 'feature_2']].columns.tolist()
我没有得到语料库中的所有单词,它只给我带来了df.text 中存在的单词和df.message 中没有单词的其他特征属性。
['and', 'another', 'extra', 'is', 'text', 'this', 'feature_1', 'feature_2']
如何让 X 包含我所有的数据框功能!!
【问题讨论】:
标签: python dataframe nlp feature-extraction tf-idf