如何将计数矢量化文本数据转换回文本形式。我有文本数据,我使用计数矢量器将其制作成稀疏矩阵进行分类。现在,我希望将文本数据的稀疏 martix 转换回文本数据。
我的代码
cv = CountVectorizer( max_features = 500,analyzer='word')
cv_addr = cv.fit_transform(data.pop('Clean_addr'))
for i, col in enumerate(cv.get_feature_names()):
data[col] = pd.SparseSeries(cv_addr[:, i].toarray().ravel(), fill_value=0)
我认为
这是不可能的 - 所有标点符号、空格、制表符都已删除。此外,所有单词都已转换为小写。AFAIK 没有办法让它恢复为原始格式。因此,您最好保留Clean_addr
列而不是删除它。
演示:
In [18]: df
Out[18]:
txt
0 a sample text
1 to be, or not to be, that is the question
In [19]: from sklearn.feature_extraction.text import CountVectorizer
In [20]: cv = CountVectorizer(max_features = 500, analyzer='word')
In [21]: cv_addr = cv.fit_transform(df['txt'])
In [22]: x = pd.SparseDataFrame(cv_addr, columns=cv.get_feature_names(),
index=df.index, default_fill_value=0)
In [23]: x
Out[23]:
be is not or question sample text that the to
0 0 0 0 0 0 1 1 0 0 0
1 2 1 1 1 1 0 0 1 1 2
In [24]: df.join(x)
Out[24]:
txt be is not or question sample text that the to
0 a sample text 0 0 0 0 0 1 1 0 0 0
1 to be, or not to be, that is the question 2 1 1 1 1 0 0 1 1 2