1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | exports.trainTestSplit = trainTestSplit;
|
13 |
|
14 | var _random = require('../random');
|
15 |
|
16 | var Random = _interopRequireWildcard(_random);
|
17 |
|
18 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
19 |
|
20 | function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
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 | function trainTestSplit(input) {
|
54 | var optionsUser = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
55 |
|
56 |
|
57 | var optionsDefault = {
|
58 | trainSize: 0.8
|
59 | };
|
60 |
|
61 | var options = _extends({}, optionsDefault, optionsUser);
|
62 |
|
63 |
|
64 | var numElements = input[0].length;
|
65 |
|
66 |
|
67 | if (!input.every(function (x) {
|
68 | return x.length === input[0].length;
|
69 | })) {
|
70 | throw new Error('All input arrays should have the same length (i.e., the size of their\n first dimensions should be the same');
|
71 | }
|
72 |
|
73 |
|
74 | var indices = [].concat(_toConsumableArray(Array(numElements).keys()));
|
75 |
|
76 |
|
77 | var numTrainElements = Math.round(numElements * options.trainSize);
|
78 |
|
79 |
|
80 |
|
81 | var trainIndices = Random.sample(indices, numTrainElements, false);
|
82 |
|
83 |
|
84 | var trainArrays = input.map(function (trainArray) {
|
85 | return trainArray.filter(function (x, i) {
|
86 | return trainIndices.includes(i);
|
87 | }).map(function (x) {
|
88 | return Array.isArray(x) ? x.slice() : x;
|
89 | });
|
90 | });
|
91 |
|
92 | var testArrays = input.map(function (testArray) {
|
93 | return testArray.filter(function (x, i) {
|
94 | return !trainIndices.includes(i);
|
95 | }).map(function (x) {
|
96 | return Array.isArray(x) ? x.slice() : x;
|
97 | });
|
98 | });
|
99 |
|
100 |
|
101 | return [].concat(_toConsumableArray(trainArrays), _toConsumableArray(testArrays));
|
102 | } |
\ | No newline at end of file |