new DpMeans([arg])

DpMeans Clustering

Example

// import analytics and la modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a KMeans object
var dpmeans = new analytics.DpMeans();
// create the matrix to be fitted
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
// create the model
dpmeans.fit(X);
// predict where the columns of the matrix will be assigned
var Y = new la.Matrix([[1, 1, 0], [-2, 3, 1]]);
var prediction = dpmeans.predict(Y);

Parameter

Name Type Optional Description

arg

(module:analytics~DpMeansParam or module:fs.FIn)

Yes

Construction arguments. There are two ways of constructing:
1. Using the module:analytics~DpMeansParam object,
2. using the file input stream module:fs.FIn.

Properties

centroids

The centroids created with the fit method. Type module:la.Matrix.

Example

// import the modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new DpMeans object
var DpMeans = new analytics.DpMeans({ iter: 1000, lambda: 3 });
// create a matrix to be fitted
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
// create the model with the matrix X
DpMeans.fit(X);
// get the centroids
var centroids = DpMeans.centroids;
// print the first centroid
console.log(centroids.getCol(0));

idxv

The integer vector containing the cluster ids of the training set created with the fit method. Type module:la.IntVector.

Example

// import the modules
var analytics = require('qminer').analytics;
// create a new DpMeans object
var DpMeans = new analytics.DpMeans({ iter: 1000, lambda: 3 });
// get the idxv
var idxv = DpMeans.idxv;

medoids

The medoids created with the fit method. Type module:la.IntVector.

Example

// import the modules
var analytics = require('qminer').analytics;
// create a new DpMeans object
var DpMeans = new analytics.DpMeans({ iter: 1000, lambda: 3 });
// get the centroids
var medoids = DpMeans.medoids;

relMeanCentroidDist

Returns the normalized weighted distance between the vectors and their centroids using the following formula: d = \frac{sumi p_i*sum_j d(x_j,c_i) / n_i}{sum{k=1}^n d(xk, mu) / n} = \frac{sum{i,j} d(c_i,x_j)}{sum_k d(x_k, mu)}

Returns

number relMeanDist

Methods

explain(X) → Array of module:analytics~DpMeansExplain

Returns the IDs of the nearest medoid for each example.

Example

// import analytics module
var analytics = require('qminer').analytics;
// import linear algebra module
var la = require('qminer').la;
// create a new DpMeans object
var DpMeans = new analytics.DpMeans({ iter: 1000, k: 3 });
// create a matrix to be fitted
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
// create the model with the matrix X using the column IDs [0,1,2]
DpMeans.fit(X, [1234,1142,2355]);
// create the matrix of the prediction vectors
var test = new la.Matrix([[2, -1, 1], [1, 0, -3]]);
// predict/explain - return the closest medoids
var explanation = DpMeans.explain(test);

Parameter

Name Type Optional Description

X

(module:la.Matrix or module:la.SparseMatrix)

 

Matrix whose columns correspond to examples.

Returns

Array of module:analytics~DpMeansExplain Array containing the DpMeans explanantions.

fit(X) → module:analytics.DpMeans

Calculates the centroids.

Examples

Asynchronous function

// import analytics module
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new DpMeans object
var DpMeans = new analytics.DpMeans({ iter: 1000, lambda: 3 });
// create a matrix to be fitted
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
// create the model with the matrix X
DpMeans.fitAsync(X, function (err) {
    if (err) console.log(err);
    // successful calculation
});

Synchronous function

var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new DpMeans object
var DpMeans = new analytics.DpMeans({ iter: 1000, lambda: 3 });
// create a matrix to be fitted
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
// create the model with the matrix X
DpMeans.fit(X);

Parameter

Name Type Optional Description

X

(module:la.Matrix or module:la.SparseMatrix)

 

Matrix whose columns correspond to examples.

Returns

module:analytics.DpMeans Self. The model has been updated.

getModel() → Object

Returns the model.

Example

// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create the KMeans object
var dpmeans = new analytics.DpMeans({ iter: 1000 });
// create a matrix to be fitted
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
// create the model
dpmeans.fit(X);
// get the model
var model = dpmeans.getModel();
Returns

Object The DpMeansModel object containing the properites:
1. DpMeansModel.C - The module:la.Matrix or module:la.SparseMatrix containing the centroids,
2. DpMeansModel.medoids - The module:la.IntVector of cluster medoids of the training data,
3. DpMeansModel.idxv - The module:la.IntVector of cluster IDs of the training data.

