zoukankan      html  css  js  c++  java
  • 随机分配主题

    # 随机给词汇分配一个主题
    # 单词i属于第k个主题的个数加1,所以
    # 文章m属于主题k的词汇的个数也加1,因为词汇i来自文章m
    # 因此每个主题下词汇个数也加1

    代码实现:

           for x in range(len(self.Z)):
                self.ndsum[x] = self.dpre.docs[x].length
                for y in range(self.dpre.docs[x].length):
                    
                    topic = random.randint(0,self.K-1)#初始时随机给文本中的每个单词分配主题
                    self.Z[x][y] = topic
                    self.nw[self.dpre.docs[x].words[y]][topic] += 1
                    self.nd[x][topic] += 1
                    self.nwsum[topic] += 1

    # z= [list([0, 1, 1, 2, 0]) list([2, 0, 2, 1]) list([2, 1, 2, 0]) list([2, 2, 2, 0]) list([2, 2, 1, 2]) list([0, 1, 2, 0])]#每个词汇的主题号

    topic = self.Z[i][j]
    word = self.dpre.docs[i].words[j]

    1,

    self.nw[word][topic] -= 1#排除当前第i个词,词汇在各个主题上的分布

    # nw=词word在主题上的分布 此时主题为3

    #[[1 0 1]
    # [1 2 0]
    # [0 1 1]
    # [0 0 1]
    # [1 0 0]
    # [1 1 1]
    # [2 0 1]
    # [0 0 1]
    # [0 1 2]
    # [0 0 1]
    # [0 1 1]
    # [0 0 1]
    # [1 0 1]]

    2,
    self.nd[i][topic] -= 1#排除当前第i个词,每个文章中各个主题下词的总数

    # nd= 每个文章中各个主题下词的总数
    # [[2 2 1]
    # [1 1 2]
    # [1 1 2]
    # [1 0 3]
    # [0 1 3]
    # [2 1 1]]

    3,
    self.nwsum[topic] -= 1#排除当前第i个词,每个主题下词的总数

    # nwsum= [ 7  6 12] 各个topic下词的总数

    4,
    self.ndsum[i] -= 1#排除当前第i个词,每篇文章中词的总数

    # ndsum= [5 4 4 4 4 4] 每篇文章中词的总数

  • 相关阅读:
    MVC Form
    The way to learn english
    Test FastThree
    C#中Trim()、TrimStart()、TrimEnd()的用法
    c# Dictionary 简介
    visual studio快捷键大全
    ASP.NET MVC 中 ActionResult
    MVC4中使用 Ninject
    MVC Chapter 12 Overview of MVC Projects
    ASP.NET Razor
  • 原文地址:https://www.cnblogs.com/smuxiaolei/p/7643198.html
Copyright © 2011-2022 走看看