gpt4 book ai didi

python - RandomForestClassifier 错误 'object of type ' int' 没有 len()'

转载 作者:行者123 更新时间:2023-12-01 09:24:50 24 4
gpt4 key购买 nike

我正在写代码?并遇到了“‘int’类型的对象没有 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()

最佳答案

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

  • 首先,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}].`

关于python - RandomForestClassifier 错误 'object of type ' int' 没有 len()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50515476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com