小编Luk*_*kas的帖子

使用 GridSearchCV scikit-learn 进行管道中的 KMeans

我想对我的文本数据执行聚类。为了找到最佳的文本预处理参数,我制作了管道并将其放入 GridSearchCV:

text_clf = Pipeline([('vect1', CountVectorizer(analyzer = "word"),
                   ('myfun', MyLemmanization(lemmatize=True,
                                           leave_other_words = True)),
                   ('vect2', CountVectorizer(analyzer = "word",
                                          max_df=0.95, min_df=2,
                                          max_features=2000)),
                   ('tfidf', TfidfTransformer()),
                   ('clust',   KMeans(n_clusters=10, init='k-means++',
                                      max_iter=100, n_init=1, verbose=1))])
parameters = {'myfun__lemmatize': (True, False),
              'myfun__leave_other_words': (True, False)}
gs_clf = GridSearchCV(text_clf, parameters, n_jobs=1, scoring=score)
gs_clf = gs_clf.fit(text_data)
Run Code Online (Sandbox Code Playgroud)

在哪里 score

score = make_scorer(my_f1, greater_is_better=True)
Run Code Online (Sandbox Code Playgroud)

my_f1具有以下形式:

def my_f1(labels_true, labels_pred):
    # fancy stuff goes here
Run Code Online (Sandbox Code Playgroud)

并且专为聚类设计

所以我的问题是:如何使它起作用?如何通过labels_pred,作为一个自然人,我只能做

gs_clf.fit(data)
Run Code Online (Sandbox Code Playgroud)

而在分类中有可能:

gs_clf.fit(data, labels_true)
Run Code Online (Sandbox Code Playgroud)

我知道我可以编写我的自定义函数,就像我做的那样MyLemmanization

class MyLemmanization(BaseEstimator, TransformerMixin):

    def __init__(self, …
Run Code Online (Sandbox Code Playgroud)

python pipeline cluster-analysis k-means scikit-learn

5
推荐指数
1
解决办法
3231
查看次数