k-fold cross validation with Python code -Output with splits and coressponding performance

lakshmi prabha ramesh
16 min readApr 28, 2022

--

Why do we need cross- validation ?

Every Machine Learning Engineer’s goal is to achieve good performance measures during production phase by the model they build. Two main issues faced at this stage is Overfitting and underfitting.

Overfitting and Underfitting

A good model should make sure the data does not overfit or underfit. That is the model does not capture the noise along with the information(overfitting) or the model does not capture enough information (underfitting). In order to figure this out we divide the dataset in hand into 2 parts Training dataset and Testing dataset.

Source:https://www.educative.io/edpresso/overfitting-and-underfitting

Drawback of dividing dataset into 2 parts

During the model building phase, the data is trained with the training dataset and tested in the testing dataset(the data which is new to the model). But as we model the data through various models and its hyperparameter tunings the test data becomes well known to the model since we keep using it every time for testing. Hence the model starts seeing it as a training dataset and it the fails to show us how the model generalizes on new data.

Solution

So, we next divide the data into 3 parts, training, validation and testing datasets. Here the validation dataset is used for all the evaluations of the hyperparameters. Now, in this case the test set is only finally used after all the evaluations. Once the right model is known, it is tested with the testing data. Thus this the testing data is completely unknown to the model.

Drawback of dividing dataset into 3 parts

When diving the data into 3 parts major patterns may be missed by the training dataset. And also the sample size of the training dataset will be drastically reduced.

Solution -Cross Validation

Here, in Cross Validation we do not divide dataset into 3 parts but only 2 parts training and testing. There is no validation dataset.

One such famous cross- validation is k-fold cross validation

Steps involved in k- fold cross validation

● The data is divided into k-folds in cross-validation.

● Training= k-1 folds are used

● Testing = kth fold is used

● This process continues k times.

● For each fold performance score is calculated and thus we will have k scores

● The average of the scores of all the models is considered as the final model perormance.

● The standard deviation of the scores can be calculated. With this measure it is found with 95% confidence that the model’s performance will be within (avg -2*std. dev., avg + 2*std dev)

Source:http://ethen8181.github.io/machine-learning/model_selection/model_selection.html

How to choose the value for k?

● k must be an integer.

● k has a minimum value of 2 with two iterations.

● Maximum value of k is the number of records in the dataset.

● K can be any value between the min and max value of k.

● k = 10 is a standard usage.

● When k is too large it limits the model differences through the iterations.

● when we have s dataset size, and k = p, per fold records = s/p

Python Code to understand the working of the k-fold cross validation

We will be using the famous pima indians diabetics dataset in Kaggle for demonstrating k-fold cross validation step by step.

Imports

Load view data

K-fold Implementation k=4

We set the performance metric to accuracy. We use LogisticRegression as our model here. For each split we calculate the score and append it in the list my_scores. Below is the clear output. The outout shos two important things

  1. Each split and its coressponding performace score
  2. The mean value of all the scores

Output

Training data index:  [  1   2   4   5   6   7   9  10  11  12  14  15  17  18  20  21  22  24
25 26 27 28 29 30 31 32 33 34 36 37 38 39 42 43 44 45
46 48 49 51 52 53 54 55 58 62 63 64 67 70 71 72 73 75
77 78 79 80 82 83 86 87 88 89 91 93 94 95 96 97 98 99
100 102 103 105 106 107 109 110 112 113 115 116 118 119 121 122 123 124
125 126 127 128 129 130 131 133 136 137 138 140 141 142 143 144 145 146
147 149 150 151 152 153 155 156 157 158 160 162 163 164 165 166 167 168
169 170 171 172 173 174 176 177 178 179 182 183 184 186 188 190 191 192
193 196 197 198 199 200 203 204 205 206 208 209 210 211 212 213 214 215
216 217 219 220 221 222 225 226 227 228 229 230 231 232 234 235 236 237
239 240 241 242 243 244 246 248 249 250 251 252 253 254 256 257 258 259
260 261 263 264 266 267 269 270 271 272 274 275 276 278 279 280 281 282
283 284 287 288 289 290 291 292 293 294 295 296 297 300 301 302 303 304
306 308 309 312 313 314 315 316 317 319 321 322 323 324 325 326 327 328
330 332 333 336 337 338 340 342 343 344 345 346 347 348 351 353 356 357
359 360 361 362 363 364 365 366 367 368 369 370 372 373 374 375 376 377
379 380 381 383 384 386 387 388 390 391 393 395 396 397 398 401 402 405
406 407 409 410 412 413 414 415 416 417 418 419 420 421 422 423 424 425
427 429 431 432 433 434 435 438 439 440 441 443 444 446 447 450 451 453
454 455 456 457 458 460 461 462 464 465 466 467 468 469 470 471 473 474
475 476 477 478 479 481 482 484 485 486 487 488 489 490 491 492 494 495
497 498 499 500 501 503 504 505 506 508 510 511 512 513 514 515 517 519
520 522 523 524 525 526 528 529 531 532 534 536 537 542 543 544 545 547
548 550 551 554 555 557 559 561 562 564 565 566 568 569 570 571 572 573
574 575 576 578 579 580 582 583 584 585 586 587 588 589 592 593 594 595
596 597 598 599 600 601 602 603 604 605 606 608 609 610 612 613 615 616
617 618 619 621 622 623 624 625 626 627 630 632 633 634 636 639 642 644
645 647 648 649 650 651 652 653 654 655 656 657 658 659 660 662 664 665
666 668 671 672 673 675 676 677 678 681 685 686 687 688 690 691 692 694
695 696 697 698 700 702 703 704 706 708 709 710 713 714 715 716 717 718
719 720 721 723 724 725 727 728 729 730 731 732 734 735 736 737 738 739
740 741 743 744 745 746 748 749 751 752 758 759 761 762 763 764 766 767]

