【问题标题】:simple k-means clustering for bag of words model using python使用python的词袋模型的简单k-means聚类
【发布时间】:2017-12-06 19:01:03
【问题描述】:

输入数据集如下所示:

{"666": ["abc",
         "xyz"],
 "888": ["xxxo",
         "xxxo"], 
 "007": ["abc"]}  

我们首先使用以下函数创建一个词袋模型:

def associate_terms_with_user(unique_term_set, all_users_terms_dict):

    associated_value_return_dict = {}

    # consider the first user
    for user_id in all_users_terms_dict:

        # what terms *could* this user have possibly used
        this_user_zero_vector = []

        # this could be refactored somehow
        for term in  unique_term_set:
            this_user_zero_vector.extend('0')

        # what terms *did* this user use
        terms_belong_to_this_user = all_users_terms_dict.get(user_id)

        # let's start counting all the possible terms that this term in the personal
        # user list of words could correspond to... 
        global_term_element_index = 0

        # while this one term is in the range of all possible terms
        while global_term_element_index < len(unique_term_set):

            # start counting the number of terms he used
            local_term_set_item_index = 0

            # if this one term he used is still in the range of terms he used, counting them one by one
            while local_term_set_item_index < len(terms_belong_to_this_user):

                # if this one user term is the same as this one global term
                if list(unique_term_set)[global_term_element_index] == terms_belong_to_this_user[local_term_set_item_index]:

                    # increment the number of times this user used this term
                    this_user_zero_vector[global_term_element_index] = '1'

                # go to the next term for this user
                local_term_set_item_index += 1

            # go to the next term in the global list of all possible terms
            global_term_element_index += 1

        associated_value_return_dict.update({user_id: this_user_zero_vector})

    pprint.pprint(associated_value_return_dict)

程序的输出如下所示:

{'007': ['0', '0', '1'], 
 '666': ['0', '1', '1'], 
 '888': ['1', '0', '0']}

我们如何实现一个简单的函数来根据它们彼此的相似性对这些向量进行聚类?我设想使用 k-means 和可能的 scikit-learn。

我以前从来没有这样做过,也不知道怎么做,我对机器学习很陌生,我什至不知道从哪里开始。

最后666007 可能会聚集在一起,而888 会单独在一个集群中,不是吗?

完整代码位于here

【问题讨论】:

  • 我认为 Kmeans 是个好主意。你可以在这里看到一个例子:link
  • 啊,很酷-谢谢。但我的意思是——你知道我将如何输入我必须使用 k-means 函数的那组词dict 数据结构吗?我需要先改变它吗?
  • 我将在答案中发布一些网站。有一些例子和答案。希望这会有所帮助
  • K-means 不适用于短文本。

标签: python machine-learning scikit-learn cluster-analysis k-means


【解决方案1】:

Kmeans 是个好主意。

来自网络的一些示例和代码:

1) 使用 Python 进行文档聚类link

2) 在 Python 中使用 scikit-learn kmeans 对文本文档进行聚类link

3) 将一长串字符串(单词)聚类成相似组link

4) Kaggle 帖子link

【讨论】:

    猜你喜欢
    • 2015-02-15
    • 2011-10-04
    • 2016-02-01
    • 2018-02-26
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 2016-08-14
    • 2015-04-11
    相关资源
    最近更新 更多