通过validation_curve画出训练图,找到最合适的参数范围和参数
#!/usr/bin/env python2# -*- coding: utf-8 -*-from sklearn.model_selection import validation_curvefrom sklearn.datasets import load_digitsfrom sklearn.svm import SVCimport matplotlib.pyplot as pltimport numpy as npdigits = load_digits()X = digits.datay = digits.target#建立参数测试集param_range = np.logspace(-6, -2.3, 5)#使用validation_curve找出模型对参数的影响,# param_name是要评估的参数,param_range是选用的参数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="b", label="Cross-validation")plt.xlabel("gamma")plt.ylabel("Loss")plt.legend(loc="best")plt.show()