الماشين ليرنينج · AI216

الـ K-Fold والـ Stratified K-Fold Cross-Validation

الماشين ليرنينج شروحات ومراجعات

الـ K-fold cross-validation بيقسم الـ training set لـ K فولدز، بيدرّب على K-1 ويقيّم على الفولد الباقي، ويكرر K مرة عشان كل فولد يتقيّم مرة.

قياس الـ model على نفس البيانات اللي اتدرب عليها بيدي نتيجة متفائلة. الـ K-fold بيدي تقدير أحسن: قسّم الـ training set لـ K فولدز، دَرّب على K-1 وقيّم على الفولد المتبقي، K مرة، وبعدين خد متوسط الـ K نتيجة. في Scikit-Learn الـ cross_val_score بيرجّع الـ scores لكل فولد، والـ cross_val_predict بيرجّع التوقعات لكل فولد وهو متشال من التدريب.

الـ StratifiedKFold بيعمل stratified sampling: كل فولد بيحافظ على نفس نسب الـ classes زي الداتا كلها. الـ KFold العادي مش بيضمن ده فلو الـ classes مش متوازنة ممكن فولد يطلع فيه نادر جدًا من الـ class القليل. الـ stratified هو الافتراضي الصح للـ classification.

لاحظ إن الـ test set بيفضل متلمّسش لحد الآخر. الـ cross-validation بيشتغل على الـ training set بس.

أهم النقاط

  • K فولدز: K-1 تدريب وواحد تقييم وتكرر K مرة.
  • متوسط الـ K نتيجة تقدير أوثق.
  • الـ stratified بيحافظ على نسب الـ classes في كل فولد.
  • cross_val_score بيرجّع scores وcross_val_predict بيرجّع predictions.
  • متلمسش الـ test set أثناء الـ cross-validation.

مثال محلول

في 5-fold cross-validation على 10,000 صورة تدريب، كل جولة بتدرّب على 8,000 وتقيّم على 2,000. بعد 5 جولات عندك 5 أرقام accuracy وبتاخد متوسطهم.

أسئلة شائعة

إيه الفرق بين StratifiedKFold وKFold؟

الـ StratifiedKFold بيحافظ على نسب الـ classes في كل فولد، والـ KFold العادي بيقسم من غير ما يهتم بتوازن الـ classes.

cross_val_predict بيستخدم في إيه؟

بيرجّع توقع لكل instance من model ماتدربش عليها، ومفيد في بناء الـ confusion matrix.

أسئلة تدريب على الموضوع ده

جرّبها بنفسك، وبعدين افتح التطبيق وشوف الإجابة والشرح.

  1. ....................performs K-fold cross-validation and returns the predictions made on each test fold
    • A. cross_val_predict()
    • B. cross_val_score()
    • C. cross_test_predict()
    • D. none of them
  2. Which of the following is TRUE about the StratifiedKFold class
    • A. It produces folds that contain a representative ratio of each class in the dataset
    • B. It is a cross-validation method designed specifically for regression problems
    • C. It always guarantees higher accuracy on imbalanced datasets
    • D. It is the same as regular KFold
  3. What is the difference between StratifiedKFold and regular KFold cross-validation
    • A. StratifiedKFold performs stratified sampling, while regular KFold does not
    • B. StratifiedKFold is always slower than regular Kfold
    • C. StratifiedKFold is only useful for imbalanced datasets.
    • D. There is no difference between StratifiedKFold and regular Kfold
  4. Just like the ....................... function, ....................performs K-fold cross-validation, but instead of returning the evaluation scores, it returns the predictions made on each test fold.
    • A. cross_val_score(), cross_val_predict()
    • B. cross_val_predict() , cross_val_score()
    • C. cross_val_test() , cross_val_score()
    • D. none of them
  5. ..........................performs K-fold cross-validation and return the evaluation scores
    • A. cross_val_score()
    • B. cross_val_predict()
    • C. cross_test_predict()
    • D. none of them
  6. K-fold cross-validation means splitting the testing set into K folds, then training them on each fold using a model trained on the remaining folds.
    • A. false
    • B. true