绘制不同程度多项式的交叉验证误差

时间:2016-02-20 22:53:53

标签: python matplotlib scikit-learn

我是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()   

1 个答案:

答案 0 :(得分:0)

您正在寻找sklearn调用validation curve的内容。 validation_curve功能可让您在为自己做简历时探索特定模型超参数的范围。

如果您想绘制错误,请参阅this example