gpt4 book ai didi

python - 从 Python 中的趋势线图中提取 y 值

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:44 24 4
gpt4 key购买 nike

有些人如何从下面的源代码生成的红色趋势线中获取 y 值?我不是数学专家。

至于下面的代码来自How to add trendline in python matplotlib dot (scatter) graphs?

#random inputs for x and y


x = np.random.uniform(low=0.5, high=13.3, size=(50,))
y = np.random.uniform(low=0.5, high=13.3, size=(50,))

# plot the data itself
pylab.plot(x,y,'o')

# calc the trendline
z = numpy.polyfit(x, y, 1)
p = numpy.poly1d(z)
pylab.plot(x,p(x),"r--")
# the line equation:
print "y=%.6fx+(%.6f)"%(z[0],z[1])

当我打印 p(x) 的值时,y 的预期值绘制红色趋势线。

[7.25072088 7.74580974 7.707636   7.57456601 7.72771792 7.36682509
7.36216195 7.45937086 7.47592622 7.76663313 7.71256734 7.68601844
7.34777885 7.2552914 7.28729136 7.4828444 7.25690455 7.47861776
7.48472596 7.63791435 7.79364877 7.79382845 7.45020348 7.5488981
7.29478413 7.27191799 7.47409563 7.26783249 7.49132469 7.2515923
7.40558937 7.55062512 7.46004735 7.4094514 7.69985713 7.23891764
7.50790404 7.38789488 7.23477781 7.59598148 7.49460819 7.62039958
7.67580303 7.40553616 7.61933389 7.60038837 7.76048006 7.41307834
7.28136679 7.5063726 ]

如果这是向上移动的趋势,数组元素是否应该从头到尾增加?如您所见,有些元素以前的值高于当前值。不应该有一个稳定的倾斜,下一个元素总是比前一个元素高吗?叫我困惑。

最佳答案

Should there not be a steady incline which where the next element would ALWAYS be the higher than the previous element?

是的,拟合是一条直线,因此 x 的较高值总是与 p(x) 的较高(或较低,取决于斜率)值相关联>.

您的情况是 x 未排序,因此 p(x) 也未排序。

In [18]: x
Out[18]:
array([ 9.95692606, 5.25372625, 9.84277793, 9.75691888,
3.53691402, 7.47732635, 13.26638669, 10.39011192,
11.86590794, 10.38592445, 0.5328471 , 7.69932299,
...

如您所见,我们并不是从左侧开始然后向右侧移动。我们首先看中间的某个点,然后向左跳很多,然后向右跳,然后向左移动一点点,等等。相应的 p(x) 值不会去要么是单调的。

如果您从左到右对点进行排序,您会发现它们确实总是沿相同的垂直方向移动:

In [20]: sorted(zip(x, p(x)))
Out[20]:
[(0.53284710066507301, 5.2982022878459842),
(0.90494271648495472, 5.3490731826338447),
(1.2383322417505211, 5.3946523906172272),
(1.2542322226117251, 5.3968261497778585),
(1.3243912128123114, 5.4064179064586044),
(1.4506628234207115, 5.4236810763129437),
(2.0368566039434102, 5.503822311163459),
(2.8349103207704576, 5.6129278876274968),
(3.0174136939304748, 5.637878759123244),
(3.5369140229038196, 5.7089020269444219),
(4.932863919562303, 5.8997487268324766),
(4.943993127936622, 5.9012702518497351),
(4.9500689452818589, 5.9021009046491208),
...

关于python - 从 Python 中的趋势线图中提取 y 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50691339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com