当前位置: 代码迷 >> 综合 >> scikit-learn的基本用法(七)——交叉验证3
  详细解决方案

scikit-learn的基本用法(七)——交叉验证3

热度:79   发布时间:2023-12-12 21:43:07.0

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

本文主要介绍scikit-learn中的交叉验证。

  • Demo
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.cross_validation import train_test_split
from sklearn.svm import SVC
from sklearn.learning_curve import validation_curve
from sklearn.model_selection import cross_val_score # 选取合适的参数gamma
# 加载数据集
digits = load_digits()
X = digits.data
y = digits.target# 定义gamma参数
param_range = np.logspace(-6, -2.3, 5)# 用SVM进行学习并记录loss
train_loss, test_loss = validation_curve(SVC(), X, y, param_name = 'gamma', param_range = param_range, cv = 10, scoring = 'mean_squared_error')# 训练误差均值
train_loss_mean = -np.mean(train_loss, axis = 1)
# 测试误差均值
test_loss_mean = -np.mean(test_loss, axis = 1)# 绘制误差曲线
plt.plot(param_range, train_loss_mean, 'o-', color = 'r', label = 'Training')
plt.plot(param_range, test_loss_mean, 'o-', color = 'g', label = 'Cross-Validation')plt.xlabel('gamma')
plt.ylabel('Loss')
plt.legend(loc = 'best')
plt.show()
  • 结果

image

参考资料

  1. https://www.youtube.com/user/MorvanZhou
  相关解决方案