■ Precision-Recall Graph를 그리는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import matplotlib.pyplot as pp import numpy as np import sklearn.metrics as metrics modelAClassValueNDArray = np.array([0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 1 , 0 , 0 , 0 , 1 , 1 ]) modelAClassProbabilityNDArray = np.array([0.05, 0.05, 0.15, 0.15, 0.25, 0.25, 0.35, 0.35, 0.45, 0.45, 0.55, 0.55, 0.65, 0.85, 0.95]) modelBClassValueNDArray = np.array([0 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 1 ]) modelBClassProbabilityNDArray = np.array([0.05, 0.05, 0.15, 0.15, 0.25, 0.25, 0.25, 0.35, 0.35, 0.45, 0.55, 0.55, 0.65, 0.75, 0.95]) modelAPrecisionNDArray, modelARecallRateNDArray, _ = metrics.precision_recall_curve(modelAClassValueNDArray, modelAClassProbabilityNDArray) modelBPrecisionNDArray, modelBRecallRateNDArray, _ = metrics.precision_recall_curve(modelBClassValueNDArray, modelBClassProbabilityNDArray) modelAAP = metrics.average_precision_score(modelAClassValueNDArray, modelAClassProbabilityNDArray) modelBAP = metrics.average_precision_score(modelBClassValueNDArray, modelBClassProbabilityNDArray) pp.title("Precision-Recall Graph") pp.xlabel("Recall" ) pp.ylabel("Precision") pp.plot(modelARecallRateNDArray, modelAPrecisionNDArray, "b", label = "Model A (AP = %0.2F)" % modelAAP) pp.plot(modelBRecallRateNDArray, modelBPrecisionNDArray, "g", label = "Model B (AP = %0.2F)" % modelBAP) pp.legend(loc = "upper right") pp.show() |