Test data index: [ 0 3 8 13 16 19 23 35 40 41 47 50 56 57 59 60 61 65
66 68 69 74 76 81 84 85 90 92 101 104 108 111 114 117 120 132
134 135 139 148 154 159 161 175 180 181 185 187 189 194 195 201 202 207
218 223 224 233 238 245 247 255 262 265 268 273 277 285 286 298 299 305
307 310 311 318 320 329 331 334 335 339 341 349 350 352 354 355 358 371
378 382 385 389 392 394 399 400 403 404 408 411 426 428 430 436 437 442
445 448 449 452 459 463 472 480 483 493 496 502 507 509 516 518 521 527
530 533 535 538 539 540 541 546 549 552 553 556 558 560 563 567 577 581
590 591 607 611 614 620 628 629 631 635 637 638 640 641 643 646 661 663
667 669 670 674 679 680 682 683 684 689 693 699 701 705 707 711 712 722
726 733 742 747 750 753 754 755 756 757 760 765]
0.7760416666666666Training data index: [ 0 1 2 3 6 7 8 10 12 13 15 16 18 19 20 21 22 23
24 25 26 27 28 30 32 35 36 37 40 41 43 44 46 47 48 50
51 53 55 56 57 58 59 60 61 63 64 65 66 68 69 70 71 72
73 74 75 76 77 79 81 83 84 85 86 87 89 90 91 92 93 94
96 98 100 101 104 105 106 108 109 111 112 113 114 115 117 120 122 123
125 126 127 129 130 131 132 134 135 136 137 138 139 140 141 142 143 144
145 148 149 150 151 152 154 155 158 159 161 163 164 166 167 168 169 170
171 174 175 176 178 180 181 182 183 184 185 187 188 189 190 193 194 195
196 198 199 201 202 203 204 206 207 208 209 210 211 212 213 215 217 218
219 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 237 238
239 240 243 244 245 246 247 249 251 252 253 254 255 256 258 262 263 264
265 266 267 268 269 270 271 273 275 276 277 278 279 281 282 283 284 285
286 287 288 290 291 292 294 295 296 297 298 299 300 302 303 305 307 308
310 311 313 315 316 317 318 319 320 321 322 324 325 326 327 328 329 330
331 332 334 335 336 337 338 339 340 341 342 344 348 349 350 351 352 354
355 356 357 358 359 360 362 365 367 369 370 371 376 377 378 380 381 382
383 384 385 387 389 390 391 392 393 394 395 397 398 399 400 401 403 404
405 406 407 408 409 410 411 413 416 417 418 420 423 424 425 426 428 430
431 435 436 437 438 440 441 442 444 445 448 449 450 451 452 453 454 455
456 457 458 459 460 461 462 463 465 466 468 469 470 471 472 475 476 477
478 479 480 483 484 485 488 489 490 492 493 494 496 497 499 500 502 503
504 505 507 508 509 512 513 514 515 516 518 520 521 527 530 532 533 534
535 536 537 538 539 540 541 542 544 545 546 547 548 549 552 553 555 556
557 558 559 560 561 562 563 564 565 566 567 568 569 571 572 573 575 577
579 580 581 582 583 584 586 588 590 591 592 594 595 596 597 606 607 608
609 610 611 613 614 615 616 617 618 619 620 621 622 624 626 627 628 629
630 631 632 633 635 636 637 638 639 640 641 643 644 645 646 647 648 651
652 653 654 655 656 657 658 660 661 663 665 666 667 668 669 670 671 672
673 674 675 676 677 678 679 680 681 682 683 684 686 687 688 689 690 692
693 698 699 701 702 703 705 706 707 708 709 711 712 713 715 716 717 719
721 722 724 726 727 728 733 734 735 737 739 740 741 742 743 744 746 747
748 749 750 751 752 753 754 755 756 757 758 760 762 763 764 765 766 767]

