Properties

new FeatureSpace(base, arg)

Feature Space

Example

// import qm module
var qm = require('qminer');
// construct a base with the store
var base = new qm.Base({
  mode: "createClean",
  schema: {
    name: "FtrSpace",
    fields: [
      { name: "Value", type: "float" },
      { name: "Category", type: "string" },
      { name: "Categories", type: "string_v" },
    ],
    joins: [],
    keys: []
  }
});
// populate the store
Store = base.store("FtrSpace");
Store.push({ Value: 1.0, Category: "a", Categories: ["a", "q"] });
Store.push({ Value: 1.1, Category: "b", Categories: ["b", "w"] });
Store.push({ Value: 1.2, Category: "c", Categories: ["c", "e"] });
Store.push({ Value: 1.3, Category: "a", Categories: ["a", "q"] });
// create a feature space
var ftr = new qm.FeatureSpace(base, { type: "numeric", source: "FtrSpace", field: "Value" });
base.close();

Parameters

Name Type Optional Description

base

module:qm.Base

 

The base where the features are extracted from.

arg

(Array of module:qm~FeatureExtractor or module:fs.FIn)

 

Constructor arguments. There are two ways of constructing:
1. Using an array of module:qm~FeatureExtractor objects,
2. using a file input stream module:fs.FIn.

Properties

dim

Returns the dimension of the feature space. Type number.

dims

Returns an array of the dimensions of each feature extractor in the feature space. Type Array of numbers.

Methods

addFeatureExtractor(ftExt) → module:qm.FeatureSpace

Adds a new feature extractor to the feature space.

Example

// import qm module
var qm = require('qminer');
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "WeatherForcast",
       fields: [
           { name: "Weather", type: "string" },
           { name: "Date", type: "datetime" },
           { name: "TemperatureDegrees", type: "int" }
       ]
   }]
});
// put some records in the "WeatherForecast" store
base.store("WeatherForcast").push({ Weather: "Partly Cloudy", Date: "2015-05-27T11:00:00", TemperatureDegrees: 19 });
base.store("WeatherForcast").push({ Weather: "Partly Cloudy", Date: "2015-05-28T11:00:00", TemperatureDegrees: 22 });
base.store("WeatherForcast").push({ Weather: "Mostly Cloudy", Date: "2015-05-29T11:00:00", TemperatureDegrees: 25 });
base.store("WeatherForcast").push({ Weather: "Mostly Cloudy", Date: "2015-05-30T11:00:00", TemperatureDegrees: 25 });
base.store("WeatherForcast").push({ Weather: "Scattered Showers", Date: "2015-05-31T11:00:00", TemperatureDegrees: 24 });
base.store("WeatherForcast").push({ Weather: "Mostly Cloudy", Date: "2015-06-01T11:00:00", TemperatureDegrees: 27 });
// create a feature space
var ftr = new qm.FeatureSpace(base, { type: "numeric", source: "WeatherForcast", field: "TemperatureDegrees" });
// add a new feature extractor to the feature space
// it adds the new feature extractor to the pre-existing feature extractors in the feature space
ftr.addFeatureExtractor({ type: "text", source: "WeatherForcast", field: "Weather", normalize: true, weight: "tfidf" });
base.close();

Parameter

Name Type Optional Description

ftExt

module:qm~FeatureExtractor

 

The added feature extractor.

Returns

module:qm.FeatureSpaceB Self.

clear() → module:qm.FeatureSpace

Clears the feature space.

Example

// import qm module
var qm = require('qminer');
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Runners",
       fields: [
           { name: "ID", type: "int", primary: true },
           { name: "Name", type: "string" },
           { name: "BestTime", type: "float" }
       ]
   }]
});
// put some records in the "Runners" store
base.store("Runners").push({ ID: 110020, Name: "Eric Ericsson", BestTime: 134.33 });
base.store("Runners").push({ ID: 123307, Name: "Fred Friedrich", BestTime: 101.11 });
base.store("Runners").push({ ID: 767201, Name: "Appel Banana", BestTime: 1034.56 });
// create a feature space
var ftr = new qm.FeatureSpace(base, { type: "numeric", source: "Runners", field: "BestTime" });
// update the feature space
ftr.updateRecords(base.store("Runners").allRecords);
// clear the feature space (return the feature space to it's default values)
ftr.clear();
base.close();
Returns

