【问题标题】:GridsearchCV to find the optimum parameter for BIRCHGridsearchCV 为 BIRCH 找到最佳参数
【发布时间】:2017-12-15 06:03:55
【问题描述】:

我正在使用 gridsearchCV 来寻找 BIRCH 的最佳参数,我的代码是:

RAND_STATE=50  # for reproducibility and consistency
folds=3
k_fold = KFold(n_splits=folds, shuffle=True, random_state=RAND_STATE)

hyperparams = { "branching_factor": [50,100,200,300,400,500,600,700,800,900],
                "n_clusters": [5,7,9,11,13,17,21],
                "threshold": [0.2,0.3,0.4,0.5,0.6,0.7]}
birch = Birch()

def sil_score(ndata):
    labels = ensemble.predict(ndata)
    score = silhouette_score(ndata, labels)
    return score

sil_scorer = make_scorer(sil_score)

ensemble = GridSearchCV(estimator=birch,param_grid=hyperparams,scoring=sil_scorer,cv=k_fold,verbose=10,n_jobs=-1)

ensemble.fit(x)
print ensemble
best_parameters = ensemble.best_params_
print best_parameters
best_score = ensemble.best_score_
print best_score

但是输出给了我一个错误:

当我已经在 sil_score 函数中说明了评分所需的参数时,我很困惑为什么分数值正在寻找 4 个参数。

【问题讨论】:

    标签: python validation optimization scikit-learn cluster-analysis


    【解决方案1】:

    您的评分函数不正确。语法应该是sil_score(y_true,y_pred),其中 y_true 是真实标签,y_pred 是预测标签。此外,您无需使用评分函数中的集成对象单独预测标签。同样在您的情况下,直接使用silhouette_score 作为评分函数更有意义,因为您正在调用您的集合来预测评分函数中的标签,这根本不需要。只需将silhouette_score 作为评分函数传递,GridSearchCV 将自行负责预测评分。

    Here is an example如果你想看看它是如何工作的。

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2020-01-28
      • 2021-11-24
      • 2020-10-10
      • 2017-05-19
      • 2018-10-15
      • 2019-09-30
      • 2019-07-24
      • 2019-04-07
      相关资源
      最近更新 更多