@jsmlt/jsmlt
Version:
JavaScript Machine Learning
141 lines (118 loc) • 4.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _datapoint = _interopRequireDefault(require("./datapoint"));
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 _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; }
/**
* Container of data points for a single data set.
*/
var Dataset =
/*#__PURE__*/
function () {
/**
* Initialize object properties.
*/
function Dataset() {
_classCallCheck(this, Dataset);
this.numDimensions = 0;
this.numDatapoints = 0;
this.datapoints = [];
}
/**
* Add a data point to the data set. Data point should be an array with the number of dimensions
* used throughout the dataset.
*
* @param features Data point features array
*/
_createClass(Dataset, [{
key: "addDatapoint",
value: function addDatapoint(features) {
// Set the dimensionality of the data if this is the first data point
if (this.numDatapoints === 0) {
this.numDimensions = features.length;
} // Construct data point
var datapoint = new _datapoint["default"](features); // Check data point validity
if (!this.isValidDataPoint(datapoint)) {
throw new Error('Invalid specification of data point.');
} // Add data point
this.datapoints.push(datapoint);
this.numDatapoints += 1;
return datapoint;
}
/**
* Check whether a data point is valid. A valid data point is an array of the correct number
* of dimensions.
*
* @param datapoint Data point to be checked
* @return bool Whether the data point is valid
*/
}, {
key: "isValidDataPoint",
value: function isValidDataPoint(datapoint) {
return Array.isArray(datapoint.features) && datapoint.features.length === this.numDimensions;
}
/**
* Get all data points from this dataset
*
* @return Array Data points. Array of datapoint objects
*/
}, {
key: "getDataPoints",
value: function getDataPoints() {
return this.datapoints;
}
/**
* Get the number of data points in this dataset
*
* @return int Number of data points
*/
}, {
key: "numDataPoints",
value: function numDataPoints() {
return this.datapoints.length;
}
/**
* Get the number of dimensions for the features of data points
*
* @return int Dimensionality
*/
}, {
key: "getNumDimensions",
value: function getNumDimensions() {
return this.numDimensions;
}
/**
* Extract the features from the datapoints and return them in an n x d array, where `n` is the
* number of data points and `d` is the number of dimensions (number of features per datapoint).
*
* @return Array[Array[Number]] Features matrix
*/
}, {
key: "getFeaturesArray",
value: function getFeaturesArray() {
return this.datapoints.map(function (x) {
return x.features;
});
}
/**
* Extract the labels from the datapoints and return them in an array.
*
* @return Array[mixed] Labels array
*/
}, {
key: "getLabelsArray",
value: function getLabelsArray() {
return this.datapoints.map(function (x) {
return x.classIndex;
});
}
}]);
return Dataset;
}();
exports["default"] = Dataset;
module.exports = exports.default;