Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should return a sparse matrix gained from the numeric feature extractor', function () {
var ftr = new qm.FeatureSpace(base, { type: "numeric", source: "FtrSpaceTest", normalize: true, field: "Value" });
var rs = Store.allRecords;
var mat = ftr.extractSparseMatrix(rs);
//assert.equal(mat.rows, 1);
assert.equal(mat.cols, 11);
assert.eqtol(mat.at(0, 0), 1);
assert.eqtol(mat.at(0, 5), 1.5);
assert.eqtol(mat.at(0, 10), 2);
// test that we get the same for a JSON array input
var rsJson = Store.allRecords.toJSON().records;
var mat2 = ftr.extractSparseMatrix(rsJson);
assert.eqtol(mat.minus(mat2).frob(), 0);
})
it('should return a bigger space matrix gained from the numeric and categorical feature extractor', function () {
it('should return a dense matrix gained from the numeric feature extractor', function () {
var ftr = new qm.FeatureSpace(base, { type: "numeric", source: "FtrSpaceTest", normalize: true, field: "Value" });
var rs = Store.allRecords;
var mat = ftr.extractMatrix(rs);
assert.equal(mat.rows, 1);
assert.equal(mat.cols, 11);
assert.eqtol(mat.at(0, 0), 1);
assert.eqtol(mat.at(0, 5), 1.5);
assert.eqtol(mat.at(0, 10), 2);
// test that we get the same for a JSON array input
var rsJson = Store.allRecords.toJSON().records;
var mat2 = ftr.extractMatrix(rsJson);
assert.eqtol(mat.minus(mat2).frob(), 0);
})
it('should return a dense matrix gained from the numeric and categorical feature extractor', function () {
{ name: "Title", type: "string" },
{ name: "YearOfRelease", type: "int" },
{ name: "EnglishEdition", type: "bool" }
]
}]
});
// put some records in the store
base.store("TheWitcherSaga").push({ Title: "Blood of Elves", YearOfRelease: 1994, EnglishEdition: true });
base.store("TheWitcherSaga").push({ Title: "Time of Contempt", YearOfRelease: 1995, EnglishEdition: true });
base.store("TheWitcherSaga").push({ Title: "Baptism of Fire", YearOfRelease: 1996, EnglishEdition: true });
base.store("TheWitcherSaga").push({ Title: "The Swallow's Tower", YearOfRelease: 1997, EnglishEdition: false });
base.store("TheWitcherSaga").push({ Title: "Lady of the Lake", YearOfRelease: 1999, EnglishEdition: false });
base.store("TheWitcherSaga").push({ Title: "Season of Storms", YearOfRelease: 2013, EnglishEdition: false });
// create a feature space with the numeric feature extractor and update the feature space with the records in store
// for update, look the method updateRecords in feature space
var ftr = new qm.FeatureSpace(base, { type: "numeric", source: "TheWitcherSaga", field: "YearOfRelease", normalize: true });
ftr.updateRecords(base.store("TheWitcherSaga").allRecords);
// because of the numeric feature extractor having normalize: true and of the records update of feature space,
// the values are not equal to those of the records
// invert the value 0 using the numeric feature extractor
var inverse = ftr.invertFeature(0, 0); // returns the value 1994
base.close();
});
});
// Initialize stream aggregates on Resampled store for computing
// 1 minute and 10 minute exponential moving averages.
Resampled.addStreamAggr({ name: "tick", type: "timeSeriesTick",
timestamp: "Time", value: "Value" });
Resampled.addStreamAggr({ name: "ema1m", type: "ema",
inAggr: "tick", emaType: "previous", interval: 60*1000, initWindow: 10*1000 });
Resampled.addStreamAggr({ name: "ema10m", type: "ema",
inAggr: "tick", emaType: "previous", interval: 600*1000, initWindow: 10*1000 });
// Buffer for keeping track of the record from 1 minute ago (6 records
// behind, which equals 60 seconds since Resampled store is equally spaced
// with 10 second rate).
Resampled.addStreamAggr({ name: "delay", type: "recordBuffer", size: 6});
// Declare features from the resampled timeseries which we will use
// to train the recursive linear regression.
var ftrSpace = new qm.FeatureSpace(base, [
{ type: "numeric", source: "Resampled", field: "Value" },
{ type: "numeric", source: "Resampled", field: "Ema1" },
{ type: "numeric", source: "Resampled", field: "Ema2" },
{ type: "multinomial", source: "Resampled", field: "Time", datetime: true }
]);
console.log("Feature space has " + ftrSpace.dim + " dimensions");
// Initialize linear regression model.
var linreg = new analytics.RecLinReg({ "dim": ftrSpace.dim, "forgetFact": 1.0 });
// We register a trigger to Resampled store, which takes the latest record
// and updates the recursive linear regression model.
Resampled.addTrigger({
onAdd: function (val) {
// Get the latest value for EMAs and store them along the
// record in the Resampled store.
it('should throw an exception if the vector length is less than the start index of the extractor', function () {
var ftr = new qm.FeatureSpace(base, [
{ type: "numeric", source: "FtrSpaceTest", field: "Value" },
{ type: "categorical", source: "FtrSpaceTest", field: "Category", values: ["a", "b", "c"] },
{ type: "categorical", source: "FtrSpaceTest", field: "Category", hashDimension: 2 },
{ type: "multinomial", source: "FtrSpaceTest", field: "Categories", values: ["a", "b", "c", "q", "w", "e"] },
{ type: "multinomial", source: "FtrSpaceTest", field: "Categories", hashDimension: 4 }
]);
var in_vec = new qm.la.Vector([1, 0, 0, 1]);
assert.throws(function () {
var out_vec = ftr.filter(in_vec, 3, false);
})
})
});
console.log("predicted/true");
console.log(p);
console.log(p.length);
console.log(t);
console.log(t.length);
}
}
var newText = "I like ngrams and tests";
var testRec = Store.newRecord({
Value: 2.0,
Category: "b",
Categories: ["b", "w"],
Text: newText
});
var ftrSpace1 = new qm.FeatureSpace(base, [
//{ type: "random", source: "FtrSpaceTestOld", seed: 1 },
{
type: "text",
source: "FtrSpaceTestOld",
field: "Text",
ngrams: [1, 3],
tokenizer: {
type: 'simple',
stopwords: 'none'
}
},
]);
var trueAnswer = ['I', 'I LIKE', 'I LIKE NGRAMS', 'LIKE', 'LIKE NGRAMS', 'LIKE NGRAMS AND', 'NGRAMS', 'NGRAMS AND', 'NGRAMS AND TESTS', 'AND', 'AND TESTS', 'TESTS'];
ftrSpace1.updateRecords(Store.allRecords);
ftrSpace1.updateRecord(testRec);
var vec = ftrSpace1.extractSparseVector(testRec);
it('should invert the numerical and categorical feature', function () {
var ftr = new qm.FeatureSpace(base, [
{ type: "numeric", source: "FtrSpaceTest", field: "Value" },
{ type: "categorical", source: "FtrSpaceTest", field: "Category", values: ["a", "b", "c"] }
]);
var inv = ftr.invertFeatureVector([1, 1, 0, 0]);
assert.equal(2, inv.length);
var numInv = inv[0];
var catInv = inv[1];
assert.equal(numInv, 1);
assert.deepEqual(catInv, { a: 1, b: 0, c: 0 });
})
});
it('should return the name of the features of extractor type: categorical', function () {
var ftr = new qm.FeatureSpace(base, { type: "categorical", source: "FtrSpaceTest", field: "Category", values: ["a", "b", "c"] });
assert.equal(ftr.getFeature(0), "a");
assert.equal(ftr.getFeature(1), "b");
assert.equal(ftr.getFeature(2), "c");
})
it('should return the name of the features of extractor type: categorical, hashDimension', function () {
it('should invert a feature vector for extractor type: categorical', function () {
var ftr = new qm.FeatureSpace(base, { type: "categorical", source: "FtrSpaceTest", field: "Category", values: ["a", "b", "c"] });
var val = ftr.invertFeature(0, [1, 0, 0]);
assert.deepEqual(val, { a: 1, b: 0, c: 0 });
})
it('should invert a feature vector for extractor type: categorical, hashDimension', function () {
ngrams: [1, 3],
tokenizer: {
type: 'simple',
stopwords: 'none'
}
},
]);
var trueAnswer = ['I', 'I LIKE', 'I LIKE NGRAMS', 'LIKE', 'LIKE NGRAMS', 'LIKE NGRAMS AND', 'NGRAMS', 'NGRAMS AND', 'NGRAMS AND TESTS', 'AND', 'AND TESTS', 'TESTS'];
ftrSpace1.updateRecords(Store.allRecords);
ftrSpace1.updateRecord(testRec);
var vec = ftrSpace1.extractSparseVector(testRec);
var testAnswer = ftrSpace1.getSparseVectorFeatures(vec);
assert.ok(arraysIdentical(testAnswer, trueAnswer), "ngrams 1,3");
printError(testAnswer, trueAnswer);
var ftrSpace2 = new qm.FeatureSpace(base, [
//{ type: "random", source: "FtrSpaceTestOld", seed: 1 },
{
type: "text",
source: "FtrSpaceTestOld",
field: "Text",
tokenizer: {
type: 'simple',
stopwords: 'none'
}
},
]);
trueAnswer = ['I', 'LIKE', 'NGRAMS', 'AND', 'TESTS'];
ftrSpace2.updateRecords(Store.allRecords);
ftrSpace2.updateRecord(testRec);
vec = ftrSpace2.extractSparseVector(testRec);
testAnswer = ftrSpace2.getSparseVectorFeatures(vec);