如何在Python中找到曲线方程未知时的对应X值

huangapple go评论76阅读模式
英文:

How to find corresponding X values when the curve equation is unknown in python

问题

我有属于累积分布的数据。但是x轴的范围取决于输入数据。y轴均匀分成5个段。我需要找到相应的X轴段。

我尝试使用torch的quantile如下所示

x_segments = torch.quantile(Cumulative_curve, torch.tensor(y_segemts))

然后将获得的x_segments与x轴范围相乘。但结果不如下图所示的预期。

如何使用Python来完成这个任务?

英文:

如何在Python中找到曲线方程未知时的对应X值

I have data belonging to cumulative distribution. But the range of the x-axis depends on the input data. The y-axis is equally divided into 5 segments. I need to find the corresponding X-axis segments to that.

I tried it with the torch quantile as follows

x_segments = torch.quantile(Cumulative_curve, torch.tensor(y_segemts))

Then the obtained x_segments multiply with the x-axis range. but the result is not as expected as shown in the following figure

How can I do this using python?

答案1

得分: 1

import torch

累积分布数据

cumulative_curve = torch.tensor([0.1, 0.3, 0.5, 0.7, 0.9])

定义y轴上的段数

num_segments = 5

计算y轴段的值

y_segments = torch.linspace(0, 1, num_segments + 1)

找到对应的x轴段

x_segments = torch.quantile(cumulative_curve, y_segments)

将x轴段乘以所需的x轴范围

x_range_min = 0 # x轴范围的最小值
x_range_max = 100 # x轴范围的最大值
x_segments *= (x_range_max - x_range_min)
x_segments += x_range_min

打印x轴段

for i in range(len(y_segments)):
print(f"段 {i+1}: [{x_segments[i]:.2f}, {x_segments[i+1]:.2f}]")

英文:

I don't have your sample data to test but I used my sample data and tried the following if this could help you

import torch

# Cumulative distribution data
cumulative_curve = torch.tensor([0.1, 0.3, 0.5, 0.7, 0.9])

# Define the number of segments on the y-axis
num_segments = 5

# Calculate the y-axis segment values
y_segments = torch.linspace(0, 1, num_segments + 1)

# Find the corresponding x-axis segments
x_segments = torch.quantile(cumulative_curve, y_segments)

# Multiply the x-axis segments with the desired x-axis range
x_range_min = 0  # Minimum value of the x-axis range
x_range_max = 100  # Maximum value of the x-axis range
x_segments *= (x_range_max - x_range_min)
x_segments += x_range_min

# Print the x-axis segments
for i in range(len(y_segments)):
    print(f"Segment {i+1}: [{x_segments[i]:.2f}, {x_segments[i+1]:.2f}]")

答案2

得分: 0

import matplotlib.pyplot as plt
import numpy as np
import torch

# 将您的累积分布曲线定义为列表或张量
cumulative_curve = [0.1, 0.3, 0.55, 0.8, 1.0]  # 示例数值
x = [0, 1, 2, 3, 4]

# 定义相应的y轴分段
y_segments = [0.2, 0.4, 0.6, 0.8, 1.0]  # 示例数值

# 定义x轴的最大值
N = 4  # 示例数值

# 将累积曲线转换为PyTorch张量
cumulative_curve_tensor = torch.tensor(cumulative_curve)

# 找到每个缩放后的y轴段的相应x轴段
x_seg = np.interp(y_segments, np.array(cumulative_curve), np.array(x))

plt.figure()
plt.plot(cumulative_curve_tensor.numpy().T, label='tanh-allJ')
for xc in y_segments:
    print('xc = ', xc)
    plt.axhline(y=xc, color='gray', linestyle='--')

for yc in list(x_seg):
    print('yc= ', yc)

    plt.axvline(x=yc, color='gray', linestyle='--')
print("相应的x轴段:", x_seg)

这段代码解决了我的问题。如果有人需要,也可以使用这个版本。

英文:
import matplotlib.pyplot as plt 
import numpy as np 
import torch

# Define your cumulative distribution curve as a list or a tensor
cumulative_curve = [0.1, 0.3, 0.55, 0.8, 1.0]  # Example values
x= [0,1, 2,3, 4]
# Define the corresponding y-axis segments
y_segments = [0.2, 0.4, 0.6, 0.8, 1.0]  # Example values

# Define the maximum value of the x-axis
N = 4 # Example value

# Convert the cumulative curve to a PyTorch tensor
cumulative_curve_tensor = torch.tensor(cumulative_curve)

# Find the corresponding x-axis segments for each scaled y-axis segment

x_seg = np.interp(y_segments, np.array(cumulative_curve ), np.array(x) )




plt.figure()
plt.plot(cumulative_curve_tensor.numpy().T,label = 'tanh-allJ')
for xc in y_segments:
    print('xc = ',xc)
    plt.axhline(y=xc, color='gray', linestyle='--')  

for yc in list(x_seg):
    print('yc= ', yc)
    
    plt.axvline(x=yc, color='gray', linestyle='--')
print("Corresponding x-axis segments:", x_seg)

This code solved my problem. If anybody needs you also can use this one.

huangapple
  • 本文由 发表于 2023年6月12日 17:34:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定