0% found this document useful (0 votes)
138 views4 pages

Cross Validation Presentation - Jupyter Notebook

This document discusses different cross validation techniques including K-fold, Leave-One-Out, Stratified K-fold, and Leave-P-Out cross validation. It imports data and splits it into training and test sets using each technique, printing the resulting train and test indexes.

Uploaded by

Divy jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views4 pages

Cross Validation Presentation - Jupyter Notebook

This document discusses different cross validation techniques including K-fold, Leave-One-Out, Stratified K-fold, and Leave-P-Out cross validation. It imports data and splits it into training and test sets using each technique, printing the resulting train and test indexes.

Uploaded by

Divy jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

5/6/2021 Cross Validation Presentation - Jupyter Notebook

In [7]:

#K-fold

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sn
import statsmodels.api as sm
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold

ipl_df=pd.read_csv(r'M:\MCA\Sem 4\ML(PREETI MAM)\p1\IPL IMB381IPL2013.csv')

#spliting dataset into train and test set


X=ipl_df['BASE PRICE']
y=ipl_df['SOLD PRICE']

kf = KFold(n_splits=2)

for train_index, test_index in kf.split(X):


print("TRAIN:", train_index)
print("TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]

TRAIN: [ 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
82
83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 126 127 128 129]
TEST: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64]
TRAIN: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64]
TEST: [ 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
82
83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 126 127 128 129]

localhost:8888/notebooks/Cross Validation Presentation.ipynb 1/4


5/6/2021 Cross Validation Presentation - Jupyter Notebook

In [15]:

from sklearn.model_selection import LeaveOneOut

loo = LeaveOneOut()

for train_index, test_index in loo.split(X):


print("TRAIN:", train_index)
print("TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]

TRAIN: [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1
7 18
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 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 126
127 128 129]
TEST: [0]
TRAIN: [ 0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1
7 18
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 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 126
127 128 129]
TEST [1]

localhost:8888/notebooks/Cross Validation Presentation.ipynb 2/4


5/6/2021 Cross Validation Presentation - Jupyter Notebook

In [14]:

import numpy as np
from sklearn.model_selection import StratifiedKFold

skf = StratifiedKFold(n_splits=2)

for train_index, test_index in skf.split(X, y):


print("TRAIN:", train_index)
print("TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]

TRAIN: [ 15 19 24 29 30 32 34 41 45 46 48 49 53 54 55 57 59
60
62 64 65 70 71 72 73 74 76 78 81 82 83 85 86 87 88 91
94 95 96 98 99 100 102 103 104 106 107 109 111 112 113 115 116 117
118 119 120 121 123 124 125 126 127 128 129]
TEST: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17
18
20 21 22 23 25 26 27 28 31 33 35 36 37 38 39 40 42 43
44 47 50 51 52 56 58 61 63 66 67 68 69 75 77 79 80 84
89 90 92 93 97 101 105 108 110 114 122]
TRAIN: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17
18
20 21 22 23 25 26 27 28 31 33 35 36 37 38 39 40 42 43
44 47 50 51 52 56 58 61 63 66 67 68 69 75 77 79 80 84
89 90 92 93 97 101 105 108 110 114 122]
TEST: [ 15 19 24 29 30 32 34 41 45 46 48 49 53 54 55 57 59
60
62 64 65 70 71 72 73 74 76 78 81 82 83 85 86 87 88 91
94 95 96 98 99 100 102 103 104 106 107 109 111 112 113 115 116 117
118 119 120 121 123 124 125 126 127 128 129]

C:\Users\DV\anaconda3\lib\site-packages\sklearn\model_selection\_split.py:67
0: UserWarning: The least populated class in y has only 1 members, which is
less than n_splits=2.
warnings.warn(("The least populated class in y has only %d"

localhost:8888/notebooks/Cross Validation Presentation.ipynb 3/4


5/6/2021 Cross Validation Presentation - Jupyter Notebook

In [12]:

#Leave-p-out

import numpy as np
from sklearn.model_selection import LeavePOut

lpo = LeavePOut(2)

for train_index, test_index in lpo.split(X):


print("TRAIN:", train_index)
print("TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]

TRAIN: [ 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1
8 19
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 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 126 127
128 129]
TEST: [0 1]
TRAIN: [ 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1
8 19
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 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 126 127
128 129]
TEST [0 2]
In [ ]:

localhost:8888/notebooks/Cross Validation Presentation.ipynb 4/4

You might also like