使用scikit Random Forest sample_weights

时间:2014-01-28 22:49:49

标签: python scikit-learn random-forest

我一直在试图找出scikit的Random Forest sample_weight使用,我无法解释我看到的一些结果。从根本上说,我需要它来平衡分类问题和不平衡类。

特别是,我期待如果我使用所有1的sample_weights数组,我会得到与w sample_weights=None相同的结果。另外,我正在考虑任何相等的权重阵列(即所有1,或全10或全0.8 ......)将提供相同的结果。在这种情况下,也许我对权重的直觉是错误的。

以下是代码:

import numpy as np
from sklearn import ensemble,metrics, cross_validation, datasets

#create a synthetic dataset with unbalanced classes
X,y = datasets.make_classification(
n_samples=10000, 
n_features=20, 
n_informative=4, 
n_redundant=2, 
n_repeated=0, 
n_classes=2, 
n_clusters_per_class=2, 
weights=[0.9],
flip_y=0.01,
class_sep=1.0, 
hypercube=True, 
shift=0.0, 
scale=1.0, 
shuffle=True, 
random_state=0)

model = ensemble.RandomForestClassifier()

w0=1 #weight associated to 0's
w1=1 #weight associated to 1's

#I should split train and validation but for the sake of understanding sample_weights I'll skip this step
model.fit(X, y,sample_weight=np.array([w0 if r==0 else w1 for r in y]))    
preds = model.predict(X)
probas = model.predict_proba(X)
ACC = metrics.accuracy_score(y,preds)
precision, recall, thresholds = metrics.precision_recall_curve(y, probas[:, 1])
fpr, tpr, thresholds = metrics.roc_curve(y, probas[:, 1])
ROC = metrics.auc(fpr, tpr)
cm = metrics.confusion_matrix(y,preds)
print "ACCURACY:", ACC
print "ROC:", ROC
print "F1 Score:", metrics.f1_score(y,preds)
print "TP:", cm[1,1], cm[1,1]/(cm.sum()+0.0)
print "FP:", cm[0,1], cm[0,1]/(cm.sum()+0.0)
print "Precision:", cm[1,1]/(cm[1,1]+cm[0,1]*1.1)
print "Recall:", cm[1,1]/(cm[1,1]+cm[1,0]*1.1)
  • w0=w1=1我得到了F1=0.9456
  • w0=w1=10我得到了F1=0.9569
  • sample_weights=None我得到F1=0.9474

1 个答案:

答案 0 :(得分:7)

使用随机森林算法,顾名思义,有一些“随机”的算法。

您获得的F1分数不同,因为随机森林算法(RFA)使用您的数据子集生成决策树,然后对所有树进行平均。因此,我对你的每次跑步都有类似(但不相同)的F1得分并不感到惊讶。

我之前尝试过平衡重量。您可能希望尝试根据总体中每个类的大小来平衡权重。例如,如果您有两个类:

Class A: 5 members
Class B: 2 members

您可能希望通过为每个Class A成员分配2/7和为每个Class B成员分配5/7来平衡权重。不过,这只是一个想法作为起点。你如何对课程进行加权取决于你遇到的问题。