[matplotlib] 绘制Cross-Validation的误差图

概述:

在调整模型参数的时候,往往会进行交叉验证(Cross-Validation)。绘制交叉验证的误差图。

数据:

k是需要调整的参数, 从k_choices中选取

k_choices = [1, 3, 5, 8, 10, 12, 15, 20, 50, 100]

假设经过验证以后k_to_accuracies字典里保存了k取不同值时多次验证的准确性:

k_to_accuracies = {
    1: [0.24, 0.23, 0.24, 0.25, 0.29],
    3: [0.17, 0.23, 0.32, 0.22, 0.23],
    5: [0.12, 0.21, 0.27, 0.19, 0.18],
    8: [0.13, 0.23, 0.26, 0.16, 0.2],
    10: [0.16, 0.18, 0.24, 0.16, 0.19],
    12: [0.17, 0.19, 0.24, 0.2, 0.26],
    15: [0.17, 0.23, 0.19, 0.12, 0.14], 
    20: [0.12, 0.17, 0.19, 0.12, 0.2],
    50: [0.2, 0.16, 0.17, 0.16, 0.14], 
    100: [0.16, 0.15, 0.19, 0.19, 0.19],
}

绘图

绘图的代码如下:

for k in k_choices:
  accuracies = k_to_accuracies[k]
  plt.scatter([k] * len(accuracies), accuracies)

# plot the trend line with error bars that correspond to standard deviation
accuracies_mean = np.array([np.mean(v) for k,v in sorted(k_to_accuracies.items())])
accuracies_std = np.array([np.std(v) for k,v in sorted(k_to_accuracies.items())])
plt.errorbar(k_choices, accuracies_mean, yerr=accuracies_std)
plt.title('Cross-validation on k')
plt.xlabel('k')
plt.ylabel('Cross-validation accuracy')
plt.show()

这里写图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值