← 返回题库
初级

模拟数据向前逐步选择

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
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]
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码