scikit의 LOGO (하나의 그룹 제외)를 교차 검증 방법으로 학습 곡선과 함께 사용하는 것을 좋아합니다. 이것은 내가 다루는 대부분의 경우에 실제로 잘 작동하지만, 나는 (경험적으로) 그러한 경우에 가장 중요하다고 생각되는 두 가지 매개 변수, 즉 최대 특징과 추정 자 수를 (효율적으로) 사용할 수있다. 아래에있는 내 코드의 예 :Scikit-learn에서 RandomizedSearchCV (또는 GridSearcCV)와 LeaveOneGroupOut 교차 유효성 확인
Fscorer = make_scorer(f1_score, average = 'micro')
gp = training_data["GP"].values
logo = LeaveOneGroupOut()
from sklearn.ensemble import RandomForestClassifier
RF_clf100 = RandomForestClassifier (n_estimators=100, n_jobs=-1, random_state = 49)
RF_clf200 = RandomForestClassifier (n_estimators=200, n_jobs=-1, random_state = 49)
RF_clf300 = RandomForestClassifier (n_estimators=300, n_jobs=-1, random_state = 49)
RF_clf400 = RandomForestClassifier (n_estimators=400, n_jobs=-1, random_state = 49)
RF_clf500 = RandomForestClassifier (n_estimators=500, n_jobs=-1, random_state = 49)
RF_clf600 = RandomForestClassifier (n_estimators=600, n_jobs=-1, random_state = 49)
param_name = "max_features"
param_range = param_range = [5, 10, 15, 20, 25, 30]
plt.figure()
plt.suptitle('n_estimators = 100', fontsize=14, fontweight='bold')
_, test_scores = validation_curve(RF_clf100, X, y, cv=logo.split(X, y, groups=gp),
param_name=param_name, param_range=param_range,
scoring=Fscorer, n_jobs=-1)
test_scores_mean = np.mean(test_scores, axis=1)
plt.plot(param_range, test_scores_mean)
plt.xlabel(param_name)
plt.xlim(min(param_range), max(param_range))
plt.ylabel("F1")
plt.ylim(0.47, 0.57)
plt.legend(loc="best")
plt.show()
plt.figure()
plt.suptitle('n_estimators = 200', fontsize=14, fontweight='bold')
_, test_scores = validation_curve(RF_clf200, X, y, cv=logo.split(X, y, groups=gp),
param_name=param_name, param_range=param_range,
scoring=Fscorer, n_jobs=-1)
test_scores_mean = np.mean(test_scores, axis=1)
plt.plot(param_range, test_scores_mean)
plt.xlabel(param_name)
plt.xlim(min(param_range), max(param_range))
plt.ylabel("F1")
plt.ylim(0.47, 0.57)
plt.legend(loc="best")
plt.show()
...
...
내가 것 정말 더 철저한 매개 변수 공간 검색, 그리드 검색 또는 무작위 검색과 로고를 결합하는 것입니다 불구하고있다. 현재로
지금 내 코드는 다음과 같습니다
param_dist = {"n_estimators": [100, 200, 300, 400, 500, 600],
"max_features": sp_randint(5, 30),
"max_depth": sp_randint(2, 18),
"criterion": ['entropy', 'gini'],
"min_samples_leaf": sp_randint(2, 17)}
clf = RandomForestClassifier(random_state = 49)
n_iter_search = 45
random_search = RandomizedSearchCV(clf, param_distributions=param_dist,
n_iter=n_iter_search,
scoring=Fscorer, cv=8,
n_jobs=-1)
random_search.fit(X, y)
은 내가이 오류 메시지가 얻을, cv=logo.split(X, y, groups=gp)
로 cv = 8
를 교체 할 때 님으로
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-0092e11ffbf4> in <module>()
---> 35 random_search.fit(X, y)
/Applications/anaconda/lib/python2.7/site-packages/sklearn/model_selection/_search.pyc in fit(self, X, y, groups)
1183 self.n_iter,
1184 random_state=self.random_state)
-> 1185 return self._fit(X, y, groups, sampled_params)
/Applications/anaconda/lib/python2.7/site-packages/sklearn/model_selection/_search.pyc in _fit(self, X, y, groups, parameter_iterable)
540
541 X, y, groups = indexable(X, y, groups)
--> 542 n_splits = cv.get_n_splits(X, y, groups)
543 if self.verbose > 0 and isinstance(parameter_iterable, Sized):
544 n_candidates = len(parameter_iterable)
/Applications/anaconda/lib/python2.7/site-packages/sklearn/model_selection/_split.pyc in get_n_splits(self, X, y, groups)
1489 Returns the number of splitting iterations in the cross-validator.
1490 """
-> 1491 return len(self.cv) # Both iterables and old-cv objects support len
1492
1493 def split(self, X=None, y=None, groups=None):
TypeError: object of type 'generator' has no len()
어떤 제안 (1) 무슨 일 그리고 더 중요한 것은, (2) 내가 어떻게 작동시킬 수 있는지 (RandomizedSearchCV와 LeaveOneGroupOut을 결합 함)?
* UPDATE 2017년 2월 8일 *
그것은 당신은 RandomizedSearchCV에 logo.split()
를 통과하지해야 random_search.fit(X, y, wells)
잘 모르겠습니다. 'cv.get_n_splits'를 어디에 전달합니까? – MyCarta
@MyCarta 죄송합니다. 저는'cv.get_n_splits'가 아니라'logo.split()'에 대해서 이야기하고있었습니다. 혼란을 없애기 위해 내 대답을 편집했습니다. –
@ Vivek Kumar 괜찮 았어. 해결 방법이 없다는 말입니까? – MyCarta