Calculating N_spams and N_Hams alternate solution

Calculating Constants


spams = training_set_clean[training_set_clean["Label"] == "spam"]



import functools
import operator
spam_flattened=functools.reduce(operator.iconcat, spams["SMS"].str.split().tolist(), [])
total_words_spam=len(spam_flattened)

hams = training_set_clean[training_set_clean["Label"] == "ham"]


hams_flattened = functools.reduce(operator.iconcat, hams["SMS"].str.split().tolist(), [])


total_words_hams = len(hams_flattened)

n_vocabulary  = len(vocabulary)

I used an alternate method using functools and operator.

What I basically did was collected all lists in 1 list and flattened it and calculated its length. Though it is a confusing and inefficient way . Using apply function is elegant .

example
[[1,2],[1,2,3,4]] converted to [1,2,1,2,3,4] and than calculated length .
2 Likes