首页 > 解决方案 > 使用 AIC 标准 Python 逐步回归

问题描述

我正在使用 AIC 标准在 Python 中进行逐步回归。我想识别函数 y = 2+3sinx+lnx,其基函数为 {1,x,x^2,...,cosx,sinx}。我已经完成了programm,但它运行得不太好,谁能告诉我为什么?

n = 500
x = np.random.uniform(low=1, high=10, size=n)
x1 = np.sort(x)
e = np.random.normal(0, 0.5, n)
y = 2+3*np.sin(x1)+np.log(x1) #noise
y2 = y +e


basic_func = {'1': x1/x1,
              'x': x1,
              'x^2': x1**2,
              'x^3': x1**3,
              'x^4': x1**4,
              'sinx': np.sin(x1),
              'cosx': np.cos(x1),
              'tanx': np.tan(x1),
              'logx': np.log(x1)}
X = pd.DataFrame(basic_func,columns=['1','x', 'x^2', 'x^3', 'x^4', 'sinx', 'cosx', 'tanx', 'logx'])
X2 = X

k = len(X2.columns)
lr = LinearRegression()

while 1:
    aic = []
    if len(X2.columns) > 2:
        for i in range(k):
            lr.fit(X2.drop(X2.columns[i],axis=1).iloc[:], y2)
            y1 = lr.predict(X2.drop(X2.columns[i],axis=1).iloc[:])
            aic.append(n*np.log(1/n*np.sum((y2-y1)**2)) + 2 * (k-1))
    else:
        for i in range(k):
            lr.fit(X2.drop(X2.columns[i], axis=1).iloc[:].values.reshape(-1, 1), y2)
            y1 = lr.predict(X2.drop(X2.columns[i], axis=1).iloc[:].values.reshape(-1, 1))
            aic.append(n * np.log(1 / n * np.sum((y2 - y1) ** 2)) + 2 * (k-1))

  lr.fit(X2.iloc[:], y2)
    y3 = lr.predict(X2.iloc[:])
    aic.append(n * np.log(1 / n * np.sum((y2 - y3) ** 2)) + 2 * k)
    aic_min = min(aic)
    r = aic.index(aic_min)

    if aic[-1] == min(aic):
        break

    k = k-1
    X2 = X2.drop(X2.columns[r],axis=1)

标签: pythonpandaslinear-regression

解决方案


推荐阅读