Issue
I am doing binary classifier. Since my data is unbalanced i am using class weight. I am getting error while passing values how to fix this.
Error: ValueError: class_weight must be dict, 'balanced', or None, got: [{0: 0.4, 1: 0.6}]"
Code
rf=RandomForestClassifier(n_estimators=1000,oob_score=True,min_samples_leaf=500,class_weight=[{0:.4, 1:.6}])
fit_rf=rf.fit(X_train_res,y_train_res)
Error
\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\class_weight.py in compute_class_weight(class_weight, classes, y)
60 if not isinstance(class_weight, dict):
61 raise ValueError("class_weight must be dict, 'balanced', or None,"
---> 62 " got: %r" % class_weight)
63 for c in class_weight:
64 i = np.searchsorted(classes, c)
ValueError: class_weight must be dict, 'balanced', or None, got: [{0: 0.4, 1: 0.6}]
How to fix this.
Solution
Per the documentation
class_weight : dict, list of dicts, “balanced”,
Therefore, the class_weight paramter accepts a dictionary, a list of dictionary, or the string "balanced". The error message you are given states that it wants a dictionary, and since you have only one dictionary a list is not needed.
So, let's try:
rf=RandomForestClassifier(n_estimators=1000,
oob_score=True,
min_samples_leaf=500,
class_weight={0:.4, 1:.6})
fit_rf=rf.fit(X_train_res,y_train_res)
Answered By - Scott Boston
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.