module:qm.FeatureSpaceB Self. Features space has been cleared.

extractMatrix(rs[, idx]) → module:la.Matrix

Extracts the feature vectors from the recordset and returns them as columns of a dense matrix.

Example

// import qm module
var qm = require("qminer");
// create a base containing the store Class. Let the Name field be the primary field.
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Class",
       fields: [
           { name: "Name", type: "string", primary: true },
           { name: "StudyGroups", type: "string_v" }
       ]
   }]
});
// add some records to the store
base.store("Class").push({ Name: "Dean", StudyGroups: ["A", "D"] });
base.store("Class").push({ Name: "Chang", StudyGroups: ["B", "D"] });
base.store("Class").push({ Name: "Magnitude", StudyGroups: ["B", "C"] });
base.store("Class").push({ Name: "Leonard", StudyGroups: ["A", "B"] });
// create a feature space containing the multinomial feature extractor
var ftr = new qm.FeatureSpace(base, { type: "multinomial", source: "Class", field: "StudyGroups", values: ["A", "B", "C", "D"] });
// create a feature matrix out of the records of the store by using the feature space
// returns a sparse matrix equal to
// 1  0  0  1
// 0  1  1  1
// 0  0  1  0
// 1  1  0  0
var matrix = ftr.extractMatrix(base.store("Class").allRecords);
base.close();

Parameters

Name Type Optional Description

rs

module:qm.RecordSet

 

The given record set.

idx

number

Yes

when given, only use specified feature extractor.

Returns

module:la.MatrixB The dense matrix, where the i-th column is the feature vector of the i-th record in rs.

extractSparseMatrix(rs[, idx]) → module:la.SparseMatrix

Extracts the sparse feature vectors from the record set and returns them as columns of the sparse matrix.

Example

// import qm module
var qm = require("qminer");
// create a base containing the store Class. Let the Name field be the primary field.
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Class",
       fields: [
           { name: "Name", type: "string", primary: true },
           { name: "StudyGroups", type: "string_v" }
       ]
   }]
});
// add some records to the store
base.store("Class").push({ Name: "Dean", StudyGroups: ["A", "D"] });
base.store("Class").push({ Name: "Chang", StudyGroups: ["B", "D"] });
base.store("Class").push({ Name: "Magnitude", StudyGroups: ["B", "C"] });
base.store("Class").push({ Name: "Leonard", StudyGroups: ["A", "B"] });
// create a feature space containing the multinomial feature extractor
var ftr = new qm.FeatureSpace(base, { type: "multinomial", source: "Class", field: "StudyGroups", values: ["A", "B", "C", "D"] });
// create a sparse feature matrix out of the records of the store by using the feature space
// returns a sparse matrix equal to
// [[(0, 1), (3, 1)], [(1, 1), (3, 1)], [(1, 1), (2, 1)], [(0, 1), (1, 1)]]
var sparseMatrix = ftr.extractSparseMatrix(base.store("Class").allRecords);
base.close();

Parameters

Name Type Optional Description

rs

module:qm.RecordSet

 

The given record set.

idx

number

Yes

When given, only use specified feature extractor.

Returns

module:la.SparseMatrixB The sparse matrix, where the i-th column is the sparse feature vector of the i-th record in rs.

extractSparseVector(rec[, idx]) → module:la.SparseVector

Creates a sparse feature vector from the given record.

Example

// import qm module
var qm = require('qminer');
// create a base containing the store Class. Let the Name field be the primary field.
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Class",
       fields: [
           { name: "Name", type: "string", primary: true },
           { name: "StudyGroup", type: "string" }
       ]
   }]
});
// add some records to the store
base.store("Class").push({ Name: "Dean", StudyGroup: "A" });
base.store("Class").push({ Name: "Chang", StudyGroup: "D" });
base.store("Class").push({ Name: "Magnitude", StudyGroup: "C" });
base.store("Class").push({ Name: "Leonard", StudyGroup: "B" });
// create a new feature space
// the feature space is of dimensions [0, 4]; 4 is the dimension of the categorical feature extractor
// and 0 is the dimension of text feature extractor (the text feature extractor doesn't have any words,
// need to be updated for use).
var ftr = new qm.FeatureSpace(base, [
   { type: "text", source: "Class", field: "Name", normalize: false },
   { type: "categorical", source: "Class", field: "StudyGroup", values: ["A", "B", "C", "D"] }
]);
// get the sparse extractor vector for the first record in store
// the sparse vector will be [(0, 1)] - uses only the categorical feature extractor. There are no
// features in the text feature extractor.
var vec = ftr.extractSparseVector(base.store("Class")[0]);
base.close();

