10
$\begingroup$

1) How can I change classification threshold (i think it is 0.5 by default) in RandomForest in sklearn?

2) how can I under-sample in sklearn?

3) I have the following result from RandomForest classifier: [[1635 1297] [ 520 3624]]

         precision    recall  f1-score   support

class 0       0.76      0.56      0.64      2932
class 1       0.74      0.87      0.80      4144

avg / total 0.75 0.74 0.73 7076

first, data is unbalanced (30% from class-0 and 70% from class-1). So, I think the classifier is more likely to be biased for class-1 meaning move some from class-0 to class-1 (there are 1297 missclassification for class-0 but 520 missclassification for class-1). How can I fix this? if downsampling can help? or changing classification threshold?

Update: class-0 has 40% of population while class-1 is 60%. However, drift from class-0 to class-1 (1297) is high while I want this becomes low.

$\endgroup$

1 Answer 1

7
$\begingroup$

You could indeed wrap you random forest in a class that a predict methods that calls the predict_proba method of the internal random forest and output class 1 only if it's higher than a custom threshold.

Alternatively you can bias the training algorithm by passing a higher sample_weight for samples from the minority class.

$\endgroup$
4
  • $\begingroup$ Thanks. I was thinking about increasing weight for minor class. However, I can not see anything in RandomForest classifier (there is in SGDclassifier) $\endgroup$ Commented Dec 18, 2013 at 23:01
  • $\begingroup$ The fit method accept a sample_weight param (one weight per parameter) that is very flexible and makes it possible to simulate class_weight (one weight per target class). $\endgroup$
    – ogrisel
    Commented Dec 19, 2013 at 8:48
  • $\begingroup$ Thanks. when I use clf = clf.fit(X, Y, sample_weight=preprocessing.balance_weights(y) it gives me ValueError: operands could not be broadcast together with shapes. y is binary 0/1 $\endgroup$ Commented Dec 19, 2013 at 22:52
  • $\begingroup$ What is the shape of y? Why do you have different Y and y? $\endgroup$
    – ogrisel
    Commented Dec 20, 2013 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.