analytics. DpMeans
Source: analyticsdoc.
DpMeans Clustering is an iterative, data-partitioning algorithm that assigns observations into clusters with the nearest centroid according to some metric.
The number of clusters is not known in advance, but can be upper and lower bounded. Rather the parameter
is the maximum radius of a cluster lambda
.
Properties
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 |
Yes |
Construction arguments. There are two ways of constructing:
|
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{sum_i p_i*sum_j d(x_j,c_i) / n_i}{sum_{k=1}^n d(x_k, mu) / n} = \frac{sum_{i,j} d(c_i,x_j)}{sum_k d(x_k, mu)}
- Returns
-
number
B 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 |
|
Matrix whose columns correspond to examples. |
- Returns
-
Array of module:analytics~DpMeansExplain
B 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 |
|
Matrix whose columns correspond to examples. |
- Returns
-
module:analytics.DpMeans
B 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
B TheDpMeansModel
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
B 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 |
|
The mapping, where |
- Returns
-
module:analytics.DpMeans
B 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 |
|
Matrix whose columns correspond to examples. |
- Returns
-
module:la.IntVector
B 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 |
|
The output stream. |
- Returns
-
module:fs.FOut
B The output streamfout
.
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 |
|
The construction parameters. |
- Returns
-
module:analytics.DpMeans
B 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 |
|
Matrix whose columns correspond to examples. |
- Returns
-
module:la.Matrix
B Matrix where each column represents the squared distances to the centroid vectors.