Parameters

Name Type Optional Description

rec

module:qm.Record

 

The given record.

idx

number

Yes

When given, only use specified feature extractor.

Returns

module:la.SparseVectorB The sparse feature vector gained from rec and idx.

extractVector(rec[, idx]) → module:la.Vector

Creates a feature vector from the given record.

Example

// import qm module
var qm = require('qminer');
// create a base containing the store Class. Let the Name field be the primary field.
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Class",
       fields: [
           { name: "Name", type: "string", primary: true },
           { name: "StudyGroup", type: "string" }
       ]
   }]
});
// add some records to the store
base.store("Class").push({ Name: "Jeff", StudyGroup: "A" });
base.store("Class").push({ Name: "Britta", StudyGroup: "D" });
base.store("Class").push({ Name: "Abed", StudyGroup: "C" });
base.store("Class").push({ Name: "Annie", StudyGroup: "B" });
// create a new feature space
// the feature space is of dimensions [0, 4]; 4 is the dimension of the categorical feature extractor
// and 0 is the dimension of text feature extractor (the text feature extractor doesn't have any words,
// need to be updated for use).
var ftr = new qm.FeatureSpace(base, [
   { type: "text", source: "Class", field: "Name", normalize: false },
   { type: "categorical", source: "Class", field: "StudyGroup", values: ["A", "B", "C", "D"] }
]);
// get the extractor vector for the first record in store
// the sparse vector will be [1, 0, 0, 0] - uses only the categorical feature extractor. There are no
// features in the text feature extractor.
var vec = ftr.extractVector(base.store("Class")[0]);
base.close();

Parameters

Name Type Optional Description

rec

module:qm.Record

 

The given record.

idx

number

Yes

when given, only use specified feature extractor.

Returns

module:la.VectorB The feature vector gained from rec and idx.

filter(vec, idx[, keepOffset]) → (module:la.Vector or module:la.SparseVector)

Filters the vector to keep only the elements from the feature extractor.

Example

// import qm module
var qm = require('qminer');
// create a new base with one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Academics",
       fields: [
           { name: "Name", type: "string" },
           { name: "Age", type: "int" },
           { name: "Gendre", type: "string" },
           { name: "Skills", type: "string_v" }
       ]
   }]
});
// create a new feature space
var ftr = new qm.FeatureSpace(base, [
    { type: "numeric", source: "Academics", field: "Age" },
    { type: "categorical", source: "Academics", field: "Gendre", values: ["Male", "Female"] },
    { type: "multinomial", source: "Academics", field: "Skills", values: ["Mathematics", "Programming", "Philosophy", "Languages", "Politics", "Cooking"] }
    ]);
// create a new dense vector
var vec = new qm.la.Vector([40, 0, 1, 0, 1, 1, 1, 0, 0]);
// filter the elements from the second feature extractor
var vec2 = ftr.filter(vec, 1); // returns vector [0, 0, 1, 0, 0, 0, 0, 0, 0]
// filter the elements from the second feature extractor, without keeping the offset
var vec3 = ftr.filter(vec, 1, false); // returns vector [0, 1]
// create a new sparse vector
var spVec = new qm.la.SparseVector([[0, 40], [2, 1], [4, 1], [5, 1], [6, 1]]);
// filter the elements from the second feature extractor
var spVec2 = ftr.filter(spVec, 1); // returns sparse vector [[2, 1]]
// filter the elements from the second feature extractor, without keeping the offset
var spVec3 = ftr.filter(spVec, 1, false); // returns sparse vector [[1, 1]]
base.close();

Parameters

Name Type Optional Description

vec

(module:la.Vector or module:la.SparseVector)

 

The vector from where the function filters the elements.

idx

number

 

The index of the feature extractor.

keepOffset

boolean

Yes

For keeping the original indexing in the new vector.

Defaults to 'true'.

Returns

(module:la.Vector or module:la.SparseVector)B
1. module:la.Vector, if vec is module:la.Vector.
2. module:la.SparseVector, if vec is module:la.SparseVector.

getFeature(idx) → String

Gives the name of the feature at the given position.

Example

