/
split_data.jl
64 lines (60 loc) · 1.53 KB
/
split_data.jl
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
##### Beginning of file
import DataFrames
import Random
import StatsBase
"""
"""
function split_data(
features_df::DataFrames.AbstractDataFrame,
labels_df::DataFrames.AbstractDataFrame,
split::Real,
)
result = split_data(
Random.GLOBAL_RNG,
features_df,
labels_df,
split,
)
return result
end
"""
"""
function split_data(
rng::AbstractRNG,
features_df::DataFrames.AbstractDataFrame,
labels_df::DataFrames.AbstractDataFrame,
split::Real,
)
#
if !(0 < split < 1)
error("split must be >0 and <1")
end
if size(features_df, 1) != size(labels_df, 1)
error("features_df and labels_df do not have the same number of rows")
end
#
num_rows = size(features_df, 1)
num_partition_1 = round(Int, split * num_rows)
num_partition_2 = num_rows - num_partition_1
#
allrows = convert(Array, 1:num_rows)
partition_1_rows = StatsBase.sample(
rng,
allrows,
num_partition_1;
replace = false,
)
partition_2_rows = setdiff(allrows, partition_1_rows)
#
partition_1_features_df = features_df[partition_1_rows, :]
partition_2_features_df = features_df[partition_2_rows, :]
#
partition_1_labels_df = labels_df[partition_1_rows, :]
partition_2_labels_df = labels_df[partition_2_rows, :]
#
return partition_1_features_df,
partition_1_labels_df,
partition_2_features_df,
partition_2_labels_df
end
##### End of file