我正在尝试使用pgmpy模块中的查询功能来推断贝叶斯网络的条件概率。我的代码如下:
data_test = pd.DataFrame(np.random.randint(1, 3, size = (240, 5)),columns=['A','B','C','D','E'])
best_model.fit(data_test)
bayes_i = VariableElimination(best_model)
# find out the probability of B given other parameters' values
q = bayes_i.query(variables=['B'], evidence={'A': 1,'C': 2,'D': 2,'E': 1})
print (q['B'])
q = bayes_i.query(variables=['B'], evidence={'A': 2,'C': 1,'D': 1,'E': 1})
print (q['B'])
错误消息是: IndexError:索引2超出了尺寸2的轴0的范围
编辑:模型是通过以下方式构造的:
def Bayes(dataset):
start = time.time()
# exhuastive search and find the set of the best DAG
searcher = ExhaustiveSearch(dataset, scoring_method=K2Score(dataset))
model_list = []
for score, model in searcher.all_scores():
model_list.append(model.edges())
model = BayesianModel(model_list[0])
end = time.time()
print('Time used to construct the model is: ' + str(end - start))
return model
best_model = Bayes(data_random)