UNPKG

@jsmlt/jsmlt

Version:

JavaScript Machine Learning

72 lines (60 loc) 3.35 kB
"use strict"; function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } 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"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } var expect = require('chai').expect; var modelSelection = require('./index.js'); var arrays = require('../arrays/index.js'); describe('ModelSelect', function () { describe('.trainTestSplit', function () { it('should split the input arrays into the proportions indicated by the training set size parameter', function () { var A = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(function (x) { return [x, x * 10]; }); var B = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1]; var _modelSelection$train = modelSelection.trainTestSplit([A, B], { trainSize: 0.7 }), _modelSelection$train2 = _slicedToArray(_modelSelection$train, 4), A_train = _modelSelection$train2[0], B_train = _modelSelection$train2[1], A_test = _modelSelection$train2[2], B_test = _modelSelection$train2[3]; expect(arrays.getShape(A_train)).to.deep.equal([7, 2]); expect(arrays.getShape(A_test)).to.deep.equal([3, 2]); expect(arrays.getShape(B_train)).to.deep.equal([7]); expect(arrays.getShape(B_test)).to.deep.equal([3]); }); it('should should keep the indices of the train/test elements consistent across input arrays', function () { var A = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; var B = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]; var C = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]; var _modelSelection$train3 = modelSelection.trainTestSplit([A, B, C]), _modelSelection$train4 = _slicedToArray(_modelSelection$train3, 6), A_train = _modelSelection$train4[0], B_train = _modelSelection$train4[1], C_train = _modelSelection$train4[2], A_test = _modelSelection$train4[3], B_test = _modelSelection$train4[4], C_test = _modelSelection$train4[5]; expect(A_train.filter(function (x, i) { return B[x] != B_train[i]; }).length).to.equal(0); expect(A_train.filter(function (x, i) { return C[x] != C_train[i]; }).length).to.equal(0); expect(A_test.filter(function (x, i) { return B[x] != B_test[i]; }).length).to.equal(0); expect(A_test.filter(function (x, i) { return C[x] != C_test[i]; }).length).to.equal(0); }); it('should throw an error when the size of the input arrays is not equal in the first dimension', function () { expect(function () { return modelSelection.trainTestSplit([[0, 1], [0, 1, 2]]); }).to["throw"](); }); }); });