【问题标题】:how to get equation for nonlinear mutivariate regression in which one variable is dependent on other two independent variables in python如何获得非线性多元回归方程,其中一个变量依赖于python中的其他两个自变量
【发布时间】:2016-10-18 06:15:03
【问题描述】:

我有 5000 个 like_so_ (x,y,z) 的数据点,例如 (0,1,50) 其中 x=1,y=2,z=120。在这 5000 个输入的帮助下,我必须得到一个方程
给定 x 和 y ,方程应该能够得到 z 的值

【问题讨论】:

  • 为什么不显示你到目前为止所拥有的并指出你被困在哪里?
  • 我尝试在 python 中使用 pyplot 函数,但它只适用于有一个因变量和一个自变量的数据集,即二维表面
  • 但我们的数据集是 3 个值而不是两个值,即它是多变量的

标签: python numpy pandas dataframe machine-learning


【解决方案1】:

您可以使用statsmodels.ols。一些示例数据 - 假设您可以从您的 (x, y, z) 数据创建一个 pd.DataFrame

import pandas as pd
df = pd.DataFrame(np.random.randint(100, size=(150, 3)), columns=list('XYZ'))
df.info()

RangeIndex: 150 entries, 0 to 149
Data columns (total 3 columns):
X    150 non-null int64
Y    150 non-null int64
Z    150 non-null int64

现在估计线性回归参数:

import numpy as np
import statsmodels.api as sm

model = sm.OLS(df['Z'], df[['X', 'Y']])
results = model.fit()

得到:

results.summary())

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      Z   R-squared:                       0.652
Model:                            OLS   Adj. R-squared:                  0.647
Method:                 Least Squares   F-statistic:                     138.6
Date:                Fri, 17 Jun 2016   Prob (F-statistic):           1.21e-34
Time:                        13:48:38   Log-Likelihood:                -741.94
No. Observations:                 150   AIC:                             1488.
Df Residuals:                     148   BIC:                             1494.
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
X              0.5224      0.076      6.874      0.000         0.372     0.673
Y              0.3531      0.076      4.667      0.000         0.204     0.503
==============================================================================
Omnibus:                        5.869   Durbin-Watson:                   1.921
Prob(Omnibus):                  0.053   Jarque-Bera (JB):                2.990
Skew:                          -0.000   Prob(JB):                        0.224
Kurtosis:                       2.308   Cond. No.                         2.70
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

预测,使用:

params = results.params
params = results.params
df['predictions'] = model.predict(params)

产生:

    X   Y   Z  predictions
0  31  85  75    54.701830
1  36  46  43    34.828605
2  77  42   8    43.795386
3  78  84  65    66.932761
4  27  54  50    36.737606

【讨论】:

  • 我需要使用非线性回归,但数据集是非线性的
  • 如果你的特征和你的输出之间的关系是非线性的,你也可以转换 - 使用例如具有特征或输出的平方、平方根或三角函数,并通过上述ols 过程使用转换后的值。但最终,您需要提供更多信息以获得帮助 - 例如,请参见此处非线性关系可以采用的各种形式:stats.stackexchange.com/questions/148638/…
猜你喜欢
  • 1970-01-01
  • 2021-05-20
  • 1970-01-01
  • 2019-04-16
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 2011-02-26
  • 2019-05-19
相关资源
最近更新 更多