快速比较多种机器学习模型实例( 二 )


接下来 , models保存在元组列表中 , 其中包含要测试的每个分类器的名称和类 。 在此之后 , 我们循环遍历这个列表并运行5-fold交叉验证 。 每次运行的结果都记录在我们附加到dfs列表的pandas dataframe中 。 必须注意 , 这里指标是两个类的加权平均指标 。
测试集上的分类报告如下:
快速比较多种机器学习模型实例
本文插图
评估结果
我们将分析从run_exps()脚本返回的final(dataframe)中的数据 。
为了更好地估计每个模型的指标分布 , 我在30个样本上运行了empirical bootstrapping 。 此外 , 我将关注两个指标:性能指标和拟合时间指标 。 下面的Python代码块实现了这一点 。
bootstraps = [] for model in list(set(final.model.values)): model_df = final.loc[final.model == model] bootstrap = model_df.sample(n=30, replace=True) bootstraps.append(bootstrap) bootstrap_df = pd.concat(bootstraps, ignore_index=True) results_long = pd.melt(bootstrap_df,id_vars=['model'],var_name='metrics', value_name='values') time_metrics = ['fit_time','score_time'] # fit time metrics ## PERFORMANCE METRICS results_long_nofit = results_long.loc[~results_long['metrics'].isin(time_metrics)] # get df without fit data results_long_nofit = results_long_nofit.sort_values(by='values') ## TIME METRICS results_long_fit = results_long.loc[results_long['metrics'].isin(time_metrics)] # df with fit data results_long_fit = results_long_fit.sort_values(by='values')首先 , 让我们绘制来自5-fold交叉验证的性能指标 。
import matplotlib.pyplot as plt import seaborn as sns plt.figure(figsize=(20, 12)) sns.set(font_scale=2.5) g = sns.boxplot(x=''model'', y=''values'', hue=''metrics'', data=http://news.hoteastday.com/a/results_long_nofit, palette=''Set3'') plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plt.title('Comparison of Model by Classification Metric') #plt.savefig('./benchmark_models_performance.png',dpi=300) plt.show()
快速比较多种机器学习模型实例
本文插图
很明显 , 支持向量机在所有指标上对我们的数据的拟合度都很差 , 而集成决策树模型(Random Forest和XGBoost)对数据的拟合非常好 。
训练时间怎么样呢?
plt.figure(figsize=(20, 12)) sns.set(font_scale=2.5) g = sns.boxplot(x=''model'', y=''values'', hue=''metrics'', data=http://news.hoteastday.com/a/results_long_fit, palette=''Set3'') plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plt.title('Comparison of Model by Fit and Score Time') plt.show()
快速比较多种机器学习模型实例
本文插图
随机森林虽然相对于KNN、GNB和LogReg来说比较慢 , 但其性能仅次于KNN 。 如果我继续细化模型 , 我可能会将大部分精力集中在随机森林上 , 因为它的性能几乎与XGBoost相同(它们的95%置信区间可能重叠) , 但训练速度几乎快了4倍!
如果您希望对这些模型进行更多的分析(例如 , 计算每个度量标准的置信区间) , 您将需要访问每个度量标准的均值和标准差 。
metrics = list(set(results_long_nofit.metrics.values)) bootstrap_df.groupby(['model'])[metrics].agg([np.std, np.mean])
快速比较多种机器学习模型实例


推荐阅读