如何在Python中将Count向量化数据转换回文本数据?

ash*_*pen 5 python pandas scikit-learn sklearn-pandas

如何将计数矢量化文本数据转换回文本形式。我有文本数据,我使用 countvectorizer 将其制成稀疏矩阵进行分类。现在我希望将文本数据的稀疏矩阵转换回文本数据。

我的代码

 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)
Run Code Online (Sandbox Code Playgroud)

Max*_*axU 5

我认为这是不可能的——所有标点符号、空格、制表符都已被删除。所有单词也已转换为小写。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
Run Code Online (Sandbox Code Playgroud)