เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การคัดเลือกฟีเจอร์แบบ Recursive ด้วยตนเอง

หลังจากสร้างตัวจำแนกโรคเบาหวานแล้ว มาลองดูกันว่าจะลดจำนวนฟีเจอร์ลงได้โดยไม่กระทบความแม่นยำของโมเดลมากนักได้อย่างไร

ในบรรทัดที่สองของโค้ด ฟีเจอร์จะถูกเลือกจาก 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))))
แก้ไขและรันโค้ด