Logo
Section 1 / 9

性能指标

通过学习关键的数据驱动性能指标,提升你的贝叶斯网络的性能和准确性。

导入

性能指标

通过学习关键的数据驱动性能指标,提升你的贝叶斯网络的性能和准确性。

MeasureFormulaDescription
Accuracy(TP + TN)/(TP + TN + FP + FN)Accuracy refers to the degree of closeness of the model's predictions to the true values. However, accuracy can be misleading in unbalanced data situations, as a high accuracy may not indicate successful identification of minority classes. For example, if we have a dataset containing 95 cats and 5 dogs and our model predicts that every image is a cat we’ll have a 95% accuracy score despite correctly identifying none of the dogs.
PrecisionTP/(TP+FP)Precision refers to the proportion of correct positive identifications made by the model. A low precision score indicates more false positives, while a model with no false positives has a precision of 1.0. For example, let's assume we have a dataset of 100 emails, where 10 are spam and 90 are not. Our model is designed to identify spam emails. If the model identifies 8 emails as spam correctly (true positives) but also incorrectly labels 12 non-spam emails as spam (false positives), then the precision of our model is calculated as 40%This means that only 40% of the emails identified as spam by our model are spam, indicating a high rate of misclassification of non-spam emails as spam.
Recall (True positive rate)TP/(TP+FN)Recall is the proportion of actual positives that the model identifies correctly. A low recall score indicates a higher number of false negatives. A model with no false negatives achieves a recall of 1.0. Continuing with the email example, if out of the 10 actual spam emails, our model correctly identifies 8 as spam (true positives) but misses 2 (false negatives), the recall of our model is calculated as 80%This means our model successfully identifies 80% of the actual spam emails, missing 20% of them.
False positive rateFP/(FP+TN)The false positive rate represents the proportion of incorrect positive identifications made by the model. A high false positive rate indicates a greater number of false positives. Using the same scenario, out of the 90 non-spam emails, our model incorrectly identifies 12 as spam (false positives), while correctly identifying 78 as not spam (true negatives). The false positive rate is calculated as 13.3%This indicates that our model incorrectly identified 13.3% of the non-spam emails as spam, which could lead to potentially missing important emails if they are automatically moved to a spam folder.
1# New library to import (the other ones are already imported) 2from sklearn.model_selection import train_test_split 3from sklearn.metrics import accuracy_score, precision_score, recall_score 4 5# Create a DataFrame from the generated boolean arrays 6df = pd.DataFrame({"Cloudy": cloudy, "Sprinkler": sprinkler, "Rain": rain, "Wet_Grass": wet_grass}) 7 8# Split the dataset into training and testing sets 9train_df, test_df = train_test_split(df, test_size=0.05, random_state=42) 10 11# Define the network structure 12sm = StructureModel() 13 14# Adding the edges 15sm.add_edges_from([ 16 ('Cloudy', 'Sprinkler'), 17 ('Cloudy', 'Rain'), 18 ('Sprinkler', 'Wet_Grass'), 19 ('Rain', 'Wet_Grass')]) 20 21# Create the model 22bn = BayesianNetwork(sm) 23 24# Use the df (columns) to create the nodes of the network 25bn = bn.fit_node_states(train_df) 26 27# Fit the model with the train set 28bn = bn.fit_cpds(train_df, method="BayesianEstimator", bayes_prior="K2") 29 30# Create the query 31ie = InferenceEngine(bn) 32marginals = ie.query() 33 34# Create a function to make the predictions 35def make_binary_predictions(test_df, threshold=0.5): 36 predictions = [] 37 38 for _, row in test_df.iterrows(): 39 query = {col: row[col] for col in train_df.columns if col != 'Wet_Grass'} 40 marginals = ie.query(query) 41 pred_prob = marginals["Wet_Grass"][1] 42 pred = 1 if pred_prob >= threshold else 0 43 predictions.append(pred) 44 45 return np.array(predictions) 46 47# Make binary predictions and compute metrics 48y_true = test_df['Wet_Grass'].values 49y_pred = make_binary_predictions(test_df) 50 51accuracy = accuracy_score(y_true, y_pred) 52precision = precision_score(y_true, y_pred) 53recall = recall_score(y_true, y_pred) 54 55print(f"Accuracy: {accuracy:.2f}") 56print(f"Precision: {precision:.2f}") 57print(f"Recall: {recall:.2f}")

基于结构的超参数在贝叶斯网络中

接收者操作特征曲线