Test data index: [ 4 5 9 11 14 17 29 31 33 34 38 39 42 45 49 52 54 62
67 78 80 82 88 95 97 99 102 103 107 110 116 118 119 121 124 128
133 146 147 153 156 157 160 162 165 172 173 177 179 186 191 192 197 200
205 214 216 220 236 241 242 248 250 257 259 260 261 272 274 280 289 293
301 304 306 309 312 314 323 333 343 345 346 347 353 361 363 364 366 368
372 373 374 375 379 386 388 396 402 412 414 415 419 421 422 427 429 432
433 434 439 443 446 447 464 467 473 474 481 482 486 487 491 495 498 501
506 510 511 517 519 522 523 524 525 526 528 529 531 543 550 551 554 570
574 576 578 585 587 589 593 598 599 600 601 602 603 604 605 612 623 625
634 642 649 650 659 662 664 685 691 694 695 696 697 700 704 710 714 718
720 723 725 729 730 731 732 736 738 745 759 761]
0.7864583333333334Training data index: [ 0 3 4 5 7 8 9 10 11 13 14 15 16 17 19 20 21 22
23 25 26 28 29 31 33 34 35 37 38 39 40 41 42 43 45 47
48 49 50 52 54 55 56 57 59 60 61 62 63 64 65 66 67 68
69 71 72 74 75 76 77 78 80 81 82 84 85 86 87 88 90 92
95 96 97 99 101 102 103 104 107 108 110 111 114 116 117 118 119 120
121 124 126 128 129 130 132 133 134 135 136 137 139 140 141 144 146 147
148 149 151 152 153 154 155 156 157 159 160 161 162 165 166 170 172 173
175 176 177 178 179 180 181 183 185 186 187 189 190 191 192 193 194 195
196 197 198 199 200 201 202 205 207 209 210 212 214 215 216 218 219 220
222 223 224 231 233 234 235 236 238 241 242 243 245 247 248 250 252 253
254 255 257 259 260 261 262 263 264 265 266 268 269 272 273 274 276 277
278 279 280 281 282 285 286 287 288 289 293 296 297 298 299 301 302 303
304 305 306 307 309 310 311 312 313 314 316 317 318 319 320 321 322 323
325 327 329 330 331 332 333 334 335 336 338 339 341 343 345 346 347 348
349 350 352 353 354 355 356 357 358 361 363 364 366 367 368 369 371 372
373 374 375 378 379 381 382 384 385 386 387 388 389 390 392 393 394 396
398 399 400 401 402 403 404 405 408 411 412 413 414 415 416 418 419 420
421 422 424 426 427 428 429 430 431 432 433 434 436 437 439 442 443 445
446 447 448 449 450 451 452 454 456 459 461 462 463 464 466 467 468 469
470 471 472 473 474 476 477 478 480 481 482 483 485 486 487 489 490 491
492 493 495 496 497 498 501 502 503 505 506 507 508 509 510 511 513 514
515 516 517 518 519 521 522 523 524 525 526 527 528 529 530 531 532 533
534 535 536 538 539 540 541 542 543 545 546 547 548 549 550 551 552 553
554 556 557 558 560 561 562 563 564 566 567 568 569 570 572 574 576 577
578 580 581 583 585 586 587 588 589 590 591 593 595 597 598 599 600 601
602 603 604 605 606 607 609 611 612 614 615 616 620 621 623 624 625 626
627 628 629 630 631 633 634 635 637 638 640 641 642 643 645 646 647 648
649 650 653 654 655 656 658 659 661 662 663 664 665 667 668 669 670 674
676 677 678 679 680 681 682 683 684 685 688 689 690 691 693 694 695 696
697 698 699 700 701 704 705 706 707 709 710 711 712 714 715 716 717 718
719 720 721 722 723 724 725 726 728 729 730 731 732 733 735 736 738 740
742 745 747 748 749 750 751 753 754 755 756 757 758 759 760 761 764 765]

