【问题标题】:Using predict on new text with kmeans (sklearn)?使用kmeans(sklearn)预测新文本?
【发布时间】:2017-08-07 03:07:28
【问题描述】:

我有一个 非常小短字符串 列表,我想要 (1) 进行聚类并 (2) 使用该模型来预测新字符串属于哪个聚类.

运行第一部分工作正常,获得新字符串的预测则不行。

第一部分

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import KMeans

# List of 
documents_lst = ['a small, narrow river',
                'a continuous flow of liquid, air, or gas',
                'a continuous flow of data or instructions, typically one having a constant or predictable rate.',
                'a group in which schoolchildren of the same age and ability are taught',
                '(of liquid, air, gas, etc.) run or flow in a continuous current in a specified direction',
                'transmit or receive (data, especially video and audio material) over the Internet as a steady, continuous flow.',
                'put (schoolchildren) in groups of the same age and ability to be taught together',
                'a natural body of running water flowing on or under the earth']


# 1. Vectorize the text
tfidf_vectorizer  = TfidfVectorizer(stop_words='english')
tfidf_matrix = tfidf_vectorizer.fit_transform(documents_lst)
print('tfidf_matrix.shape: ', tfidf_matrix.shape)

# 2. Get the number of clusters to make .. (find a better way than random)
num_clusters = 3

# 3. Cluster the defintions
km = KMeans(n_clusters=num_clusters, init='k-means++').fit(tfidf_matrix)

clusters = km.labels_.tolist()

print(clusters)

返回:

tfidf_matrix.shape:  (8, 39)
[0, 1, 0, 2, 1, 0, 2, 0]

第二部分

失败的部分:

predict_doc = ['A stream is a body of water with a current, confined within a bed and banks.']

tfidf_vectorizer  = TfidfVectorizer(stop_words='english')
tfidf_matrix = tfidf_vectorizer.fit_transform(predict_doc)
print('tfidf_matrix.shape: ', tfidf_matrix.shape)

km.predict(tfidf_matrix)

错误:

ValueError: Incorrect number of features. Got 7 features, expected 39

FWIW:我有点理解,训练和预测在矢量化后具有不同数量的特征......

我对任何解决方案持开放态度,包括从 kmeans 更改为更适合短文本聚类的算法。

提前致谢

【问题讨论】:

  • 您正在为预测任务训练一个新的 tfidfvectorizer。因此它会生成一组不同的特征。您应该使用原始的 tfidf_vectorizer
  • @VivekKumar 当然:)。呃。这就说得通了。 [stackoverflow.com/a/26943563/6041010]

标签: python-3.x scikit-learn nlp k-means


【解决方案1】:

为了完整起见,我将用 here 的答案来回答我自己的问题,这并不能回答这个问题。但回答我的

from sklearn.cluster import KMeans

list1 = ["My name is xyz", "My name is pqr", "I work in abc"]
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))
vec = vectorizer.fit(list1)   # train vec using list1
vectorized = vec.transform(list1)   # transform list1 using vec

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, n_jobs=1)

km.fit(vectorized)
list2Vec = vec.transform(list2)  # transform list2 using vec
km.predict(list2Vec)

感谢@IrshadBhat

【讨论】:

    猜你喜欢
    • 2020-08-27
    • 2020-09-24
    • 2018-10-15
    • 2013-07-20
    • 2017-05-25
    • 2016-01-19
    • 2015-01-10
    • 2019-05-02
    • 2016-09-09
    相关资源
    最近更新 更多