我想在逻辑回归中得到ROC的最佳截止点作为数字而不是两条交叉曲线.使用下面的代码,我可以得到将显示最佳点的图,但在某些情况下,我只需要将该点作为可用于其他计算的数字.以下是代码行:
library(Epi)
ROC( form = IsVIP ~ var1+var2+var3+var4+var5, plot="sp", data=vip_data )
Run Code Online (Sandbox Code Playgroud)
谢谢
我有一个使用 Matplotlib 生成的图(它最初是直方图的精确回忆曲线),我需要计算与 y = 0.9 的 y 值相对应的正确 x 值。数据是从列中存在的文本文件加载的。这是用于创建绘图的代码:
import numpy as np
import matplotlib.pyplot as plt
import pylab
from sklearn import metrics
data1 = np.loadtxt('text1.txt')
data2 = np.loadtxt('text2.txt')
background = 1 - (1 + y) / 2.
signal = 1 - (1 + x) / 2.
classifier_output = np.concatenate([background,signal])
true_value = np.concatenate([np.zeros_like(background, dtype=int), np.ones_like(signal, dtype=int)])
precision, recall, threshold = metrics.precision_recall_curve(true_value, classifier_output)
plt.plot(threshold, precision[:-1])
plt.savefig('Plot.pdf', dpi = 2000)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我有以下ROC曲线:

