Ruční rekurzivní eliminace příznaků
Teď, když máme klasifikátor diabetu, zkusíme snížit počet příznaků, aniž bychom výrazně poškodili přesnost modelu.
Na druhém řádku kódu se vybírají příznaky z původního DataFrame. Tuto selekcí upravíš.
Instance StandardScaler() je předem definována jako scaler a instance LogisticRegression() jako lr.
Všechny potřebné funkce a balíčky jsou také předem načteny.
Toto cvičení je součástí kurzu
Redukce dimenzionality v Pythonu
Interaktivní cvičení na vyzkoušení si v praxi
Vyzkoušejte si toto cvičení dokončením tohoto ukázkového kódu.
# 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))))