赞
踩
【pyhon 版本 3.5.0 skit-learn版本<0.18.1>】
昨天发现的问题,RandomizedSearchCV怎么都调不通:
- # Split the dataset in two equal parts
- X_train, X_test, y_train, y_test = train_test_split(
- data,label, test_size=0.25, random_state=0)
-
- # Set the parameters by cross-validation
- tuned_parameters = [{'n_neighbors': range(2,7)},
- {'leaf_size':range(9,100,3)},
- {'p':range(1,5)}]
-
- svr=KNeighborsClassifier()
-
- scores = ['precision', 'recall']
-
- for score in scores:
- print("# Tuning hyper-parameters for %s" % score)
- print()
-
- labels=y_train.values
- aa
- c, r = labels.shape
- labels = labels.reshape(c,)
-
- clf = RandomizedSearchCV(svr, tuned_parameters,cv=5,n_jobs=-1,verbose=3)
- # clf = GridSearchCV(svr, tuned_parameters,cv=5,n_jobs=-1,verbose=3)
- clf.fit(X_train, labels)
报错如下:
- File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
- exec(compile(f.read(), filename, 'exec'), namespace)
-
- File "C:/Users/gzhuangzhongyi/Desktop/NetEase/test/RandomSearchCV_Functional.py", line 46, in <module>
- clf.fit(X_train, labels)
-
- File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 1190, in fit
- return self._fit(X, y, groups, sampled_params)
-
- File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 564, in _fit
- for parameters in parameter_iterable
-
- File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 758, in __call__
- while self.dispatch_one_batch(iterator):
-
- File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 603, in dispatch_one_batch
- tasks = BatchedCalls(itertools.islice(iterator, batch_size))
-
- File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 127, in __init__
- self.items = list(iterator_slice)
-
- File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 557, in <genexpr>
- )(delayed(_fit_and_score)(clone(base_estimator), X, y, self.scorer_,
-
- File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 230, in __iter__
- for v in self.param_distributions.values()])
-
- AttributeError: 'list' object has no attribute 'values'
经过查看fit方法,发现无论如何调整fit方法的参数,都没法运行。
但是如果换成GridSearchCV就可以运行。
经过查看类实现,发现两种类调用了相同的,fit方法,但是,fit方法有隐含传入的参数:
- sampled_params = ParameterSampler(self.param_distributions,
- self.n_iter,
- random_state=self.random_state)
- return self._fit(X, y, groups, sampled_params)
其中,sampled_params为传入参数之采样。
其传入参数在初始化的时候传入:
clf = RandomizedSearchCV(svr, tuned_parameters,cv=5,n_jobs=-1,verbose=3)
而,这个参数由:
- tuned_parameters = [{'n_neighbors': range(2,7)},
- {'leaf_size':range(9,100,3)},
- {'p':range(1,5)}]
语句设定,这里有三个字典。而正确的是:
- tuned_parameters = [{'n_neighbors': range(2,7),
- 'leaf_size':range(9,100,3),
- 'p':range(1,5)}]
Grid的时候会遍历字典中所有参数的组合,所以字典的划分不重要。
- for p in self.param_grid:
- # Always sort the keys of a dictionary, for reproducibility
- items = sorted(p.items())
- if not items:
- yield {}
- else:
- keys, values = zip(*items)
- for v in product(*values):
- params = dict(zip(keys, v))
- yield params
但是Randomlize,当传入字典的时候,会作为带分布的进行处理,对字典取值
- # Always sort the keys of a dictionary, for reproducibility
- items = sorted(self.param_distributions.items())
- for _ in six.moves.range(self.n_iter):
- params = dict()
- for k, v in items:
- if hasattr(v, "rvs"):
- if sp_version < (0, 16):
- params[k] = v.rvs()
- else:
- params[k] = v.rvs(random_state=rnd)
- else:
- params[k] = v[rnd.randint(len(v))]
- yield params
Random会检查传入的参数,如果可以遍历就认为是分布。
于是传入作为fit的参数集的时候,不是作为可遍历的对象的字典,可以.values,而是一个一个把分布元素组合成字典的list,但因为传入的不是一个分布而是一个list,所以不能对分布取值。
上面的两段函数GridSearchCV产生的参数集:
RandomizeSearchCV产生的参数集因为debug调不出来,无法展示。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。