시작하기무료로 시작하기

수동 Recursive Feature Elimination

이제 당뇨병 분류기를 만들었으니, 모델의 정확도를 크게 떨어뜨리지 않으면서 사용할 특성 수를 줄일 수 있는지 살펴보겠습니다.

두 번째 코드 줄에서 원래 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))))
코드 편집 및 실행