python - 使用 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)
解决方案
推荐阅读
- ruby-on-rails-4 - 什么服务器规格足以满足我网站上 10,000 个并发用户的请求
- json - 如何在bash中使用jq过滤器过滤键和值
- javascript - forEach 不是函数并且 for...of 不是可迭代的
- python - 模拟 Python 对象实例化
- windows - 获取“Get-ChildItem:不允许请求的注册表访问”错误
- amazon-web-services - EC2 积分 - t2
- mysql - MySQL 使用触发器基于另一个表更新列
- html - 带有独立可滚动列的 Bootstrap 4 全高卡片
- java - Java计算具有不同字段的类对象
- c++ - 当满足两个条件时,如何在 C++ 中停止 For 循环?