sklearn の CountVectorizer の挙動についてメモしておきます
sklearn の version は 0.22.2.post1 です
sklearn.feature_extraction.text.CountVectorizer - scikit-learn 0.24.2 documentation
class sklearn.feature_extraction.text. CountVectorizer( *, input='content', encoding='utf-8', decode_error='strict', strip_accents=None, lowercase=True, preprocessor=None, tokenizer=None, stop_words=None, token_pattern='(?u)\\b\\w\\w+\\b', ngram_range=(1, 1), analyzer='word', max_df=1.0, min_df=1, max_features=None, vocabulary=None, binary=False, dtype= ) Convert a collection of text documents to a matrix of token counts This implementation produces a sparse representation of the counts using scipy.sparse.csr_matrix.
scikit-learn.org
CountVectorizer を使うと、text document を単語の出現頻度のマトリックスに変換できます
これを使うと、Pandas に格納されている list 型のデータを one hot encoding なデータとして展開できます
data の準備
>>> df = pd.DataFrame([
... [1, ["test1", "test2"]],
... [2, ["test2", "test3"]],
... [3, ["test4"]]
... ], columns=['user_id', 'multi_select_values'])
>>> df['multi_select_values']
0 [test1, test2]
1 [test2, test3]
2 [test4]
Name: multi_select_values, dtype: objectfit する