简体   繁体   English

Python多变量非线性回归计算

[英]Python multivariable nonlinear regression calculation

i was trying to make a curve fit of data and want to find nonlinear regression equation.我试图对数据进行曲线拟合,并想找到非线性回归方程。

在此处输入图像描述

thats what my plot looks like i got x,y data which will be my reference data, then i got x0 and y0 which will be my second point, dx and dy will be difference between them when i show this as vector it showed form of这就是我的情节看起来的样子

在此处输入图像描述

when i convert dx,dy to R and theta it showed x^2+y^2 form, is it possible to find those equation with it?当我将 dx,dy 转换为 R 和 theta 时,它显示 x^2+y^2 形式,是否可以用它找到这些方程? here's my current code这是我当前的代码

import matplotlib.pyplot as plt
import numpy as np
import math
import seaborn as sns
from statsmodels.formula.api import ols
from mpl_toolkits.mplot3d import Axes3D
from scipy.interpolate import griddata
"""setting dpi for graph shown in editor"""
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 300

import pandas as pd
"""reading data from excel sheet 1"""
df = pd.read_excel(r'C:\Users\JRKIM\Desktop\data\2513data.xlsx')

"""variable selection"""

tx_0 = df.loc[:,'TRUE_x_0']
ty_0 = df.loc[:,'TRUE_y_0']

v_x_0 = df.loc[:,'vx']
v_y_0 = df.loc[:,'vy']

dx0_0 = tx_0-v_x_0
dy0_0 = ty_0-v_y_0

dr0_0 = df.loc[:,'dr']




fig1, ax0 = plt.subplots()
ax0.set_title("delta0 in vector")

qk = ax0.quiver(tx_0,ty_0,dx0_0,dy0_0)
ax0.scatter(tx_0, ty_0, color='r', s=1)

"""3d graph with vector and position """

fig4 = plt.figure()
ax4 = fig4.add_subplot(111, projection='3d')

ax4.scatter(tx_0, ty_0, dr0_0, marker='*',linewidth = 0.01, cmap="jet")

ax4.set_xlabel('X Label')
ax4.set_ylabel('Y Label')
ax4.set_zlabel('dr Label')

you can use scipy.optimize.curve_fit as follows.你可以使用scipy.optimize.curve_fit如下。

from scipy.optimize import curve_fit

def quadratic_function(x, a, b):
    return a * x[0]**2 + b * x[1]**2 # + c * x[0]*x[1] if you want to satisfy quadratic form

xdata = np.vstack([np.array(dx0_0).flatten(),np.array(dy0_0).flatten()])
ydata = np.array(dr0_0).flatten()
popt, pcov = curve_fit(quadratic_function,xdata,ydata)
print(popt) # values for a and b

this should get you the coefficients for a and b for the least squares fitting of a*x**2 + b*y**2 = r , you have to further calculate the error and see if the error is low enough for your liking.这应该为您提供a*x**2 + b*y**2 = r的最小二乘拟合的 a 和 b 的系数,您必须进一步计算误差并查看误差是否足够低以满足您的喜好.

Edit: corrected the dimensions of inputs编辑:更正了输入的尺寸

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM