the bag-of-words representation is implemented in countvectorizerwhich is transformer let' first apply it to toy datasetconsisting of two samplesto see it workingin[ ]bards_words =["the fool doth think he is wise,""but the wise man knows himself to be fool"we import and instantiate the countvectorizer and fit it to our toy data as followsin[ ]from sklearn feature_extraction text import countvectorizer vect countvectorizer(vect fit(bards_wordsfitting the countvectorizer consists of the tokenization of the training data and building of the vocabularywhich we can access as the vocabulary_ attributein[ ]print("vocabulary size{}format(len(vect vocabulary_))print("vocabulary content:\ {}format(vect vocabulary_)out[ ]vocabulary size vocabulary content{'the' 'himself' 'wise' 'he' 'doth' 'to' 'knows' 'man' 'fool' 'is' 'be' 'think' 'but' the vocabulary consists of wordsfrom "beto "wiseto create the bag-of-words representation for the training datawe call the transform methodin[ ]bag_of_words vect transform(bards_wordsprint("bag_of_words{}format(repr(bag_of_words))out[ ]bag_of_wordswith stored elements in compressed sparse row formatthe bag-of-words representation is stored in scipy sparse matrix that only stores the entries that are nonzero (see the matrix is of shape with one row for each of the two data points and one feature for each of the words in the vocabulary sparse matrix is used as most documents only contain small subset of the words in the vocabularymeaning most entries in the feature array are think representing text data as bag of words