多项式回归预测值作为数据框 (Python)

Polynomial regression predicted values as dataframe (Python)

关于这个问题回答了很多,但是,我想不通一件事。

我有一个数据框,我正在执行回归,之后结果存储在 Test 数据框的新列中。为了比较方法,我需要同时进行线性和多项式回归。

我找到了一种通过线性回归完美地做到这一点的方法,结果我在数据框的新列中预测了值 Test。但是我不能使用多项式回归在同一个循环中完成这项工作,因为在最终的 Test 数据帧中我有多个 Null 值,因为在 model_2.fit_transform(X) 值的步骤中以某种方式丢失了相应的 Test 索引.

import pandas as pd
import statsmodels.api as sm
from sklearn.preprocessing import PolynomialFeatures

Test = pd.read_csv(r'D:\myfile.csv')

df_coef =[]
value = list(set(Test['Value']))
for value in value:
    df_redux = Test[Test['Value'] == value]

    Y = df_redux['Y']
    X = df_redux[['X1', 'A', 'B', 'B']]
    X = sm.add_constant(X)

    # linear
    model_1 = sm.OLS(Y, X).fit()
    predictions_1 = model_1.predict(X)

    # polynomial
    polynomial_features = PolynomialFeatures(degree=2)
    xp = polynomial_features.fit_transform(X)
    model_2 = sm.OLS(Y, xp).fit()
    predictions_2 = model_2.predict(xp)

    stats_1 = pd.read_html(model_1.summary().tables[1].as_html(), header=0, index_col=0)[0]
    stats_2 = pd.read_html(model_2.summary().tables[1].as_html(), header=0, index_col=0)[0]

    predictions_1 = pd.DataFrame(predictions_1, columns=['lin'])
    predictions_2 = pd.DataFrame(predictions_2, columns=['poly'])

    # ??? how to concat and appen both prediction_1 and prediction_2 in the same df_coef = [] dataframe?
    gf = pd.concat([predictions_1, df_redux], axis=1)
    df_coef.append(gf)

all_coef = pd.concat(df_coef)

type(all_coef)
Out[234]: pandas.core.frame.DataFrame

问题是转换后的 xp 类型是 <class 'numpy.ndarray'>,但 X 类型是 <class 'pandas.core.frame.DataFrame'>。问题是如何在 linear reg 旁边的 Test 的新列中获得多项式回归预测值。结果。这可能真的很简单,但我想不通。

print(type(X))
print(type(xp))
print(X.sample(2))
print()
print(xp)
<class 'pandas.core.frame.DataFrame'>
<class 'numpy.ndarray'>
      X1         A          B          G1
962    4.334912  1.945910  3.135494  3.258097
1365   4.197888  2.197225  3.135494  3.332205
[[ 1.          4.77041663  1.94591015 ... 35.74106743 34.52550933
  33.35129251]
 [ 1.          4.43240629  1.94591015 ... 33.28387641 32.03140262
  30.82605947]
 [ 1.          3.21669428  1.94591015 ... 29.95821572 30.38903979
  30.82605947]

我用多项式 reg 得到的结果。附加到原始测试数据帧的预测值:

0     6.178542     3.0  692  ...  2.079442  4.783216  6.146329
1     6.156108    11.0  692  ...  2.197225  4.842126  6.113682
2     6.071453    12.0  692  ...  2.197225  4.814595  6.052089
3     5.842053     NaN        NaN  ...       NaN       NaN       NaN
4     4.625762    30.0  692  ...  1.945910  5.018201  5.828946

这是我仅使用线性回归获得的正确且良好的结果,没有 Nan 并且每一行都有值,它应该是这样的:

0     6.151675     3  692  5  ...  3.433987  2.079442  4.783216  6.146329
1     6.132077    11  692  5  ...  3.401197  2.197225  4.842126  6.113682
2     6.068450    12  692  5  ...  3.332205  2.197225  4.814595  6.052089
4     5.819535    30  692  5  ...  3.258097  1.945910  5.018201  5.828946
8     4.761362    61  692  5  ...  2.564949  1.945910  3.889585  4.624973

通过为 numpy 到系列转换添加一行来解决这个问题。对于模型统计 statsmodels 摘要:

import pandas as pd
from statsmodels.api import OLS

predictions_2 = model_2.predict(xp)
predictions_2_series = pd.Series(predictions_2, index=df_redux.index.values)

print(OLS(Y, xp).fit().summary())