[英]Decision Tree Of SkLearn: Overfitting or Bug?
我正在使用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的树包的性质有关吗? 还是还有其他问题?
PS:在其他模型(如朴素贝叶斯,knn或ann)上,训练误差绝对不是0.0。
这些建议给出了一些非常有用的指导。 我只想添加您可能想要调整的参数,称为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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.