UNPKG

3.17 kBJavaScriptView Raw
1'use strict';
2
3var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
4
5var expect = require('chai').expect;
6var modelSelection = require('./index.js');
7var arrays = require('../arrays/index.js');
8
9describe('ModelSelect', function () {
10 describe('.trainTestSplit', function () {
11 it('should split the input arrays into the proportions indicated by the training set size parameter', function () {
12 var A = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(function (x) {
13 return [x, x * 10];
14 });
15 var B = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1];
16
17 var _modelSelection$train = modelSelection.trainTestSplit([A, B], { trainSize: 0.7 }),
18 _modelSelection$train2 = _slicedToArray(_modelSelection$train, 4),
19 A_train = _modelSelection$train2[0],
20 B_train = _modelSelection$train2[1],
21 A_test = _modelSelection$train2[2],
22 B_test = _modelSelection$train2[3];
23
24 expect(arrays.getShape(A_train)).to.deep.equal([7, 2]);
25 expect(arrays.getShape(A_test)).to.deep.equal([3, 2]);
26 expect(arrays.getShape(B_train)).to.deep.equal([7]);
27 expect(arrays.getShape(B_test)).to.deep.equal([3]);
28 });
29
30 it('should should keep the indices of the train/test elements consistent across input arrays', function () {
31 var A = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
32 var B = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
33 var C = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29];
34
35 var _modelSelection$train3 = modelSelection.trainTestSplit([A, B, C]),
36 _modelSelection$train4 = _slicedToArray(_modelSelection$train3, 6),
37 A_train = _modelSelection$train4[0],
38 B_train = _modelSelection$train4[1],
39 C_train = _modelSelection$train4[2],
40 A_test = _modelSelection$train4[3],
41 B_test = _modelSelection$train4[4],
42 C_test = _modelSelection$train4[5];
43
44 expect(A_train.filter(function (x, i) {
45 return B[x] != B_train[i];
46 }).length).to.equal(0);
47 expect(A_train.filter(function (x, i) {
48 return C[x] != C_train[i];
49 }).length).to.equal(0);
50 expect(A_test.filter(function (x, i) {
51 return B[x] != B_test[i];
52 }).length).to.equal(0);
53 expect(A_test.filter(function (x, i) {
54 return C[x] != C_test[i];
55 }).length).to.equal(0);
56 });
57
58 it('should throw an error when the size of the input arrays is not equal in the first dimension', function () {
59 expect(function () {
60 return modelSelection.trainTestSplit([[0, 1], [0, 1, 2]]);
61 }).to.throw();
62 });
63 });
64});
\No newline at end of file