Test data index: [ 1 2 6 12 18 24 27 30 32 36 44 46 51 53 58 70 73 79
83 89 91 93 94 98 100 105 106 109 112 113 115 122 123 125 127 131
138 142 143 145 150 158 163 164 167 168 169 171 174 182 184 188 203 204
206 208 211 213 217 221 225 226 227 228 229 230 232 237 239 240 244 246
249 251 256 258 267 270 271 275 283 284 290 291 292 294 295 300 308 315
324 326 328 337 340 342 344 351 359 360 362 365 370 376 377 380 383 391
395 397 406 407 409 410 417 423 425 435 438 440 441 444 453 455 457 458
460 465 475 479 484 488 494 499 500 504 512 520 537 544 555 559 565 571
573 575 579 582 584 592 594 596 608 610 613 617 618 619 622 632 636 639
644 651 652 657 660 666 671 672 673 675 686 687 692 702 703 708 713 727
734 737 739 741 743 744 746 752 762 763 766 767]
0.7864583333333334Training data index: [ 0 1 2 3 4 5 6 8 9 11 12 13 14 16 17 18 19 23
24 27 29 30 31 32 33 34 35 36 38 39 40 41 42 44 45 46
47 49 50 51 52 53 54 56 57 58 59 60 61 62 65 66 67 68
69 70 73 74 76 78 79 80 81 82 83 84 85 88 89 90 91 92
93 94 95 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
112 113 114 115 116 117 118 119 120 121 122 123 124 125 127 128 131 132
133 134 135 138 139 142 143 145 146 147 148 150 153 154 156 157 158 159
160 161 162 163 164 165 167 168 169 171 172 173 174 175 177 179 180 181
182 184 185 186 187 188 189 191 192 194 195 197 200 201 202 203 204 205
206 207 208 211 213 214 216 217 218 220 221 223 224 225 226 227 228 229
230 232 233 236 237 238 239 240 241 242 244 245 246 247 248 249 250 251
255 256 257 258 259 260 261 262 265 267 268 270 271 272 273 274 275 277
280 283 284 285 286 289 290 291 292 293 294 295 298 299 300 301 304 305
306 307 308 309 310 311 312 314 315 318 320 323 324 326 328 329 331 333
334 335 337 339 340 341 342 343 344 345 346 347 349 350 351 352 353 354
355 358 359 360 361 362 363 364 365 366 368 370 371 372 373 374 375 376
377 378 379 380 382 383 385 386 388 389 391 392 394 395 396 397 399 400
402 403 404 406 407 408 409 410 411 412 414 415 417 419 421 422 423 425
426 427 428 429 430 432 433 434 435 436 437 438 439 440 441 442 443 444
445 446 447 448 449 452 453 455 457 458 459 460 463 464 465 467 472 473
474 475 479 480 481 482 483 484 486 487 488 491 493 494 495 496 498 499
500 501 502 504 506 507 509 510 511 512 516 517 518 519 520 521 522 523
524 525 526 527 528 529 530 531 533 535 537 538 539 540 541 543 544 546
549 550 551 552 553 554 555 556 558 559 560 563 565 567 570 571 573 574
575 576 577 578 579 581 582 584 585 587 589 590 591 592 593 594 596 598
599 600 601 602 603 604 605 607 608 610 611 612 613 614 617 618 619 620
622 623 625 628 629 631 632 634 635 636 637 638 639 640 641 642 643 644
646 649 650 651 652 657 659 660 661 662 663 664 666 667 669 670 671 672
673 674 675 679 680 682 683 684 685 686 687 689 691 692 693 694 695 696
697 699 700 701 702 703 704 705 707 708 710 711 712 713 714 718 720 722
723 725 726 727 729 730 731 732 733 734 736 737 738 739 741 742 743 744
745 746 747 750 752 753 754 755 756 757 759 760 761 762 763 765 766 767]

Test data index: [ 7 10 15 20 21 22 25 26 28 37 43 48 55 63 64 71 72 75
77 86 87 96 126 129 130 136 137 140 141 144 149 151 152 155 166 170
176 178 183 190 193 196 198 199 209 210 212 215 219 222 231 234 235 243
252 253 254 263 264 266 269 276 278 279 281 282 287 288 296 297 302 303
313 316 317 319 321 322 325 327 330 332 336 338 348 356 357 367 369 381
384 387 390 393 398 401 405 413 416 418 420 424 431 450 451 454 456 461
462 466 468 469 470 471 476 477 478 485 489 490 492 497 503 505 508 513
514 515 532 534 536 542 545 547 548 557 561 562 564 566 568 569 572 580
583 586 588 595 597 606 609 615 616 621 624 626 627 630 633 645 647 648
653 654 655 656 658 665 668 676 677 678 681 688 690 698 706 709 715 716
717 719 721 724 728 735 740 748 749 751 758 764]
0.7552083333333334
The mean value is
0.7760416666666667

Usage of cross_val_scores

Here the above code is used to output the cross validation scores in an array.We can apply it on the same data with k-fold split to get the scores.

Output

Usage of cross_val_predict

Here the above code is used to output the cross validation prediction in an array.We can apply it on the same data with k-fold split to get the predictions.

Output

Summary

Hence we saw a detailed explanation of the usage of k-fold cross validation method and its implementation.

Thank you for your time.

--

--