@jsmlt/jsmlt
Version:
JavaScript Machine Learning
158 lines (116 loc) • 5.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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; };
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _base = require('./base');
var _base2 = _interopRequireDefault(_base);
var _arrays = require('../../arrays');
var Arrays = _interopRequireWildcard(_arrays);
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; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // Internal dependencies
/**
* k-nearest neighbours learner. Classifies points based on the (possibly weighted) vote
* of its k nearest neighbours (euclidian distance).
*/
var KNN = 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 optionsUser = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, KNN);
// Parse options
var _this = _possibleConstructorReturn(this, (KNN.__proto__ || Object.getPrototypeOf(KNN)).call(this));
var optionsDefault = {
numNeighbours: 3
};
var options = _extends({}, 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;
}(_base2.default);
exports.default = KNN;
module.exports = exports['default'];