开始使用免费开始使用

构建糖尿病分类器

您将使用 Pima Indians 糖尿病数据集,通过逻辑回归预测某人是否患有糖尿病。该数据集包含 8 个特征和 1 个目标变量。数据已被划分为训练集和测试集,并以 X_trainy_trainX_testy_test 的形式为您预加载。

StandardScaler() 实例已预定义为 scalerLogisticRegression() 实例已预定义为 lr

本练习是课程的一部分

Python 中的降维

查看课程

练习说明

  • 在训练特征上拟合标准化器,并一次性转换这些特征。
  • 在已缩放的训练数据上拟合逻辑回归模型。
  • 对测试特征进行缩放。
  • 在已缩放的测试集上预测是否患有糖尿病。

交互式实操练习

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

# Fit the scaler on the training features and transform these in one go
X_train_std = scaler.____(____)

# Fit the logistic regression model on the scaled training data
lr.____(____, ____)

# Scale the test features
X_test_std = scaler.____(____)

# Predict diabetes presence on the scaled test set
y_pred = lr.____(____)

# Prints accuracy metrics and feature coefficients
print(f"{accuracy_score(y_test, y_pred):.1%} accuracy on test set.")
print(dict(zip(X.columns, abs(lr.coef_[0]).round(2))))
编辑并运行代码