用GridsearchCV寻找桦木的最佳参数

2024-04-25 15:18:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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

但是输出给我一个错误:

enter image description here

我很困惑为什么score值要寻找4个参数,而我已经在sil_score函数中声明了评分所需的参数。在


Tags: 参数labelsfoldbestscorestateprintensemble
1条回答
网友
1楼 · 发布于 2024-04-25 15:18:28

你的记分功能不正确。语法应该是sil_score(y_true,y_pred),其中y嫒true是基本真理标签,y_pred是预测的标签。你也不需要在评分函数中使用集合对象来分别预测标签。另外,在您的例子中,直接使用silhouette_score作为评分函数更有意义,因为您正在调用您的集成来预测评分函数内部的标签,这根本不需要。只需传递silhouette_score作为评分函数,GridSearchCV将自行预测评分。在

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

相关问题 更多 >