from sklearn datasets import fetch_ newsgroups from sklearn naive_bayes import multinomialnb from sklearn feature_extraction text import tfidftransformer from sklearn feature_extraction text import countvectorizer define the category map we are using five different categories named religionautossportselectronics and space category_map {'talk religion misc''religion''rec autos''autos','rec sport hockey':'hockey','sci electronics':'electronics''sci space''space'create the training settraining_data fetch_ newsgroups(subset='train'categories=category_map keys()shuffle=truerandom_state= build count vectorizer and extract the term countsvectorizer_count countvectorizer(train_tc vectorizer_count fit_transform(training_data dataprint("\ndimensions of training data:"train_tc shapethe tf-idf transformer is created as followstfidf tfidftransformer(train_tfidf tfidf fit_transform(train_tcnowdefine the test datainput_data 'discovery was space shuttle''hinduchristiansikh all are religions''we must have to drive safely''puck is disk made of rubber''televisionmicrowaverefrigrated all uses electricitythe above data will help us train multinomial naive bayes classifierclassifier multinomialnb(fit(train_tfidftraining_data target