getParams() → module:analytics~KMeansParam

Returns the parameters.

Example

// import analytics module
var analytics = require('qminer').analytics;
// create a new DpMeans object
var DpMeans = new analytics.DpMeans({ iter: 1000, lambda: 5 });
// get the parameters
var json = DpMeans.getParams();
console.log(json.lambda);
Returns

module:analytics~KMeansParam The construction parameters.

permuteCentroids(mapping) → module:analytics.DpMeans

Permutates the clusters, and with it module:analytics.DpMeans#centroids, module:analytics.DpMeans#medoids and module:analytics.DpMeans#idxv.

Example

// import the modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new DpMeans object
var DpMeans = new analytics.DpMeans({ iter: 1000, lambda: 3 });
// create a matrix to be fitted
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
// create the model with the matrix X
DpMeans.fit(X);
if (DpMeans.centroids.cols > 1) {
    // create the mapping vector: swap first two centroids
    var Mapping = new la.IntVector([1, 0, 2, 3, 4, 5].splice(0,DpMeans.centroids.cols));
    console.log(DpMeans.centroids.toString());
    // permutate the clusters.
    DpMeans.permuteCentroids(Mapping);
    console.log(DpMeans.centroids.toString());
}

Parameter

Name Type Optional Description

mapping

module:la.IntVector

 

The mapping, where mapping[4] = 2 means "map cluster 4 into cluster 2".

Returns

module:analytics.DpMeans Self. The clusters have been permuted.

predict(A) → module:la.IntVector

Returns an vector of cluster id assignments.

Example

// import analytics module
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new DpMeans object
var DpMeans = new analytics.DpMeans({ iter: 1000, lambda: 3 });
// create a matrix to be fitted
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
// create the model with the matrix X
DpMeans.fit(X);
// create the matrix of the prediction vectors
var pred = new la.Matrix([[2, -1, 1], [1, 0, -3]]);
// predict the values
var prediction = DpMeans.predict(pred);

Parameter

Name Type Optional Description

A

(module:la.Matrix or module:la.SparseMatrix)

 

Matrix whose columns correspond to examples.

Returns

module:la.IntVector Vector of cluster assignments.

save(fout) → module:fs.FOut

Saves DpMeans internal state into (binary) file.

Example

// import the modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
var fs = require('qminer').fs;
// create a new DpMeans object
var DpMeans = new analytics.DpMeans({ iter: 1000, lambda: 3 });
// create a matrix to be fitted
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
// create the model with the matrix X
DpMeans.fit(X);
// create the file output stream
var fout = new fs.openWrite('DpMeans.bin');
// save the DpMeans instance
DpMeans.save(fout);
fout.close();
// load the DpMeans instance
var fin = fs.openRead('DpMeans.bin');
var KMeans2 = new analytics.DpMeans(fin);

Parameter

Name Type Optional Description

fout

module:fs.FOut

 

The output stream.

Returns

module:fs.FOut The output stream fout.

setParams(params) → module:analytics.DpMeans

Sets the parameters.

Example

// import analytics module
var analytics = require('qminer').analytics;
// create a new DpMeans object
var DpMeans = new analytics.DpMeans();
// change the parameters of the DpMeans object
DpMeans.setParams({ iter: 1000, lambda: 5 });

Parameter

Name Type Optional Description

params

module:analytics~KMeansParam

 

The construction parameters.

Returns

module:analytics.DpMeans Self. The model parameters have been updated.

transform(A) → module:la.Matrix

Transforms the points to vectors of distances to centroids.

Example

// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new DpMeans object
var DpMeans = new analytics.DpMeans({ iter: 1000, lambda: 3 });
// create a matrix to be fitted
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
// create the model with the matrix X
DpMeans.fit(X);
// create the matrix of the transform vectors
var matrix = new la.Matrix([[-2, 0], [0, -3]]);
// get the transform values of matrix
// returns the matrix
//  10    17
//   1    20
//  10     1
DpMeans.transform(matrix);

Parameter

Name Type Optional Description

A

(module:la.Matrix or module:la.SparseMatrix)

 

Matrix whose columns correspond to examples.

Returns

module:la.Matrix Matrix where each column represents the squared distances to the centroid vectors.