如何将函数拟合到Python中的数据

发布于 2025-02-06 18:06:43 字数 1261 浏览 2 评论 0原文

我想将功能适合独立(x)和依赖(y)变量:

import numpy as np
y = np.array([1.45952016, 1.36947283, 1.31433227, 1.24076599, 1.20577963,
       1.14454815, 1.13068077, 1.09638278, 1.08121406, 1.04417094,
       1.02251471, 1.01268524, 0.98535659, 0.97400591])
X = np.array([4.571428571362048, 8.771428571548313, 12.404761904850602, 17.904761904850602,
            22.904761904850602, 31.238095237873495, 37.95833333302289, 
            44.67857142863795, 51.39880952378735, 64.83928571408615, 
            71.5595238097012, 85., 98.55357142863795, 112.1071428572759])

我已经以这种方式尝试了Scipy软件包:

from scipy.optimize import curve_fit
def func (x, a, b, c):
    return 1/(a*(x**2) + b*(x**1) + c)
g = [1, 1, 1]
c, cov = curve_fit (func, X.flatten(), y.flatten(), g)
test_ar = np.arange(min(X), max(X), 0.25)
pred = np.empty(len(test_ar))
for i in range (len(test_ar)):
    pred[i] = func(test_ar[i], c[0], c[1], c[2])

我可以将更高的多项式订单添加到使我的func更加准确,但我想保持简单。如果有人向我提供有关如何找到其他功能或使我的预测更好的帮助,我非常感谢。该图还显示了预测的结果:

”在此处输入图像说明”

I want to fit a function to the independant (X) and dependent (y) variables:

import numpy as np
y = np.array([1.45952016, 1.36947283, 1.31433227, 1.24076599, 1.20577963,
       1.14454815, 1.13068077, 1.09638278, 1.08121406, 1.04417094,
       1.02251471, 1.01268524, 0.98535659, 0.97400591])
X = np.array([4.571428571362048, 8.771428571548313, 12.404761904850602, 17.904761904850602,
            22.904761904850602, 31.238095237873495, 37.95833333302289, 
            44.67857142863795, 51.39880952378735, 64.83928571408615, 
            71.5595238097012, 85., 98.55357142863795, 112.1071428572759])

I already tried scipy package in this way:

from scipy.optimize import curve_fit
def func (x, a, b, c):
    return 1/(a*(x**2) + b*(x**1) + c)
g = [1, 1, 1]
c, cov = curve_fit (func, X.flatten(), y.flatten(), g)
test_ar = np.arange(min(X), max(X), 0.25)
pred = np.empty(len(test_ar))
for i in range (len(test_ar)):
    pred[i] = func(test_ar[i], c[0], c[1], c[2])

I can add higher orders of polynomial to make my func more accurate but I want to keep it simple. I very much appreciate if anyone an give me some help on how to find another function or make my prediction better. The figure also shows the result of the prediction:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这个俗人 2025-02-13 18:06:43

您要做的第一件事是指定如何测量“准确性”,而在您的情况下,这根本不是一个合适的术语。

您本质上在做什么称为线性回归。在这种情况下,合适的指标是平均平方误差(MSE),均方根误差(RMSE),平均绝对误差(MAE)。由您决定要使用哪种指标以及设定“可接受”的阈值。

您上面显示的图像(已安装了生产线)看起来不错,但请从-100扩展到300,然后再次向我们展示图像这是高度多项式的问题

这是101个示例在scikit-learn中如何使用回归。在您的情况下,如果您想使用x^2或x^3进行预测y,则只需要将它们添加到数据中...当前您的x变量是一个数组(vector),您需要扩展它以使其成为每个列是一个功能(x,x^2,x^3 ...)的矩阵,

这是一些代码:

import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error, r2_score

y = [1.45952016, 1.36947283, 1.31433227, 1.24076599, 
 1.20577963, 1.14454815, 1.13068077, 1.09638278, 
 1.08121406, 1.04417094, 1.02251471, 1.01268524, 0.98535659, 
 0.97400591]

x = [4.571428571362048, 8.771428571548313, 12.404761904850602, 
 17.904761904850602, 22.904761904850602, 31.238095237873495,
 37.95833333302289, 44.67857142863795, 51.39880952378735, 
 64.83928571408615, 71.5595238097012, 85., 98.55357142863795, 112.1071428572759]

df = pd.DataFrame({
    'x' : x,
    'x^2': [i**2 for i in x],
    'x^3': [i**3 for i in x],
    'y': y
})

X = df[['x','x^2','x^3']]
y = df['y']

model = linear_model.LinearRegression()
model.fit(X, y)
y1 = model.predict(X)

coef = model.coef_
intercept = model.intercept_

“

您可以从coef coef 变量中看到系数

array([-1.67456732e-02,  2.03899728e-04, -8.70976426e-07])

您可以看到intercept变量的拦截:

1.5042389677980577

在您的情况下,这意味着 - > y1 = -1.67e-2x + 2.03e-4x^2 -8.70e-7x^3 + 1.5

First thing you want to do is to specify how do you measure "accuracy" which in your case is not an appropriate term at all.

What are you essentially doing is called linear regression. Suitable metrics in this case are mean squared error (MSE), root mean squared error (RMSE), mean absolute error (MAE). It is up to you to decide which metric to use and what threshold to set for being "acceptable".

The image that you are showing above (where you've fitted the line) looks fine BUT please expand your X-axis from -100 to 300 and show us the image again this is a problem with high degree polynomials.

This is a 101 example how to use regression in scikit-learn. In your case if you want to use x^2 or x^3 for predicting y, you just need to add them in to the data ... Currently your X variable is an array (a vector) you need to expand that to become a matrix where each column is a feature (x, x^2, x^3 ...)

here is some code:

import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error, r2_score

y = [1.45952016, 1.36947283, 1.31433227, 1.24076599, 
 1.20577963, 1.14454815, 1.13068077, 1.09638278, 
 1.08121406, 1.04417094, 1.02251471, 1.01268524, 0.98535659, 
 0.97400591]

x = [4.571428571362048, 8.771428571548313, 12.404761904850602, 
 17.904761904850602, 22.904761904850602, 31.238095237873495,
 37.95833333302289, 44.67857142863795, 51.39880952378735, 
 64.83928571408615, 71.5595238097012, 85., 98.55357142863795, 112.1071428572759]

df = pd.DataFrame({
    'x' : x,
    'x^2': [i**2 for i in x],
    'x^3': [i**3 for i in x],
    'y': y
})

X = df[['x','x^2','x^3']]
y = df['y']

model = linear_model.LinearRegression()
model.fit(X, y)
y1 = model.predict(X)

coef = model.coef_
intercept = model.intercept_

linear regression

you can see the coefficients from the coef variable:

array([-1.67456732e-02,  2.03899728e-04, -8.70976426e-07])

you can see the intercept from the intercept variable:

1.5042389677980577

which in your case means -> y1 = -1.67e-2x +2.03e-4x^2 -8.70e-7x^3 + 1.5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文