@jsmlt/jsmlt
Version:
JavaScript Machine Learning
206 lines (153 loc) • 10.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _base = require("../base");
var _decisionTree = _interopRequireDefault(require("./decision-tree"));
var Arrays = _interopRequireWildcard(require("../../arrays"));
var Random = _interopRequireWildcard(require("../../random"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
/**
* Random forest learner. Builds multiple decision trees with a random subsample of the samples,
* and averages their predictions for the final prediction model.
*/
var RandomForest =
/*#__PURE__*/
function (_Classifier) {
_inherits(RandomForest, _Classifier);
/**
* Constructor. Initialize class members and store user-defined options.
*
* @param {Object} [optionsUser] - User-defined options for random forest
* @param {number} [optionsUser.numTrees = 10] - Number of decision trees to build
* @param {string} [optionsUser.criterion = 'gini'] - Splitting criterion. Either 'gini', for the
* Gini coefficient, or 'entropy' for the Shannon entropy
* @param {number|string} [optionsUser.numFeatures = 1.0] - Number of features to subsample at
* each node. Either a number (float), in which case the input fraction of features is used
* (e.g., 1.0 for all features), or a string. If string, 'sqrt' and 'log2' are supported,
* causing the algorithm to use sqrt(n) and log2(n) features, respectively (where n is the
* total number of features)
* @param {number} [optionsUser.maxDepth = -1] - Maximum depth of each decision tree. The depth of
* a decision tree is the number of nodes in the longest path from the decision tree root to a
* leaf. It is an indicator of the complexity of the tree. Use -1 for no maximum depth
* @param {boolean} [bootstrap = true] - Whether to select samples for each tree by bootstrapping.
* If false, all samples are used for each tree. If true, n samples are drawn with replacement
* from the full set of samples for each tree (where n is the total number of samples)
* @param {number} [optionsUser.numTrees = 10] - Number of trees to construct
*/
function RandomForest() {
var _this;
var optionsUser = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, RandomForest);
_this = _possibleConstructorReturn(this, _getPrototypeOf(RandomForest).call(this)); // Parse options
var optionsDefault = {
criterion: 'gini',
numFeatures: 1.0,
maxDepth: -1,
numTrees: 10,
bootstrap: true
};
var options = _objectSpread({}, optionsDefault, {}, optionsUser); // Set options
_this.criterion = options.criterion;
_this.numFeatures = options.numFeatures;
_this.maxDepth = options.maxDepth;
_this.numTrees = options.numTrees;
_this.bootstrap = options.bootstrap;
return _this;
}
/**
* @see {@link Classifier#train}
*/
_createClass(RandomForest, [{
key: "train",
value: function train(X, y) {
if (X.length !== y.length) {
throw new Error('Number of data points should match number of labels.');
} // Construct and train decision trees
this.trees = []; // All sample indices
var sampleIndices = _toConsumableArray(Array(X.length)).map(function (x, i) {
return i;
});
for (var i = 0; i < this.numTrees; i += 1) {
// Construct decision tree
var tree = new _decisionTree["default"]({
criterion: this.criterion,
numFeatures: this.numFeatures,
maxDepth: this.maxDepth
}); // Select the input samples. If bootstrapping is disabled, use all samples. If it is enabled,
// use a bootstrapped sample of all samples
var treeX = void 0;
var treeY = void 0;
if (this.bootstrap) {
var treeSamples = Random.sample(sampleIndices, X.length, true);
treeX = treeSamples.map(function (sampleIndex) {
return X[sampleIndex];
});
treeY = treeSamples.map(function (sampleIndex) {
return y[sampleIndex];
});
} else {
treeX = X;
treeY = y;
} // Train the tree
tree.train(treeX, treeY); // Add the trained tree to the list of trees
this.trees.push(tree);
}
}
/**
* @see {@link Classifier#predict}
*/
}, {
key: "predict",
value: function predict(X) {
var _this2 = this;
if (typeof this.trees === 'undefined') {
throw new Error('Model has to be trained in order to make predictions.');
} // Make prediction for each data point
var predictions = X.map(function (x) {
return _this2.predictSample(x);
});
return predictions;
}
/**
* Make a prediction for a single sample.
*
* @param {Array.<number>} sampleFeatures - Data point features
* @return {mixed} Prediction. Label of class with highest prevalence among k nearest neighbours
*/
}, {
key: "predictSample",
value: function predictSample(sampleFeatures) {
// Gather predictions from all trees
var predictions = this.trees.map(function (x) {
return x.predictSample(sampleFeatures);
}); // Count the number of votes for each class
var predictionCounts = Arrays.valueCounts(predictions); // Predict the class with the most predictions
return predictionCounts.reduce(function (r, x) {
return x[1] > r[1] ? x : r;
}, [-1, -1])[0];
}
}]);
return RandomForest;
}(_base.Classifier);
exports["default"] = RandomForest;
module.exports = exports.default;