@jsmlt/jsmlt
Version:
JavaScript Machine Learning
88 lines (73 loc) • 3.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.loadDatasetFromCSV = loadDatasetFromCSV;
exports.loadDatasetFromRemoteCSV = loadDatasetFromRemoteCSV;
exports.loadIris = loadIris;
var _request = _interopRequireDefault(require("request"));
var _csv = _interopRequireDefault(require("csv"));
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 }; }
// External dependencies
// Internal dependencies
/**
* Load a dataset (features and target) from some CSV input string. Extracts the data from the CSV
* and uses all but the last column as the features and the last column as the target. This function
* is asynchronous, and needs a user callback for when the file is successfully parsed.
*
* @param {string} input - Input CSV string
* @param {function(X: Array.<Array.<number>>, y: Array.<number>)} callback - Callback function with
* arguments X (features) and y (targets)
*/
function loadDatasetFromCSV(input, callback) {
_csv["default"].parse(input, {
auto_parse: true
}, function (err, output) {
// Extract the feature and target columns
var X = Arrays.slice(output, [0, 0], [null, -1]);
var y = Arrays.flatten(Arrays.slice(output, [0, -1], [null, null])); // Call user-provided callback
callback(X, y);
});
}
/**
* Load a dataset from a remote CSV file. Fetches the CSV file and calls loadDatasetFromCSV. This
* function is asynchronous, and needs a user callback for when the remote CSV file is successfully
* loaded and parsed.
*
* @param {string} url - Input CSV file URL
* @param {function(X: Array.<Array.<number>>, y: Array.<number>)} callback - Callback function with
* arguments X (features) and y (targets)
*/
function loadDatasetFromRemoteCSV(url, callback) {
_request["default"].get(url, function (error, response, body) {
if (error || response.statusCode !== 200) {
throw new Error('Unable to load remote dataset file.');
}
loadDatasetFromCSV(body, callback);
});
}
/**
* Load the iris dataset. This is an asynchronous function: when the Iris dataset is loaded, a
* user-specified callback function is invoked, with the data set features array and the targets
* array as the first and second parameter, respectively.
*
* For more information, see https://github.com/jsmlt/datasets/tree/master/iris
*
* @example <caption>Load the Iris dataset and run a Perceptron classifier on it</caption>
* var datasets = require('@jsmlt/jsmlt/datasets');
* var Perceptron = require('@jsmlt/jsmlt/supervised/linear/perceptron');
*
* datasets.loadIris(function(X, y) {
* var clf = new Perceptron();
* clf.train(X, y);
* });
*
* @param {function(X: Array.<Array.<number>>, y: Array.<number>)} callback - Callback function with
* arguments X (features) and y (targets). Called when the dataset is successfully loaded
*/
function loadIris(callback) {
loadDatasetFromRemoteCSV('https://raw.githubusercontent.com/jsmlt/datasets/master/iris/data.csv', callback);
}