开始使用免费开始使用

对已缩放数据进行 KNN

在未缩放的 wine 数据集上,准确率还算不错。但让我们看看通过标准化能获得什么提升。knn 模型以及 Xy 的数据和标签集已经为您创建好。

本练习是课程的一部分

Python 中的机器学习预处理

查看课程

练习说明

  • 创建 StandardScaler(),并将其保存到名为 scaler 的变量中。
  • 缩放训练集和测试集的特征,注意避免引入数据泄漏
  • knn 模型拟合到已缩放的训练数据上。
  • 通过计算测试集准确率来评估模型表现。

交互式实操练习

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

X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42)

# Instantiate a StandardScaler
scaler = ____

# Scale the training and test features
X_train_scaled = ____.____(____)
X_test_scaled = ____.____(____)

# Fit the k-nearest neighbors model to the training data
____.____(____, ____)

# Score the model on the test data
print(____.____(____, ____))
编辑并运行代码