[英]Finding an accurate mathmatical function from a plot/ 2d array?
我将如何从图形/二维数组中找到数学函数? 例如
line = [0,1,2,3,4,5,6,7,8,9,10]
print(findfunction(line))
>y=x
line2 =[5,7,9,11,13,15,17,19]
print(findfunction(line2))
>y=2x+5
对多项式、指数和介于两者之间的所有事物,依此类推。
我了解某些行可能没有函数**,或者我可能必须将其分解为范围以获得与函数非常相似的任何内容,但我想不出如何做到这一点,除了蛮力但那个剂量似乎不可靠。
**没有功能? 有点道理,有一个函数可以描述每条可能的线/曲线,对吧?
对于线性方程,您可以简单地使用 NumPy 的 polyfit。 假设您提到的线阵列的值 x = 1,2,3,4... 我们可以执行以下操作
def fit_to_function(x, y):
"""
fit_to_function(x, y)
x: list of x values
y: list of y values
returns: list of y values that fit the function
"""
x, b = np.polyfit(x, y, 1)
return f"y = {x}x + {b}"
测试:
x = [0,1,2,3,4,5,6,7]
y = [5,7,9,11,13,15,17,19]
print(fit_to_function(x, y))
输出:
y = 1.9999999999999998x + 5.000000000000003
可选:如果您想像原来的问题一样对其进行四舍五入
import numpy as np
def fit_to_function(x, y):
"""
fit_to_function(x, y)
x: list of x values
y: list of y values
returns: list of y values that fit the function
"""
x, b = np.polyfit(x, y, 1)
x, b = round(x, 2), round(b, 2)
return f"y = {x}x + {b}"
输出:
y = 2.0x + 5.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.