Python 以x轴0.05的规则间隔提取Y轴的值

Python 以x轴0.05的规则间隔提取Y轴的值,python,arrays,numpy,Python,Arrays,Numpy,我有两个数组的数据。我想绘制ROC_AUC曲线,并在x轴的规则间隔提取y轴的值。我成功地绘制了ROC曲线,但很难在x轴的规则范围内提取值 这是我的尝试: import numpy from matplotlib import pyplot x = numpy.array([0,0,0,0.33333333,0.33333333,0.66666667,0.66666667,1,1]) y = numpy.array([0,0.05,0.8,0.8,0.85333,0.85333,0.912,0.

我有两个数组的数据。我想绘制ROC_AUC曲线,并在x轴的规则间隔提取y轴的值。我成功地绘制了ROC曲线,但很难在x轴的规则范围内提取值

这是我的尝试:

import numpy
from matplotlib import pyplot

x = numpy.array([0,0,0,0.33333333,0.33333333,0.66666667,0.66666667,1,1])
y = numpy.array([0,0.05,0.8,0.8,0.85333,0.85333,0.912,0.912,1])
print(x)
print(y)

pyplot.plot(x, y, linestyle='--', color='navy', label='PTI')

for i,j in zip(x,y):
        pyplot.annotate(str(j),xy=(i,j))

# axis labels
pyplot.xlabel('False Positive Rate')
pyplot.ylabel('True Positive Rate')

pyplot.xticks(np.arange(min(x), max(x)+0.05, 0.05))

#show the legend
pyplot.legend()
  

#show the plot
pyplot.savefig('ROC', dpi = 500)
pyplot.show()
Rocplot: 我的预期输出文本文件应为:

 0             0
 0             0.05
 0             0.8 
 0.05          0.8
 0.10          0.8
 0.15          0.8
 0.20          0.8
 0.25          0.8
 0.30          0.8
 0.33333333    0.8
 0.33333333    0.85333
 0.35          0.85333 
 0.40          0.85333
 .             . 
 .             .
 .             .
 0.65          0.85333                   
 0.66666667    0.85333
 0.66666667    0.912   
 0.70          0.912
 .             .
 .             .
 1             0.912             
 1             1

表示如果y=0.8,x=0,y=0.8,x=0.0333;因此,y值在x=0.05、0.10、1.15…….0.03的范围内保持0.8。在x的另一个间隔中也是如此。

scipy的interp1d可用于解释和重新采样数据-参见下面的代码,但我发现在0.05处重新采样时,不足以完美地重新创建信号-这由图上的橙色线表示,如果这并不重要,那么不要担心,否则您需要以更高的速率采样以获得完全间隔的采样,请参见绿色虚线,该虚线在0.005处采样

import numpy as np
import matplotlib.pyplot as plt

from scipy.interpolate import interp1d  


x = np.array([0,0,0,0.33333333,0.33333333,0.66666667,0.66666667,1,1])
y = np.array([0,0.05,0.8,0.8,0.85333,0.85333,0.912,0.912,1])

for x_, y_ in zip(x, y):
  print(x_, y_)

F = interp1d(x,y, kind = 'next')#, fill_value='nearest') 
times_resample_005 = np.arange(x.min(), x.max(), 0.05)
values_resample_005 = F(times_resample_005)

times_resample_0005 = np.arange(x.min(), x.max(), 0.005)
print(times_resample_0005[:10])
values_resample_0005 = F(times_resample_0005)


## ploting the data
fig, axs = plt.subplots(1, figsize=(10,10))
axs.plot(x,y, 'o')
axs.plot(times_resample_005,values_resample_005)
axs.plot(times_resample_0005,values_resample_0005, '--')
axs.set_xticks(np.arange(min(times), max(times)+0.05, 0.05))
print()

AFAIK,在Numpy中没有这样做的功能(也可能没有任何有用的构建块)。因此,您需要使用普通的Python循环自己编写此代码。