簡體   English   中英

如何獲得非線性多元回歸的方程,其中一個變量依賴於python中的其他兩個獨立變量

[英]how to get equation for nonlinear mutivariate regression in which one variable is dependent on other two independent variables in python

我為like_so_(x,y,z)設置了5000個數據點,例如(0,1,50),其中x = 1,y = 2,z = 120.在這5000個腸道的幫助下,我必須得到一個方程式
給定x和y,方程應該能夠得到z的值

您可以使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM