Isolation Forest 的 ROC 曲线
ROC curve for Isolation Forest
我正在尝试绘制 ROC 曲线来评估隔离森林对乳腺癌数据集的准确性。我从混淆矩阵中计算出真阳性率 (TPR) 和假阳性率 (FPR)。但是,我不明白 TPR 和 FPR 是如何以矩阵形式出现的,而不是单个整数值。而且ROC曲线似乎只对矩阵形式的FPR和TPR有效(我也试过手动写计算FPR和TPR的代码)。
TPR和FPR的值都是矩阵形式吗?
无论哪种方式,我的 ROC 曲线都是一条直线。为什么会这样?
混淆矩阵:
from sklearn.metrics import confusion_matrix
cnf_matrix = confusion_matrix(y, y_pred_test1)
O/P :
> [[ 5 25]
> [ 21 180]]
真阳性和假阳性:(另外,为什么这些值直接取自混淆矩阵?)
F_P = cnf_matrix.sum(axis=0) - np.diag(cnf_matrix)
F_N = cnf_matrix.sum(axis=1) - np.diag(cnf_matrix)
T_P = np.diag(cnf_matrix)
T_N = cnf_matrix.sum() - (FP + FN + TP)
F_P = F_P.astype(float)
F_N = F_N.astype(float)
T_P = T_P.astype(float)
T_N = T_N.astype(float)
O/P :
False Positive [21. 25.]
False Negative [25. 21.]
True Positive [ 5. 180.]
True Negative [180. 5.]
TPR 和 FPR:
tp_rate = TP/(TP+FN)
fp_rate = FP/(FP+TN)
O/P :
TPR : [0.16666667 0.89552239]
FPR [0.10447761 0.83333333]
ROC 曲线:
from sklearn import metrics
import matplotlib.pyplot as plt
plt.plot(fp_rate,tp_rate)
plt.show()
O/P :
混淆矩阵实质上为您提供了 ROC 曲线上的一个点。要构建 'full' ROC 曲线,您需要一个概率列表,然后可以通过改变用于确定 class 预测的 'threshold' 来绘制 ROC 曲线,以确定哪个 class 每个实例属于。
在您的简单情况下(当您只有 ROC 曲线的一个点时),您可以通过外推到原点和点 (1,1) 来绘制 ROC 曲线:
# compare to your confusion matrix to see values.
TP = 180
FN = 21
tpr = TP/(TP+FN)
fpr = 1-tpr
tpr_line = [0, tpr, 1]
fpr_line = [0, fpr 1]
plt.plot(fpr, tpr, 'k-', lw=2)
plt.xlabel('FPR')
plt.ylabel('TPR')
plt.xlim(0, 1)
plt.ylim(0, 1)
ROC 曲线如下所示:
confusion_matrix()
函数只为您提供 correctly/misclassified 点,但不提供有关模型在误class 确定数据点时的置信度的信息。
此信息用于创建 ROC 曲线(用于衡量模型根据每个数据点对特定 class 的可能性对其进行排名的能力)。
相反,使用 decision_function()
or score_samples()
functions to calculate the model's confidence that each data point is (or is not) an anomaly. Then, use roc_curve()
获取绘制曲线本身所需的点。
这是乳腺癌数据集的示例。
from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(return_X_y=True)
from sklearn.ensemble import IsolationForest
clf = IsolationForest(behaviour='new', max_samples=100,
random_state=0, contamination='auto')
clf.fit(X)
y_pred = clf.score_samples(X)
from sklearn.metrics import roc_curve
fpr, tpr, thresholds = roc_curve(y,y_pred)
import matplotlib.pyplot as plt
plt.plot(fpr, tpr, 'k-', lw=2)
plt.xlabel('FPR')
plt.ylabel('TPR')
plt.show()
我正在尝试绘制 ROC 曲线来评估隔离森林对乳腺癌数据集的准确性。我从混淆矩阵中计算出真阳性率 (TPR) 和假阳性率 (FPR)。但是,我不明白 TPR 和 FPR 是如何以矩阵形式出现的,而不是单个整数值。而且ROC曲线似乎只对矩阵形式的FPR和TPR有效(我也试过手动写计算FPR和TPR的代码)。
TPR和FPR的值都是矩阵形式吗?
无论哪种方式,我的 ROC 曲线都是一条直线。为什么会这样?
混淆矩阵:
from sklearn.metrics import confusion_matrix
cnf_matrix = confusion_matrix(y, y_pred_test1)
O/P :
> [[ 5 25]
> [ 21 180]]
真阳性和假阳性:(另外,为什么这些值直接取自混淆矩阵?)
F_P = cnf_matrix.sum(axis=0) - np.diag(cnf_matrix)
F_N = cnf_matrix.sum(axis=1) - np.diag(cnf_matrix)
T_P = np.diag(cnf_matrix)
T_N = cnf_matrix.sum() - (FP + FN + TP)
F_P = F_P.astype(float)
F_N = F_N.astype(float)
T_P = T_P.astype(float)
T_N = T_N.astype(float)
O/P :
False Positive [21. 25.] False Negative [25. 21.] True Positive [ 5. 180.] True Negative [180. 5.]
TPR 和 FPR:
tp_rate = TP/(TP+FN)
fp_rate = FP/(FP+TN)
O/P :
TPR : [0.16666667 0.89552239] FPR [0.10447761 0.83333333]
ROC 曲线:
from sklearn import metrics
import matplotlib.pyplot as plt
plt.plot(fp_rate,tp_rate)
plt.show()
O/P :
混淆矩阵实质上为您提供了 ROC 曲线上的一个点。要构建 'full' ROC 曲线,您需要一个概率列表,然后可以通过改变用于确定 class 预测的 'threshold' 来绘制 ROC 曲线,以确定哪个 class 每个实例属于。
在您的简单情况下(当您只有 ROC 曲线的一个点时),您可以通过外推到原点和点 (1,1) 来绘制 ROC 曲线:
# compare to your confusion matrix to see values.
TP = 180
FN = 21
tpr = TP/(TP+FN)
fpr = 1-tpr
tpr_line = [0, tpr, 1]
fpr_line = [0, fpr 1]
plt.plot(fpr, tpr, 'k-', lw=2)
plt.xlabel('FPR')
plt.ylabel('TPR')
plt.xlim(0, 1)
plt.ylim(0, 1)
ROC 曲线如下所示:
confusion_matrix()
函数只为您提供 correctly/misclassified 点,但不提供有关模型在误class 确定数据点时的置信度的信息。
此信息用于创建 ROC 曲线(用于衡量模型根据每个数据点对特定 class 的可能性对其进行排名的能力)。
相反,使用 decision_function()
or score_samples()
functions to calculate the model's confidence that each data point is (or is not) an anomaly. Then, use roc_curve()
获取绘制曲线本身所需的点。
这是乳腺癌数据集的示例。
from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(return_X_y=True)
from sklearn.ensemble import IsolationForest
clf = IsolationForest(behaviour='new', max_samples=100,
random_state=0, contamination='auto')
clf.fit(X)
y_pred = clf.score_samples(X)
from sklearn.metrics import roc_curve
fpr, tpr, thresholds = roc_curve(y,y_pred)
import matplotlib.pyplot as plt
plt.plot(fpr, tpr, 'k-', lw=2)
plt.xlabel('FPR')
plt.ylabel('TPR')
plt.show()