SkLearn决策树:过度拟合还是Bug?

2024-06-07 06:19:31 发布

您现在位置:Python中文网/ 问答频道 /正文

利用sklearn的树包分析了决策树模型的训练误差和验证误差。在

#compute the rms error
def compute_error(x, y, model):
 yfit = model.predict(x.toarray())
 return np.mean(y != yfit) 

def drawLearningCurve(model,xTrain, yTrain, xTest, yTest):
 sizes = np.linspace(2, 25000, 50).astype(int)
 train_error = np.zeros(sizes.shape)
 crossval_error = np.zeros(sizes.shape)

 for i,size in enumerate(sizes):

  model = model.fit(xTrain[:size,:].toarray(),yTrain[:size])

  #compute the validation error
  crossval_error[i] = compute_error(xTest,yTest,model)

  #compute the training error
  train_error[i] = compute_error(xTrain[:size,:],yTrain[:size],model)

from sklearn import tree
clf = tree.DecisionTreeClassifier()
drawLearningCurve(clf, xtr, ytr, xte, yte)

问题是(我不知道这是否是个问题),如果我给函数drawLearningCurve提供一个决策树作为模型,那么在每个循环中,我都会收到训练错误的结果0.0。它与我的数据集的性质有关,还是与sklearn的树包的性质有关?还是有什么不对劲?在

注:在其他模型中,如naivebayes、knn或ann,训练误差绝对不是0.0。在


Tags: the模型决策树sizemodelnperrorsklearn
1条回答
网友
1楼 · 发布于 2024-06-07 06:19:31

这些赞扬给出了一些非常有用的指示。我只想添加一个您可能需要调整的参数,名为max_depth。在

更让我担心的是你的compute_error函数很奇怪。你得到一个0的错误说明你的分类器在训练集上没有错误。但是,如果它确实犯了错误,那么错误函数不会告诉你。在

import numpy as np
np.mean([0,0,0,0] != [0,0,0,0]) # perfect match, error is 0
0.0

np.mean([0,0,0,0] != [1, 1, 1, 1]) # 100% wrong answers
1.0

np.mean([0,0,0,0] != [1, 1, 1, 0]) # 75% wrong answers
1.0

np.mean([0,0,0,0] != [1, 1, 0, 0]) # 50% wrong answers
1.0

np.mean([0,0,0,0] != [1, 1, 2, 2]) # 50% wrong answers
1.0

您需要的是np.sum(y != yfit),或者更好的是sklearn附带的一个错误函数,例如accuracy_score。在

相关问题 更多 >