Skip to main content
 首页 » 编程设计

python之使用python对词袋模型进行简单的k均值聚类

2024年10月01日8think

输入数据集如下所示:

{"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。

我以前从来没有这样做过,我也不知道怎么做,总的来说,我是机器学习的新手,我什至不知道从哪里开始。

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

完整代码在here .

请您参考如下方法:

Kmeans 是个好主意。

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

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

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

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

4) Kaggle 帖子 link