pgmpy包构建贝叶斯网络架构(Bayesian Network Structure)

import pandas as pd
import numpy as np
from pgmpy.estimators import ExhaustiveSearch, HillClimbSearch, BicScore

def build_structure(data):
    df = pd.DataFrame(data)
    est = HillClimbSearch(df, scoring_method=BicScore(df))
    model = est.estimate()
    DAG = np.zeros((data.shape[1], data.shape[1]), np.int64)

    for edge in model.edges():
        DAG[edge[0], edge[1]] = 1

    np.save('prepare_data/DAG.npy', DAG)
    return DAG

if __name__=='__main__':
    data = np.load('prepare_data/data.npy')
    build_structure(data)

pgmpy接口说明网站http://pgmpy.org/estimators.html
此处data为numpy数据,每一行数据为一个实例,每一列为一个类别,之后输出类别依赖关系

你可能感兴趣的:(多标签学习,贝叶斯)