How to convert Countvectorized data back to text data in Python?

Viewed 1511

how can I convert count vectorized text data back to textual form. I have text data which I had made into sparse matrix using countvectorizer for classification. Now I want the sparse martix of text data to be converted back into text data.

My code

 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)
1 Answers

I don't think it's possible - all punctuations, spaces, tabs have been removed. Also all words have been converted to lower case. AFAIK there is no way to get it back in the original format. So you'd better keep Clean_addr column instead of dropping it.

Demo:

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
Related