Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with an unbalanced dataset with scikit-learn Random forest?

I have an unbalanced textual dataset this is how it looks:

label | texts(documents)
----------
5     |1190
4     |839
3     |239
1     |204
2     |127

I tried to use the fit(X, y[, sample_weight]) parameter but I did not understand in the documentation how does this is expected. I tried the following:

from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import balance_weights

classifier=RandomForestClassifier(n_estimators=10,criterion='entropy')
classifier.fit(X_train, y_train,sample_weight = balance_weights(y))
prediction = classifier.predict(X_test)

But I get this exception:

/usr/local/lib/python2.7/site-packages/sklearn/utils/__init__.py:93: DeprecationWarning: Function balance_weights is deprecated; balance_weights is an internal function and will be removed in 0.16
  warnings.warn(msg, category=DeprecationWarning)
Traceback (most recent call last):
  File "/Users/user/RF_classification.py", line 34, in <module>
    classifier.fit(X_train, y_train,sample_weight = balance_weights(y))
  File "/usr/local/lib/python2.7/site-packages/sklearn/ensemble/forest.py", line 279, in fit
    for i in range(n_jobs))
  File "/usr/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 653, in __call__
    self.dispatch(function, args, kwargs)
  File "/usr/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 400, in dispatch
    job = ImmediateApply(func, args, kwargs)
  File "/usr/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 138, in __init__
    self.results = func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/sklearn/ensemble/forest.py", line 85, in _parallel_build_trees
    curr_sample_weight *= sample_counts
ValueError: operands could not be broadcast together with shapes (2599,) (1741,) (2599,) 

How can I balance this estimator for this "unbalanced data"?.

like image 795
tumbleweed Avatar asked Jan 18 '26 16:01

tumbleweed


1 Answers

Update to 0.16-dev. Random Forests now support class_weight="auto", which basically rebalances classes automatically for you.

like image 63
Gilles Louppe Avatar answered Jan 21 '26 06:01

Gilles Louppe