나는 텍스트가 :Sklearn 파이프 라인 : 텍스트를 클러스터링하는 kmeans를 빌드하는 방법은 무엇입니까? 같이
list1 = ["My name is xyz", "My name is pqr", "I work in abc"]
가 위의 클러스터링 텍스트 kmeans를 사용하여 설정 훈련 될 것이다.
list2 = ["My name is xyz", "I work in abc"]
위의 내용이 내 테스트 세트입니다.
다음과 같이 나는 벡터화 및 모델을 구축:
vectorizer = TfidfVectorizer(min_df = 0, max_df=0.5, stop_words = "english", charset_error = "ignore", ngram_range = (1,3))
vectorized = vectorizer.fit_transform(list1)
km=KMeans(n_clusters=2, init='k-means++', n_init=10, max_iter=1000, tol=0.0001, precompute_distances=True, verbose=0, random_state=None, copy_x=True, n_jobs=1)
km.fit(vectorized)
내가 "리스트 2"내 테스트 세트에 대한 클러스터를 예측하려고하면 :
km.predict(list2)
나는 오류 아래 :
ValueError: Incorrect number of features. Got 2 features, expected 5
나는이 문제를 해결하기 위해 Pipeline
을 사용했다고 들었다. 그래서 아래의 코드 작성 :
pipe = Pipeline([('vect', vectorizer), ('vectorized', vectorized), ('kmeans',km)])
을하지만 오류 얻을 :
TypeError Traceback (most recent call last)
/mnt/folder/Text_Mining/<ipython-input-14-321cabc3bf35> in <module>()
----> 1 pipe = Pipeline([('vect', vectorizer), ('vectorized', vectorized), ('kmeans',km)])
/usr/local/lib/python2.7/dist-packages/scikit_learn-0.13-py2.7-linux-x86_64.egg/sklearn/pipeline.pyc in __init__(self, steps)
87 raise TypeError("All intermediate steps a the chain should "
88 "be transforms and implement fit and transform"
---> 89 "'%s' (type %s) doesn't)" % (t, type(t)))
90
91 if not hasattr(estimator, "fit"):
TypeError: All intermediate steps a the chain should be transforms and implement fit and transform' (0, 2) 1.0
(1, 4) 0.57735026919
(1, 3) 0.57735026919
(1, 1) 0.57735026919
(2, 0) 1.0' (type <class 'scipy.sparse.csr.csr_matrix'>) doesn't)
내가 어쩌면 vectorized
의 출력이 적합을 구현하고 변환하지 않는 생각,하지만 난에있는 것을 어떻게해야합니까를 이 특별한 사건? 나는 Machine Learning에 익숙하지 않습니다. 또한 kmeans 모델에서 레이블을 가져 오는 방법은 무엇입니까? kmeans를 실행할 때 km.labels_
을 사용하여 클러스터 레이블에 액세스 할 수 있습니다. 파이프 라인에서 비슷한 작업을 수행하는 방법? 당신이해야 할 일은
'('벡터화 된', 벡터화 된) 부분은 파이프 라인의 유효한 부분이 아닙니다. 파이프 라인에서는 '적합'한 객체와 '마지막'을 제외한 모든 객체를 원합니다. 여러분은 여러분이했던 것처럼'fitting'을'pipe'에 호출해야합니다 :'pipe.fit (list1)' – eickenberg
도움을 주셔서 감사합니다 : – user1452759