// import qm module
var qm = require("qminer");
// create a base containing the store Class. Let the Name field be the primary field.
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Class",
       fields: [
           { name: "Name", type: "string", primary: true },
           { name: "StudyGroups", type: "string_v" }
       ]
   }]
});
// add some records to the store
base.store("Class").push({ Name: "Dean", StudyGroups: ["A", "D"] });
base.store("Class").push({ Name: "Chang", StudyGroups: ["B", "D"] });
base.store("Class").push({ Name: "Magnitude", StudyGroups: ["B", "C"] });
base.store("Class").push({ Name: "Leonard", StudyGroups: ["A", "B"] });
// create a feature space containing the multinomial feature extractor
var ftr = new qm.FeatureSpace(base, [
{ type: "text", source: "Class", field: "Name" },
{ type: "multinomial", source: "Class", field: "StudyGroups", values: ["A", "B", "C", "D"] }
]);
// get the feature at position 2
var feature = ftr.getFeature(2); // returns "C", because the text extractor has no features at the moment
// update the feature space with the records of the store; see the method updateRecords
ftr.updateRecords(base.store("Class").allRecords);
// get the feature at position 2
var feature2 = ftr.getFeature(2); // returns "magnitude"
base.close();

Parameter

Name Type Optional Description

idx

number

 

The index of the feature in feature space (zero based).

Returns

StringB The name of the feature at the position idx.

getFeatureExtractor(idx) → String

Gives the name of feature extractor at given position.

Example

// import qm module
var qm = require("qminer");
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "People",
       fields: [
           { name: "Name", type: "string" },
           { name: "Gendre", type: "string" },
           { name: "Age", type: "int" }
       ]
   }]
});
// create a feature space containing a categorical and numeric feature extractor
var ftr = new qm.FeatureSpace(base, [
   { type: "numeric", source: "People", field: "Age" },
   { type: "categorical", source: "People", field: "Gendre", values: ["Male", "Female"] }
]);
// get the name of the feature extractor with index 1
var extractorName = ftr.getFeatureExtractor(1); // returns "Categorical[Gendre]"
base.close();

Parameter

Name Type Optional Description

idx

number

 

The index of the feature extractor in feature space (zero based).

Returns

StringB The name of the feature extractor at position idx.

invertFeature(idx, val) → Object

Calculates the inverse of a single feature using a specific feature extractor.

Example

