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

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

输入数据集如下所示:

{"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 是个好主意。

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

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

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

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

4) Kaggle 帖子 link

关于python - 使用python对词袋模型进行简单的k均值聚类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44886756/

相关文章:

python - Python 中的基准测试 : Why does my code run slower with repetition?

java - 使用 Runtime.exec() 从 Java 代码调用 Python 脚本 : ImportError: No module named sklearn

ios - 将 .model 文件添加到我的 xcode 项目的最佳方式?

python - 在列表字典上使用 TfidfVectorizer

python warnings.filterwarnings 不会忽略来自 'import sklearn.ensemble' 的 DeprecationWarning

python - 最有效的 groupby 方式 => 聚合 pandas 中的大型数据框

python - 使用 python 替代 awk 来代替 Linux 命令

python - 为 Tensorflow 2.0 适配 Tensorflow RNN Seq2Seq 模型代码

algorithm - 如何从纯文本中提取特征?

python - sklearn 和 n_jobs 中的超参数优化 > 1 : Pickling