RandomForestClassifier错误'类型'int'的对象没有len()'

时间:2018-05-24 17:58:25

标签: python scikit-learn random-forest

我正在编写代码?并且与''对象类型'争吵'没有len()''

这是代码片段

parameters = {'max_depth':range(3, 11, 2), 'class_weight':range(3, 11, 2), 'min_impurity_decrease':range(3, 11, 2), 'max_features':range(3, 11, 2)}
knc = RandomForestClassifier()
clf = GridSearchCV(knc, parameters)
clf.fit(X_train, y_train)
clf.best_estimator_

这是跟踪板

TypeError                                 Traceback (most recent call last)
<ipython-input-71-3f7bf4b64f84> in <module>()
      4 knc = RandomForestClassifier()
      5 clf = GridSearchCV(knc, parameters)
----> 6 clf.fit(X_train, y_train)
      7 clf.best_estimator_

~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params)
    637                                   error_score=self.error_score)
    638           for parameters, (train, test) in product(candidate_params,
--> 639                                                    cv.split(X, y, groups)))
    640 
    641         # if one choose to see train score, "out" will contain train score info

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in __call__(self, iterable)
    777             # was dispatched. In particular this covers the edge
    778             # case of Parallel used with an exhausted iterator.
--> 779             while self.dispatch_one_batch(iterator):
    780                 self._iterating = True
    781             else:

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in dispatch_one_batch(self, iterator)
    623                 return False
    624             else:
--> 625                 self._dispatch(tasks)
    626                 return True
    627 

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in _dispatch(self, batch)
    586         dispatch_timestamp = time.time()
    587         cb = BatchCompletionCallBack(dispatch_timestamp, len(batch), self)
--> 588         job = self._backend.apply_async(batch, callback=cb)
    589         self._jobs.append(job)
    590 

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py in apply_async(self, func, callback)
    109     def apply_async(self, func, callback=None):
    110         """Schedule a func to be run"""
--> 111         result = ImmediateResult(func)
    112         if callback:
    113             callback(result)

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py in __init__(self, batch)
    330         # Don't delay the application, to avoid keeping the input
    331         # arguments in memory
--> 332         self.results = batch()
    333 
    334     def get(self):

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in __call__(self)
    129 
    130     def __call__(self):
--> 131         return [func(*args, **kwargs) for func, args, kwargs in self.items]
    132 
    133     def __len__(self):

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in <listcomp>(.0)
    129 
    130     def __call__(self):
--> 131         return [func(*args, **kwargs) for func, args, kwargs in self.items]
    132 
    133     def __len__(self):

~\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, return_n_test_samples, return_times, error_score)
    456             estimator.fit(X_train, **fit_params)
    457         else:
--> 458             estimator.fit(X_train, y_train, **fit_params)
    459 
    460     except Exception as e:

~\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py in fit(self, X, y, sample_weight)
    271         self.n_outputs_ = y.shape[1]
    272 
--> 273         y, expanded_class_weight = self._validate_y_class_weight(y)
    274 
    275         if getattr(y, "dtype", None) != DOUBLE or not y.flags.contiguous:

~\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py in _validate_y_class_weight(self, y)
    512                     class_weight = self.class_weight
    513                 expanded_class_weight = compute_sample_weight(class_weight,
--> 514                                                               y_original)
    515 
    516         return y, expanded_class_weight

~\Anaconda3\lib\site-packages\sklearn\utils\class_weight.py in compute_sample_weight(class_weight, y, indices)
    162             weight_k = compute_class_weight(class_weight_k,
    163                                             classes_full,
--> 164                                             y_full)
    165 
    166         weight_k = weight_k[np.searchsorted(classes_full, y_full)]

~\Anaconda3\lib\site-packages\sklearn\utils\class_weight.py in compute_class_weight(class_weight, classes, y)
     42         raise ValueError("classes should include all valid labels that can "
     43                          "be in y")
---> 44     if class_weight is None or len(class_weight) == 0:
     45         # uniform class weights
     46         weight = np.ones(classes.shape[0], dtype=np.float64, order='C')

TypeError: object of type 'int' has no len()

1 个答案:

答案 0 :(得分:1)

我们需要更改代码中的一些内容:

  • 首先,min_impurity_decrease应替换为min_impurity_splitRandomForestClassifier没有min_impurity_decrease
  • 其次,代码中的问题是您定义class_weight
  • 的方式
  • 您不需要功能listrange适用于除class_weight
  • 之外的所有参数

适用于虹膜数据的示例:

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV

x = load_iris().data
y = load_iris().target

parameters = {'max_depth': range(3, 11, 2), 'class_weight':[{0: 1, 1: 1}, {0: 1, 1: 5}, {0: 1, 1: 1}], 'min_impurity_split':range(3, 11, 2), 'max_features':range(3, 4)}
knc = RandomForestClassifier()
clf = GridSearchCV(knc, parameters)

clf.fit(x,y)
clf.best_estimator_

来自documentation

class_weight : dict, list of dicts, “balanced”,

Note that for multioutput (including multilabel) weights should be defined for 
each class of every column in its own dict. For example, for four-class 
multilabel classification weights should be [{0: 1, 1: 1}, {0: 1, 1: 5}, {0: 
1, 1: 1}, {0: 1, 1: 1}] instead of [{1:1}, {2:5}, {3:1}, {4:1}].`