forked from instillai/machine-learning-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leave-p-out.py
60 lines (42 loc) · 1.63 KB
/
leave-p-out.py
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
# Example of LOOCV and LPOCV splitting
import numpy
from sklearn.model_selection import LeaveOneOut, LeavePOut
# Configurable constants
P_VAL = 2
def print_result(split_data):
"""
Prints the result of either a LPOCV or LOOCV operation
Args:
split_data: The resulting (train, test) split data
"""
for train, test in split_data:
output_train = ''
output_test = ''
bar = ["-"] * (len(train) + len(test))
# Build our output for display from the resulting split
for i in train:
output_train = "{}({}: {}) ".format(output_train, i, data[i])
for i in test:
bar[i] = "T"
output_test = "{}({}: {}) ".format(output_test, i, data[i])
print("[ {} ]".format(" ".join(bar)))
print("Train: {}".format(output_train))
print("Test: {}\n".format(output_test))
# Create some data to split with
data = numpy.array([[1, 2], [3, 4], [5, 6], [7, 8]])
# Our two methods
loocv = LeaveOneOut()
lpocv = LeavePOut(p=P_VAL)
split_loocv = loocv.split(data)
split_lpocv = lpocv.split(data)
print("""\
The Leave-P-Out method works by using every combination of P points as test data.
The following output shows the result of splitting some sample data by Leave-One-Out and Leave-P-Out methods.
A bar displaying the current train-test split as well as the actual data points are displayed for each split.
In the bar, "-" is a training point and "T" is a test point.
""")
print("Data:\n{}\n".format(data))
print("Leave-One-Out:\n")
print_result(split_loocv)
print("Leave-P-Out (where p = {}):\n".format(P_VAL))
print_result(split_lpocv)