the only missing thing is to define how gridsearchcv should determine the best estimator this can be done by providing the desired score function to (surprise!the score_func parameter we could either write one ourselves or pick one from the sklearn metrics package we should certainly not take metric accuracy because of our class imbalance (we have lot less tweets containing sentiment than neutral onesinsteadwe want to have good precision and recall on both the classesthe tweets with sentiment and the tweets without positive or negative opinions one metric that combines both precision and recall is the -measure metricwhich is implemented as metrics _scoreputting everything togetherwe get the following codefrom sklearn grid_search import gridsearchcv from sklearn metrics import _score def grid_search_model(clf_factoryxy)cv shufflesplitn=len( )n_iter= test_size= indices=truerandom_ state= param_grid dict(vect__ngram_range=[( )( )( )]vect__min_df=[ ]vect__stop_words=[none"english"]vect__smooth_idf=[falsetrue]vect__use_idf=[falsetrue]vect__sublinear_tf=[falsetrue]vect__binary=[falsetrue]clf__alpha=[ ]grid_search gridsearchcv(clf_factory()param_grid=param_gridcv=cvscore_func= _scoreverbose= grid_search fit(xyreturn grid_search best_estimator_