>>> from sklearn.feature_extraction.text import CountVectorizer
>>> corpus = [
... 'This is the first document.',
... 'This document is the second document.',
... 'And this is the third one.',
... 'Is this the first document?',
... ]
>>> vectorizer = CountVectorizer()
>>> X = vectorizer.fit_transform(corpus)
>>> print(vectorizer.get_feature_names())
['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']
>>> print(X.toarray())
[[0 1 1 1 0 0 1 0 1]
[0 2 0 1 0 1 1 0 1]
[1 0 0 1 1 0 1 1 1]
[0 1 1 1 0 0 1 0 1]]
# analyzer は default で "word" を使うので .lower() を対象となるデータに使おうとするため、データが list 型だとエラーになる。そのため analyzer には list 型をそのまま返すような処理を書いている
>>> vectorizer = CountVectorizer(analyzer=lambda x: x)
>>> X = vectorizer.fit_transform(df['multi_select_values'])
# feature の名前を取得できる
>>> print(vectorizer.get_feature_names())
['test1', 'test2', 'test3', 'test4']
# toarray() すると numpy.ndarray で結果を取得できる
>>> print(X.toarray())
[[1 1 0 0]
[0 1 1 0]
[0 0 0 1]]
# dataframe にするには以下など (ただし選択肢が日本語の場合は、カラム名が日本語になってしまう)
>>> pd.DataFrame(X.toarray(), columns=vectorizer.get_feature_names())