keras and nlp - when to use .texts_to_matrix instead of .texts_to_sequences?

Viewed 816

Keras offers a couple of helper functions to process text:

texts_to_sequences and texts_to_matrix

It seems that most people use texts_to_sequences, but it is unclear to me why one is picked over the other and under what conditions you might want to use texts_to_matrix.

1 Answers

texts_to_matrix is easy to understand. It will convert texts to a matrix with columns refering to words and cells carrying number of occurrence or presence. Such a design will be useful for direct application of ML algorithms (logistic regression, decision tree, etc.)

texts_to_sequence will create lists that are collection of integers representing words. Certain functions like Keras-embeddings require this format for preprocessing.

Consider the example below.

txt = ['Python is great and useful', 'Python is easy to learn', 'Python is easy to implement']
txt = pd.Series(txt)

tok = Tokenizer(num_words=10)
tok.fit_on_texts(txt)
mat_texts = tok.texts_to_matrix(txt, mode='count')
mat_texts

Output: array([[0., 1., 1., 0., 0., 1., 1., 1., 0., 0.], [0., 1., 1., 1., 1., 0., 0., 0., 1., 0.], [0., 1., 1., 1., 1., 0., 0., 0., 0., 1.]])

tok.get_config()['word_index']

Output: '{"python": 1, "is": 2, "easy": 3, "to": 4, "great": 5, "and": 6, "useful": 7, "learn": 8, "implement": 9}'

mat_texts_seq = tok.texts_to_sequences(txt)
mat_texts_seq

Output:- [[1, 2, 5, 6, 7], [1, 2, 3, 4, 8], [1, 2, 3, 4, 9]]

Related