Try running the below method which uses a cross validation estrategastrategy to evaluate the models' performance across different metrics.
# Authors: Julio J. Luna Moreno
# License: BSD 3 clause
# Contact: [email protected]
from functools import reduce
def _get_model_name(model):
"""
Returns a string with the name of a sklearn model
model: Sklearn stimator class
"""
if isinstance(model, Pipeline):
estimator = model.steps[-1][1]
name = "Pipeline_" + str(estimator)[:str(estimator).find("(")]
else:
name = str(model)[:str(model).find("(")]
return name
def plot_cv_score(X, y, models_list, cv = 5, scoring_list = None, refit = True, return_scores = False):
"""
X: numpy_array/pandas dataframe n_rows, m_features
y: numpy_array/pandas dataframe n_rows
Plots min, max and avg kfold crosval_score for a list of models
"""
names, mean_score = list(), list()
ldf = list()
mnames = list()
for i, model in enumerate(models_list):
name = _get_model_name(model)
if refit:
model.fit(X, y)
for metric in score_list:
score = cross_val_score(model, X, y, cv = cv, scoring = metric, n_jobs= -1)
mean_score.append(np.mean(score))
tmp = pd.DataFrame({name: mean_score}, index = score_list)
ldf.append(tmp)
mean_score = list()
frame_scores = reduce(lambda x,y: pd.merge(x,y, left_index = True, right_index = True), ldf).T
fig, ax = plt.subplots(1,1, figsize = (10,5))
frame_scores.plot.bar(ax = ax, cmap = 'RdYlBu', edgecolor = "black")
ax.legend(loc = 'best')
ax.set_xlabel("Score")
ax.set_title("Cross validation model benchmark")
if return_scores:
return frame_scores