手動による再帰的特徴量削減(RFE)
糖尿病の分類器を作成したので、モデルの精度をあまり落とさずに、特徴量の数を減らせるか試してみましょう。
2行目のコードでは、元のDataFrameから特徴量を選択しています。この選択を調整してください。
StandardScaler() のインスタンスは scaler、LogisticRegression() のインスタンスは lr としてあらかじめ定義されています。
必要な関数とパッケージはすべて読み込まれています。
この演習はコースの一部です
Pythonで学ぶ次元削減
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Remove the feature with the lowest model coefficient
X = diabetes_df[['pregnant', 'glucose', 'diastolic', 'triceps', 'insulin', 'bmi', 'family', 'age']]
# Performs a 25-75% train test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
# Scales features and fits the logistic regression model
lr.fit(scaler.fit_transform(X_train), y_train)
# Calculates the accuracy on the test set and prints coefficients
acc = accuracy_score(y_test, lr.predict(scaler.transform(X_test)))
print(f"{acc:.1%} accuracy on test set.")
print(dict(zip(X.columns, abs(lr.coef_[0]).round(2))))