Overall, we should avoid shuffling the entire input. I think we can rewrite this training algorithm so it does some map side aggregations first (i.e. group by key on each partition, compute the per-partition naive bayes model, and then combine the per-partition model into a global model).