【发布时间】:2016-03-01 03:47:12
【问题描述】:
我正在使用带有分层 CV 的 scikit-learn 来比较一些分类器。 我正在计算:准确度、召回率、auc。
我用 GridSearchCV 进行参数优化,CV 为 5。
RandomForestClassifier(warm_start= True, min_samples_leaf= 1, n_estimators= 800, min_samples_split= 5,max_features= 'log2', max_depth= 400, class_weight=None)
是来自 GridSearchCV 的 best_params。
我的问题,我觉得我真的过拟合了。例如:
具有标准偏差 (+/-) 的随机森林
- 精度:0.99 (+/- 0.06)
- 灵敏度:0.94 (+/- 0.06)
- 特异性:0.94 (+/- 0.06)
- B_accuracy:0.94 (+/- 0.06)
- AUC:0.94 (+/- 0.11)
带标准差 (+/-) 的逻辑回归
- 精度:0.88(+/- 0.06)
- 灵敏度:0.79 (+/- 0.06)
- 特异性:0.68 (+/- 0.06)
- B_accuracy:0.73 (+/- 0.06)
- AUC:0.73 (+/- 0.041)
其他看起来也像逻辑回归(所以它们看起来并没有过度拟合)。
我的简历代码是:
for i,j in enumerate(data):
X.append(data[i][0])
y.append(float(data[i][1]))
x=np.array(X)
y=np.array(y)
def SD(values):
mean=sum(values)/len(values)
a=[]
for i in range(len(values)):
a.append((values[i]-mean)**2)
erg=sum(a)/len(values)
SD=math.sqrt(erg)
return SD,mean
for name, clf in zip(titles,classifiers):
# go through all classifiers, compute 10 folds
# the next for loop should be 1 tab indent more, coudlnt realy format it here, sorry
pre,sen,spe,ba,area=[],[],[],[],[]
for train_index, test_index in skf:
#print train_index, test_index
#get the index from all train_index and test_index
#change them to list due to some errors
train=train_index.tolist()
test=test_index.tolist()
X_train=[]
X_test=[]
y_train=[]
y_test=[]
for i in train:
X_train.append(x[i])
for i in test:
X_test.append(x[i])
for i in train:
y_train.append(y[i])
for i in test:
y_test.append(y[i])
#clf=clf.fit(X_train,y_train)
#predicted=clf.predict_proba(X_test)
#... other code, calculating metrics and so on...
print name
print("precision: %0.2f \t(+/- %0.2f)" % (SD(pre)[1], SD(pre)[0]))
print("sensitivity: %0.2f \t(+/- %0.2f)" % (SD(sen)[1], SD(pre)[0]))
print("specificity: %0.2f \t(+/- %0.2f)" % (SD(spe)[1], SD(pre)[0]))
print("B_accuracy: %0.2f \t(+/- %0.2f)" % (SD(ba)[1], SD(pre)[0]))
print("AUC: %0.2f \t(+/- %0.2f)" % (SD(area)[1], SD(area)[0]))
print "\n"
如果我使用scores = cross_validation.cross_val_score(clf, X, y, cv=10, scoring='accuracy') 方法,我不会得到这个“过度拟合”值。所以也许我正在使用的 CV 方法有问题?但它只适用于射频......
由于 cross_val_function 中特异性评分函数的滞后,我自己做了。
【问题讨论】:
-
问题是……?
-
您不交叉验证随机森林。相反,在构建森林时会对其进行交叉验证。
-
但是我应该如何比较模型呢?我知道这是经过交叉验证的,但不知何故我应该将它与其他方法(如 SVM、逻辑回归等)进行比较。我的问题也是:为什么我会过度拟合?带有“for train_index, test_index in skf:”的部分是否整个循环可能是错误的?因为正如我提到的, scorer_val 函数给出“正常”值
-
@TimBiegeleisen 您指的是 OoB 错误吗?因为“在构建过程中进行交叉验证”意味着与实际发生的情况不同,不应将其视为使用 RF 时无需单独训练/测试的声明。
-
为什么你认为随机森林过拟合?过度拟合是指您在训练数据上表现良好(随机森林几乎总是如此)但在测试数据上表现不佳。似乎随机森林的表现优于逻辑回归,如果您有高维度,这是可以预期的高度非线性解的问题。 en.wikipedia.org/wiki/Overfitting
标签: python machine-learning scikit-learn random-forest