R 或 Python 中的 ROC 曲面图

问题描述 投票:0回答:1

设 X、Y、Z 分别为来自 F1、F2、F3 cdf 的随机变量。我想为 ROC 曲面图编写手动代码。我该怎么做?通过使用 R studio 或 Python。谢谢您的帮助。

x<-rW(100,7,2)
y<-rW(100,9,4)
z<-rW(100,18,8)
n <- 50
x <- seq(0, 1, length.out = n)
y <- seq(0, 1, length.out = n)
z <- outer(x, y, function(x, y) ifelse(x <= y, x, y))

# Multiplying z values to increase the surface volume
z <- z * 2  # For example, we multiply z values by 2

# Create a 3D surface graph
plot_ly(x = x, y = y, z = z, type = "surface") %>%
  layout(scene = list(
xaxis = list(title = "False Positive Rate (FPR)", range = c(0, 1)),
yaxis = list(title = "True Positive Rate (TPR)", range = c(0, 1)),
zaxis = list(title = "Threshold")
)) 

我尝试过这段代码。但是,我不喜欢它。 我需要绘图上的 VUS 值,并且每个轴必须介于 0 和 1 之间。请参阅“医学统计 - 2004 年 - Nakas - 使用连续测量进行有序多类 ROC 分析”文章。我需要像图 2 或类似的东西那样绘制。

python r roc surface
1个回答
0
投票

要创建与您描述的类似的 ROC 曲面,您可以使用

Matplotlib
NumPy

等库
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Declare X, Y, Z
np.random.seed(0)
x = np.random.normal(7, 2, 100)
y = np.random.normal(9, 4, 100)
z = np.random.normal(18, 8, 100)

# Define the number of thresholds
n = 50

# Create thresholds
thresholds = np.linspace(0, 1, n)

# Initialize an empty array to store the ROCs
roc_values = np.zeros((n, n))

# Calculate ROCs for each threshold combination
for i, threshold_x in enumerate(thresholds):
    for j, threshold_y in enumerate(thresholds):
        roc_values[i, j] = np.mean((x <= threshold_x) & (y <= threshold_y) & (z <= threshold_x))

# Create a grid for surface plot
X, Y = np.meshgrid(thresholds, thresholds)

# Plot the ROC surface
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, roc_values, cmap='viridis')

# Set labels and title
ax.set_xlabel('False Positive Rate (FPR)')
ax.set_ylabel('True Positive Rate (TPR)')
ax.set_zlabel('VUS Value')
ax.set_title('ROC Surface Plot')

# Set limits for axes
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)

plt.show()

您可以根据需要调整样本数据生成和阈值参数。

© www.soinside.com 2019 - 2024. All rights reserved.