@jsmlt/jsmlt
Version:
JavaScript Machine Learning
165 lines (120 loc) • 7.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _base = _interopRequireDefault(require("./base"));
var Arrays = _interopRequireWildcard(require("../../arrays"));
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 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); }
/**
* k-nearest neighbours learner. Classifies points based on the (possibly weighted) vote
* of its k nearest neighbours (euclidian distance).
*/
var KNN =
/*#__PURE__*/
function (_Neighbors) {
_inherits(KNN, _Neighbors);
/**
* Constructor. Initialize class members and store user-defined options.
*
* @param {Object} [optionsUser] - User-defined options for KNN
* @param {number} [optionsUser.numNeighbours = 3] - Number of nearest neighbours to consider for
* the majority vote
*/
function KNN() {
var _this;
var optionsUser = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, KNN);
_this = _possibleConstructorReturn(this, _getPrototypeOf(KNN).call(this)); // Parse options
var optionsDefault = {
numNeighbours: 3
};
var options = _objectSpread({}, optionsDefault, {}, optionsUser); // Set options
_this.numNeighbours = options.numNeighbours;
return _this;
}
/**
* @see {@link Classifier#train}
*/
_createClass(KNN, [{
key: "train",
value: function train(X, y) {
if (X.length !== y.length) {
throw new Error('Number of data points should match number of labels.');
} // Store data points
this.training = {
X: X,
y: y
};
}
/**
* @see {@link Classifier#predict}
*/
}, {
key: "predict",
value: function predict(X) {
var _this2 = this;
if (typeof this.training === 'undefined') {
throw new Error('Model has to be trained in order to make predictions.');
}
if (X[0].length !== this.training.X[0].length) {
throw new Error('Number of features of test data should match number of features of training data.');
} // 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) {
var _this3 = this;
// Calculate distances to all other data points
var distances = Arrays.zipWithIndex(this.training.X.map(function (x) {
return Arrays.norm(Arrays.sum(sampleFeatures, Arrays.scale(x, -1)));
})); // Sort training data points based on distance
distances.sort(function (a, b) {
if (a[0] > b[0]) return 1;
if (a[0] < b[0]) return -1;
return 0;
}); // Number of nearest neighbours to consider
var k = Math.min(this.numNeighbours, distances.length); // Take top k distances
var distancesTopKClasses = distances.slice(0, k).map(function (x) {
return _this3.training.y[x[1]];
}); // Count the number of neighbours per class
var votes = Arrays.valueCounts(distancesTopKClasses); // Get class index with highest number of votes
var highest = -1;
var highestLabel = -1;
votes.forEach(function (vote) {
if (vote[1] > highest) {
highest = vote[1];
highestLabel = vote[0];
}
});
return highestLabel;
}
}]);
return KNN;
}(_base["default"]);
exports["default"] = KNN;
module.exports = exports.default;