如何获得非线性多元回归的方程,其中一个变量依赖于python中的其他两个独立变量

时间:2016-06-16 15:59:44

标签: python numpy pandas dataframe machine-learning

我为like_so_(x,y,z)设置了5000个数据点,例如(0,1,50) 其中x = 1,y = 2,z = 120.在这5000个肠道的帮助下,我必须得到一个方程式 给定x和y,方程应该能够得到z的值

1 个答案:

答案 0 :(得分:0)

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

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