// import qm module
var qm = require('qminer');
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "TheWitcherSaga",
       fields: [
           { 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();

Parameters

Name Type Optional Description

idx

number

 

The index of the specific feature extractor.

val

Object

 

The value to be inverted.

Returns

ObjectB The inverse of val using the feature extractor with index idx.

invertFeatureVector(ftr) → Array of Object

Performs the inverse operation of ftrVec. Works only for numeric feature extractors.

Example

// import qm module
var qm = require('qminer');
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "TheWitcherSaga",
       fields: [
           { 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);
// get a feature vector for the second record
// 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, i.e. the value 1995 is now 0.105263
var ftrVec = ftr.extractVector(base.store("TheWitcherSaga")[1]);
// get the inverse of the feature vector
// the function returns the values to their first value, i.e. 0.105263 returns to 1995
var inverse = ftr.invertFeatureVector(ftrVec); // returns a vector [1995]
base.close();

Parameter

Name Type Optional Description

ftr

(module:la.Vector or Array of Object)

 

The feature vector or an array with feature values.

Returns

Array of ObjectB The inverse of ftr as an array.

save(fout) → module:fs.FOut

Serialize the feature space to an output stream.

Parameter

Name Type Optional Description

fout

module:fs.FOut

 

The output stream.

Returns

module:fs.FOutB The output stream fout.

updateRecord(rec) → module:qm.FeatureSpace

Updates the feature space definitions and extractors by adding one record.
For text feature extractors, it can update it's vocabulary by taking into account the new text.
For numeric feature extractors, it can update the minimal and maximal values used to form the normalization.
For jsfunc feature extractors, it can update a parameter used in it's function.
For dateWindow feature extractor, it can update the start and the end of the window period to form the normalization.

Example

// import qm module
var qm = require('qminer');
// create a new base
var base = new qm.Base({
  mode: "createClean",
  schema: {
    name: "FtrSpace",
    fields: [
      { name: "Value", type: "float" },
      { name: "Category", type: "string" },
      { name: "Categories", type: "string_v" },
    ]
  }
});
// populate the store
Store = base.store("FtrSpace");
Store.push({ Value: 1.0, Category: "a", Categories: ["a", "q"] });
Store.push({ Value: 1.1, Category: "b", Categories: ["b", "w"] });
Store.push({ Value: 1.2, Category: "c", Categories: ["c", "e"] });
Store.push({ Value: 1.3, Category: "a", Categories: ["a", "q"] });
// create a new feature space
var ftr = new qm.FeatureSpace(base, [
  { type: "numeric", source: "FtrSpace", normalize: true, field: "Value" },
  { type: "categorical", source: "FtrSpace", field: "Category", values: ["a", "b", "c"] },
  { type: "multinomial", source: "FtrSpace", field: "Categories", normalize: true, values: ["a", "b", "c", "q", "w", "e"] }
]);
// update the feature space with the first three record of the store
ftr.updateRecord(Store[0]);
ftr.updateRecord(Store[1]);
ftr.updateRecord(Store[2]);
// get the feature vectors of these records
ftr.extractVector(Store[0]); // returns the vector [0, 1, 0, 0, 1 / Math.sqrt(2), 0, 0, 1 / Math.sqrt(2), 0, 0]
ftr.extractVector(Store[1]); // returns the vector [1/2, 0, 1, 0, 0, 1 / Math.sqrt(2), 0, 0, 1 / Math.sqrt(2), 0]
ftr.extractVector(Store[2]); // returns the vector [1, 0, 0, 1, 0, 0, 1 / Math.sqrt(2), 0, 0, 1 / Math.sqrt(2)]
base.close();

Parameter

Name Type Optional Description

rec

module:qm.Record

 

The record, which updates the feature space.

Returns

module:qm.FeatureSpaceB Self. The feature space has been updated.

updateRecords(rs) → module:qm.FeatureSpace

Updates the feature space definitions and extractors by adding all the records of a record set.
For text feature extractors, it can update it's vocabulary by taking into account the new text.
For numeric feature extractors, it can update the minimal and maximal values used to form the normalization.
For jsfunc feature extractors, it can update a parameter used in it's function.
For dateWindow feature extractor, it can update the start and the end of the window period to form the normalization.

Example

// import qm module
var qm = require('qminer');
// create a new base
var base = new qm.Base({
  mode: "createClean",
  schema: {
    name: "FtrSpace",
    fields: [
      { name: "Value", type: "float" },
      { name: "Category", type: "string" },
      { name: "Categories", type: "string_v" },
    ]
  }
});
// populate the store
Store = base.store("FtrSpace");
Store.push({ Value: 1.0, Category: "a", Categories: ["a", "q"] });
Store.push({ Value: 1.1, Category: "b", Categories: ["b", "w"] });
Store.push({ Value: 1.2, Category: "c", Categories: ["c", "e"] });
Store.push({ Value: 1.3, Category: "a", Categories: ["a", "q"] });
// create a new feature space
var ftr = new qm.FeatureSpace(base, [
    { type: "numeric", source: "FtrSpace", normalize: true, field: "Value" },
    { type: "categorical", source: "FtrSpace", field: "Category", values: ["a", "b", "c"] },
    { type: "multinomial", source: "FtrSpace", field: "Categories", normalize: true, values: ["a", "b", "c", "q", "w", "e"] }
]);
// update the feature space with the record set
ftr.updateRecords(Store.allRecords);
// get the feature vectors of these records
ftr.extractVector(Store[0]); // returns the vector [0, 1, 0, 0, 1 / Math.sqrt(2), 0, 0, 1 / Math.sqrt(2), 0, 0]
ftr.extractVector(Store[1]); // returns the vector [1/3, 0, 1, 0, 0, 1 / Math.sqrt(2), 0, 0, 1 / Math.sqrt(2), 0]
ftr.extractVector(Store[2]); // returns the vector [2/3, 0, 0, 1, 0, 0, 1 / Math.sqrt(2), 0, 0, 1 / Math.sqrt(2)]
ftr.extractVector(Store[3]); // returns the vector [1, 1, 0, 0, 1 / Math.sqrt(2), 0, 0, 1 / Math.sqrt(2), 0, 0]
base.close();

Parameter

Name Type Optional Description

rs

module:qm.RecordSet

 

The record set, which updates the feature space.

Returns

module:qm.FeatureSpaceB Self. The feature space has been updated.