Logo
Section 1 / 6

从 ROC 曲线评估准确率

掌握 ROC 曲线及其曲线下面积(AUC)在评估模型准确性和指示分类性能方面的应用。

导入

从 ROC 曲线评估准确率

掌握 ROC 曲线及其曲线下面积(AUC)在评估模型准确性和指示分类性能方面的应用。

1# 使用 NetworkX 创建有向图 2G = nx.DiGraph() 3 4# 添加节点和边 5G.add_nodes_from(nodes + maturity_nodes) 6G.add_edges_from(Edges + maturity_edges) 7 8# 定义节点的笛卡尔坐标 9pos = { 10 "A": (0, 2), "B": (1, 2), "C": (0, 1), 11 "D": (2, 2), "E": (2, 0), "F": (0, 0), 12 "G": (3, 1), "H": (4, 0), "I": (2, 1), 13 "J": (1, 1), "Project_Overcost": (2, -1) 14} 15 16# 定义成熟度节点的位置 17maturity_pos = {f"Maturity_{node}": (pos[node][0] -0.45, pos[node][1] + 0.15) for node in nodes} 18pos.update(maturity_pos) 19 20# 绘制图形 21plt.figure(figsize=(12, 8)) 22nx.draw(G, pos, with_labels=True, node_size=2000, node_color="lightblue", font_size=12, font_weight='bold') 23 24plt.show() 25plt.savefig('output/FinalModel')
1# 将数据拆分为训练集和测试集 2train_df, test_df = train_test_split(df, test_size=0.15, random_state=12) 3 4# 使用离散化数据拟合贝叶斯网络 5bn = BayesianNetwork(sm) 6bn = bn.fit_node_states(train_df) 7 8# 使用 BayesianEstimator 拟合 CPDs 9bn = bn.fit_cpds(train_df, method="BayesianEstimator", bayes_prior="K2") 10#bn = bn.fit_cpds(train_df, method="BayesianEstimator", bayes_prior="BDeu",equivalent_sample_size=5) 11 12# 创建一个使用 InferenceEngine 进行预测的函数: 13ie = InferenceEngine(bn) 14 15def make_predictions(test_df): 16 predictions = [] 17 18 for _, row in test_df.iterrows(): 19 query = {col: row[col] for col in train_df.columns if col != 'Project_Overcost'} 20 marginals = ie.query(query) 21 pred_prob = marginals["Project_Overcost"][1] 22 predictions.append(pred_prob) 23 24 return np.array(predictions) 25 26# 进行预测并计算 ROC 曲线 27y_true = test_df['Project_Overcost'].values 28y_pred = make_predictions(test_df) 29fpr, tpr, _ = roc_curve(y_true, y_pred) 30roc_auc = auc(fpr, tpr)

构建一个复杂的贝叶斯网络

查询贝叶斯网络