并且它不会以1.0结尾,因为我的预测包括例如零
预测= [0.9,0.1,0.8,0.0]
对于ROC曲线,我采用top-k预测,首先是{0.9},然后是{0.9,0.8}等.如果预测中没有值> 0,则预测不会随着k的增加而改变.
因此我无法得到零的真正负值,并且由于误报率为fp /(fp + tn),因此曲线在达到1之前结束.
现在,我是否应该人为地使用零进行预测,或者如果曲线就这样结束就可以了?使用零也感觉不对.或者我错过了什么?
如何在r中使用ROCR包绘制ROC曲线,只有一个分类列联表?
我有一个列联表,其中真正的正面,误报等等.所有额定值都可以计算出来.我有500个复制,因此有500个表.但是,我无法生成指示每个估计概率和真值的单个案例的预测数据.如何在没有单个数据的情况下获得曲线.以下是使用的包指令.
## computing a simple ROC curve (x-axis: fpr, y-axis: tpr)
library(ROCR)
data(ROCR.simple)
pred <- prediction( ROCR.simple$predictions, ROCR.simple$labels)
perf <- performance(pred,"tpr","fpr")
plot(perf)
Run Code Online (Sandbox Code Playgroud) 该ROCR库R提供了绘制平均 ROC 曲线的能力(来自ROCR 参考手册):
library(ROCR)
library(ROCR)
data(ROCR.xval)
# plot ROC curves for several cross-validation runs (dotted
# in grey), overlaid by the vertical average curve and boxplots
# showing the vertical spread around the average.
data(ROCR.xval)
pred <- prediction(ROCR.xval$predictions, ROCR.xval$labels)
perf <- performance(pred,"tpr","fpr")
plot(perf,col="grey82",lty=3)
plot(perf,lwd=3,avg="vertical",spread.estimate="boxplot",add=TRUE)
Run Code Online (Sandbox Code Playgroud)
迷人的。不幸的是,似乎无法将平均 ROC 曲线本身作为对象/数据帧/等获取。用于进一步的统计测试(例如,使用 pROC)。我确实做了一些研究(尽管可能是在事实之后),我发现了这篇文章:
我查看了 ROCR 的代码,发现以下几行用于将结果传递给绘图:
performance_plots.R,(从第 451 行开始)
## compute average curve
perf.avg <- perf.sampled
perf.avg@x.values <- list( rowMeans( data.frame( perf.avg@x.values))) …Run Code Online (Sandbox Code Playgroud) 我试图从tensorflow提供的CIFAR-10示例的修改版本绘制ROC曲线.它现在是2班而不是10班.
网络的输出称为logits并采用以下形式:
[[-2.57313061 2.57966399] [0.04221377 -0.04033273] [-1.42880082 1.43337202] [-2.7692945 2.78173304] [-2.48195744 2.49331546] [2.0941515 -2.10268974] [-3.51670194 3.53267646] [-2.74760485 2.75617766] ...]
首先,这些logits实际代表什么?网络中的最后一层是WX + b形式的"softmax linear".
该模型能够通过调用计算准确性
top_k_op = tf.nn.in_top_k(logits, labels, 1)
Run Code Online (Sandbox Code Playgroud)
然后,一旦图形被初始化:
predictions = sess.run([top_k_op])
predictions_int = np.array(predictions).astype(int)
true_count += np.sum(predictions)
...
precision = true_count / total_sample_count
Run Code Online (Sandbox Code Playgroud)
这很好用.
但是现在如何从中绘制ROC曲线?
我一直在尝试"sklearn.metrics.roc_curve()"函数(http://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html#sklearn.metrics.roc_curve)但我不喜欢不知道该用什么作为我的"y_score"参数.
任何帮助,将不胜感激!
在运行逻辑回归后,我使用以下代码绘制 ROC 曲线。
fit1 <- glm(formula=GB160M3~Behvscore, data=eflscr,family="binomial", na.action = na.exclude)
prob1=predict(fit1, type=c("response"))
eflscr$prob1 = prob1
library(pROC)
g1 <- roc(GB160M3~prob1, data=eflscr, plot=TRUE, grid=TRUE, print.auc=TRUE)
Run Code Online (Sandbox Code Playgroud)
绘制的 ROC 曲线如下所示(见下面的链接)

我需要的是:
为此,我的方法是使用这个非常好的教程:
根据他的想法和方法,我只是简单地更改了如何获取原始数据的方式,如下所示:
df = pd.read_csv(
filepath_or_buffer='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',
header=None,
sep=',')
df.columns=['sepal_len', 'sepal_wid', 'petal_len', 'petal_wid', 'class']
df.dropna(how="all", inplace=True) # drops the empty line at file-end
df.tail()
# split data table into data X and class labels y
X = df.iloc[:,0:4].values
Y = df.iloc[:,4].values
Run Code Online (Sandbox Code Playgroud)
他们我只是运行代码。如果我尝试运行类似的指标,accuracy或者
balanced_accuracy一切正常(即使使用许多其他指标)。我的问题是,当我尝试使用指标运行时,roc_auc出现错误:
“ ValueError:y_true中仅存在一个类。在这种情况下,未定义ROC AUC分数。”
这个错误已经被讨论here1,here2,here3和here4。但是,我无法使用他们提供的任何“解决方案” /解决方法来解决我的问题。
我的整个代码是:
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import pandas as pd
import numpy as np …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用https://scikit-learn.org/stable/modules/ generated/sklearn.metrics.RocCurveDisplay.html#sklearn.metrics.RocCurveDisplay.from_predictions 中介绍的 sklearn 函数 RocCurveDisplay.from_predictions 。我像这样运行该函数:
from sklearn.metrics import RocCurveDisplay
true = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0.])
prediction = np.array([0., 0., 0., 0., …Run Code Online (Sandbox Code Playgroud) 由于我有二进制响应的数据,但事件很少,我想通过拟合bgeva模型而不是gam模型来改进其预测。为了证明和比较它的预测准确性并将其与我尝试过的其他模型进行比较,我需要计算 AUC 并绘制 ROC 曲线。
问题是我的代码,它适用于glmand gam,不适用于bgevaobject. 确切地说,该函数的使用predict()打印了 Error:
no applicable method for 'predict' applied to an object of class "bgeva"
而我的朋友 Google 没有为我找到任何解决方案。
下面是从一个简单的例子bgeva()包和代码,我用于计算AUC并绘制ROC曲线glm和gam对象:
library(bgeva)
set.seed(0)
n <- 1500
x1 <- round(runif(n))
x2 <- runif(n)
x3 <- runif(n)
f1 <- function(x) (cos(pi*2*x)) + sin(pi*x)
f2 <- function(x) (x+exp(-30*(x-0.5)^2))
y <- as.integer(rlogis(n, location = -6 + 2*x1 + f1(x2) + f2(x3), scale = …Run Code Online (Sandbox Code Playgroud)