开始使用免费开始使用

自动递归特征消除

现在让我们把这个递归过程自动化。请将递归特征消除器(RFE)包装在逻辑回归估计器外,并传入期望保留的特征数量。

所需的函数和包都已为您预加载,特征也已完成缩放。

本练习是课程的一部分

Python 中的降维

查看课程

练习说明

  • 使用 LogisticRegression() 作为估计器创建 RFE,并设置选择 3 个特征。
  • 打印各特征及其排名。
  • 打印未被消除的特征。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Create the RFE with a LogisticRegression estimator and 3 features to select
rfe = ____(estimator=____, n_features_to_select=____, verbose=1)

# Fits the eliminator to the data
rfe.fit(X_train, y_train)

# Print the features and their ranking (high = dropped early on)
print(dict(zip(X.columns, rfe.____)))

# Print the features that are not eliminated
print(X.columns[rfe.____])

# Calculates the test set accuracy
acc = accuracy_score(y_test, rfe.predict(X_test))
print(f"{acc:.1%} accuracy on test set.") 
编辑并运行代码