performance by actually building classifier we have the training labels stored in y_train and the bag-of-words representation of the training data in x_trainso we can train classifier on this data for high-dimensionalsparse data like thislinear models like logisticregression often work best let' start by evaluating logisticregresssion using cross-validation: in[ ]from sklearn model_selection import cross_val_score from sklearn linear_model import logisticregression scores cross_val_score(logisticregression()x_trainy_traincv= print("mean cross-validation accuracy{ }format(np mean(scores))out[ ]mean cross-validation accuracy we obtain mean cross-validation score of %which indicates reasonable performance for balanced binary classification task we know that logisticregression has regularization parametercwhich we can tune via cross-validationin[ ]from sklearn model_selection import gridsearchcv param_grid {' '[ ]grid gridsearchcv(logisticregression()param_gridcv= grid fit(x_trainy_trainprint("best cross-validation score{ }format(grid best_score_)print("best parameters"grid best_params_out[ ]best cross-validation score best parameters{' ' we obtain cross-validation score of using = we can now assess the generalization performance of this parameter setting on the test setin[ ]x_test vect transform(text_testprint("{ }format(grid score(x_testy_test))out[ ] the attentive reader might notice that we violate our lesson from on cross-validation with preprocessing here using the default settings of countvectorizerit actually does not collect any statisticsso our results are valid using pipeline from the start would be better choice for applicationsbut we defer it for ease of exposure working with text data