初级
模拟数据向前逐步选择
未完成
初级参考
完整示例代码供参考,建议自己理解后重新输入
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
np.random.seed(1)
X = np.random.randn(100)
eps = np.random.randn(100)
Y = 3 + 4*X + 2*X**2 + X**3 + eps
poly = PolynomialFeatures(10, include_bias=False)
X_poly = poly.fit_transform(X.reshape(-1, 1))
def forward_selection(X, y):
remaining = list(range(X.shape[1]))
selected = []
while remaining:
scores = []
for candidate in remaining:
features = selected + [candidate]
model = LinearRegression().fit(X[:, features], y)
scores.append((model.score(X[:, features], y), candidate))
scores.sort()
best_score, best_candidate = scores[-1]
remaining.remove(best_candidate)
selected.append(best_candidate)
return selected
print('向前逐步选择结果:', forward_selection(X_poly, Y))
示例
输入
solve()
期望输出
向前逐步选择结果: [0, 1, 2, 5, 9, 3, 7, 8, 6, 4]
👑
升级 VIP
解锁全部题目,畅通无阻地学习
- ✓ 解锁全部训练包所有题目
- ✓ 查看完整参考代码和提示
- ✓ 浏览器内直接运行 Python 代码
- ✓ 自动批改 + 进度追踪
30天
¥18
1年
¥99
2年
¥158
3年
¥199