(self, datas, attr_len, labels, dense=True)
贝叶斯训练函数 
            params:
                datas 训练数据 ， [[]]
                attr_len 属性长度
                labels 分类数组 ， 与datas对应
                dense 是否为稀疏矩阵，现在只支持dense=True
            return
                None
            raise 
                None
贝叶斯训练函数 
            params:
                datas 训练数据 ， [[]]
                attr_len 属性长度
                labels 分类数组 ， 与datas对应
                dense 是否为稀疏矩阵，现在只支持dense=True
            return
                None
            raise 
                None
贝叶斯训练函数 params : datas 训练数据 ， [[ ]] attr_len 属性长度 labels 分类数组 ， 与datas对应 dense 是否为稀疏矩阵，现在只支持dense = True return None raise None
def train(self, datas, attr_len, labels, dense=True):
        """贝叶斯训练函数 
            params:
                datas 训练数据 ， [[]]
                attr_len 属性长度
                labels 分类数组 ， 与datas对应
                dense 是否为稀疏矩阵，现在只支持dense=True
            return
                None
            raise 
                None
        """
        self.label_status = Counter(labels)
        self.default_prob = defaultdict(float)
        self.attr_status = {
            i: defaultdict(lambda: defaultdict(float)) for i in range(attr_len)}
        self.base_count = len(datas)
        self.attr_range = range(attr_len)
        for i in range(len(datas)):
            for j in range(attr_len):
                attr_val = datas[i][j]
                # 统计每个属性对应 p(I | C) , I < (v1 , v2 ,v3....,vn)
                self.attr_status[j][attr_val][labels[i]] += 1.
        # 计算每个属性出现val 时 ， P(v1|I,C)
        for feature, attr_label in self.attr_status.items():
            for attr_val, label in self.attr_status[feature].items():
                for cl in label.keys():
                    self.attr_status[feature][attr_val][
                        cl] /= ( self.label_status[cl] + self.base_count)
        for label in self.label_status.keys():
            self.default_prob[label] = 1. / ( self.label_status[label] + self.base_count)
        # 计算所有类别出现的概率 P(C) = sum(Cn) / sum(C) , n < (1,2,3,4,5....n)
        labels_count = float(sum(self.label_status.values()))
        for label, count in self.label_status.items():
            self.label_status[label] /= labels_count
def train ( self , datas , attr_len , labels , dense = True ) : self . label_status = Counter ( labels ) self . default_prob = defaultdict ( float ) self . attr_status = { i : defaultdict ( lambda : defaultdict ( float ) ) for i in range ( attr_len ) } self . base_count = len ( datas ) self . attr_range = range ( attr_len ) for i in range ( len ( datas ) ) : for j in range ( attr_len ) : attr_val = datas [ i ] [ j ] # 统计每个属性对应 p(I | C) , I < (v1 , v2 ,v3....,vn) self . attr_status [ j ] [ attr_val ] [ labels [ i ] ] += 1. # 计算每个属性出现val 时 ， P(v1|I,C) for feature , attr_label in self . attr_status . items ( ) : for attr_val , label in self . attr_status [ feature ] . items ( ) : for cl in label . keys ( ) : self . attr_status [ feature ] [ attr_val ] [ cl ] /= ( self . label_status [ cl ] + self . base_count ) for label in self . label_status . keys ( ) : self . default_prob [ label ] = 1. / ( self . label_status [ label ] + self . base_count ) # 计算所有类别出现的概率 P(C) = sum(Cn) / sum(C) , n < (1,2,3,4,5....n) labels_count = float ( sum ( self . label_status . values ( ) ) ) for label , count in self . label_status . items ( ) : self . label_status [ label ] /= labels_count