通过validation_curve画出训练图,找到最合适的参数范围和参数
#!/usr/bin/env python2 # -*- coding: utf-8 -*- from sklearn.model_selection import validation_curve from sklearn.datasets import load_digits from sklearn.svm import SVC import matplotlib.pyplot as plt import numpy as np digits = load_digits() X = digits.data y = 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()