我是python的新手,想为每个折叠和每个多项式绘制一个CV误差。下面的代码计算不同次数多项式和每个折叠的误差值。请在这方面指导我。
from sklearn.cross_validation import KFold
kf = KFold(len(dF), n_folds=5)
e_test = []
orders = [2,3]
dims = [6,10]
for i, order in enumerate(orders):
dF = getDataByDegree(d,order)
error = []
wTemp = np.empty(dims[i])
wTemp.fill(0.001)
for train_index, test_index in kf:
x_train, x_test = dF[train_index], data['l'][train_index]
y_train, y_test = dF[test_index], data['l'][test_index]
w, x_error = gradientDes(wTemp,x_train,x_test)
y_error = errorfun(w,y_train,y_test)
error.insert(i,y_error[0])
e_test.insert(i,error)
fig, ax = plt.subplots()
for i in range(1,len(orders):
ax.plot(orders,values[i],lw=2, label='Test Error - Fold %s' % str(int(i)+1))
plt.show()
您正在寻找sklearn所谓的验证曲线。validation_curve
函数可让您在为您执行 CV 时探索特定模型超参数的范围。
如果要绘制错误,请参阅此示例。