from sklearn feature_extraction text import tfidfvectorizer from sklearn pipeline import make_pipeline pipe make_pipeline(tfidfvectorizer(min_df= norm=none)logisticregression()param_grid {'logisticregression__c'[ ]grid gridsearchcv(pipeparam_gridcv= grid fit(text_trainy_trainprint("best cross-validation score{ }format(grid best_score_)out[ ]best cross-validation score as you can seethere is some improvement when using tf-idf instead of just word counts we can also inspect which words tf-idf found most important keep in mind that the tf-idf scaling is meant to find words that distinguish documentsbut it is purely unsupervised technique so"importanthere does not necessarily relate to the "positive reviewand "negative reviewlabels we are interested in firstwe extract the tfidfvectorizer from the pipelinein[ ]vectorizer grid best_estimator_ named_steps["tfidfvectorizer"transform the training dataset x_train vectorizer transform(text_trainfind maximum value for each of the features over the dataset max_value x_train max(axis= toarray(ravel(sorted_by_tfidf max_value argsort(get feature names feature_names np array(vectorizer get_feature_names()print("features with lowest tfidf:\ {}formatfeature_names[sorted_by_tfidf[: ]])print("features with highest tfidf\ {}formatfeature_names[sorted_by_tfidf[- :]])out[ ]features with lowest tfidf['poignant'disagree'instantly'importantly'lacked'occurred'currently'altogether'nearby'undoubtedly'directs'fond'stinker'avoided'emphasis'commented'disappoint'realizing'downhill'inane'features with highest tfidf['coop'homer'dillinger'hackenstein'gadget'taker'macarthur'vargas'jesse'basket'dominick'the'victor'bridget'victoria'khouri'zizek'rob'timon'titanic'rescaling the data with tf-idf