文章作者:Tyan
博客:noahsnail.com ?|? CSDN ?|? 簡(jiǎn)書
本文主要介紹scikit-learn中的交叉驗(yàn)證。
- 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
# 選取合適的參數(shù)gamma
# 加載數(shù)據(jù)集
digits = load_digits()
X = digits.data
y = digits.target
# 定義gamma參數(shù)
param_range = np.logspace(-6, -2.3, 5)
# 用SVM進(jìn)行學(xué)習(xí)并記錄loss
train_loss, test_loss = validation_curve(SVC(), X, y, param_name = 'gamma', param_range = param_range,
cv = 10, scoring = 'mean_squared_error')
# 訓(xùn)練誤差均值
train_loss_mean = -np.mean(train_loss, axis = 1)
# 測(cè)試誤差均值
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()
- 結(jié)果