Luk*_*kas 5 python pipeline cluster-analysis k-means scikit-learn
我想对我的文本数据执行聚类。为了找到最佳的文本预处理参数,我制作了管道并将其放入 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, lemmatize=True, leave_other_words=True):
#some code here
def do_something_to(self, X):
# some code here
return articles
def transform(self, X, y=None):
return self.do_something_to(X) # where the actual feature extraction happens
def fit(self, X, y=None):
return self # generally does nothing
Run Code Online (Sandbox Code Playgroud)
但是如何以及必须对 KMeans 或其他聚类算法做什么?
| 归档时间: |
|
| 查看次数: |
3231 次 |
| 最近记录: |