開始使用免費開始

起始模型

在這個練習中,你會在第 1 章使用的影像資料集上建立一個 MLP 分類器。提醒一下,每張影像都代表數字 09,目標是將每張影像分類為正確的數字。所用的特徵是組成影像的像素值,範圍為 0–16。將特徵標準化後,你會在測試集上評估分類器的準確率。

在你的工作環境中,已載入以 DataFrame 形式提供的影像範例資料 image_data,以及 sklearn 和作為 pdpandassklearn.preprocessing 中的 StandardScaler() 也可使用。

本練習屬於課程

用 Python 透過機器學習預測 CTR

檢視課程

練習說明

  • 使用 .fit_transform() 對特徵做標準化,並使用 train_test_split() 將資料分成訓練集與測試集。
  • 建立一個 MLP 分類器。
  • 使用分類器產生預測,並用 accuracy_score() 評估準確率。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Define X and y
X, y = image_data.data, image_data.target

# Scale features and split into training and testing
X_scaled = ____().____(X)
X_train, X_test, y_train, y_test = ____(
  X_scaled, y, test_size = .2, random_state = 0)

# Create classifier, train and evaluate accuracy 
clf = ____()
y_pred = clf.____(X_train, y_train).____(X_test)
print(____(y_test, y_pred))
編輯並執行程式碼