-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDR.js
More file actions
111 lines (101 loc) · 2.89 KB
/
DR.js
File metadata and controls
111 lines (101 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { euclidean } from "../metrics/index";
import { Matrix } from "../matrix/index";
import { Randomizer } from "../util/randomizer";
/**
* @class
* @alias DR
*/
export class DR{
static parameter_list = [];
/**
*
* @constructor
* @memberof module:dimensionality_reduction
* @alias DR
* @param {Matrix|Array<Array<Number>>} X - the high-dimensional data.
* @param {number} [d = 2] - the dimensionality of the projection.
* @param {function} [metric = euclidean] - the metric which defines the distance between two points.
* @param {seed} [seed=1987] - the seed value for the random number generator.
* @returns {DR}
*/
constructor(X, d=2, metric=euclidean, seed=1212) {
if (Array.isArray(X)) {
this._type = "array";
this.X = Matrix.from(X);
} else if (X instanceof Matrix) {
this._type = "matrix";
this.X = X;
} else {
throw "no valid type for X";
}
[this._N, this._D] = this.X.shape;
this._d = d;
this._metric = metric;
this._seed = seed;
this._randomizer = new Randomizer(seed);
this._is_initialized = false;
return this;
}
/**
* Set and get parameters
* @param {String} name - name of the parameter.
* @param {Number} [value = null] - value of the parameter to set, if null then return actual parameter value.
*/
parameter(name, value=null) {
if (this.parameter_list.findIndex(parameter => parameter === name) === -1) {
throw `${name} is not a valid parameter!`;
}
if (value) {
this[`_${name}`] = value;
return this;
} else {
return this[`_${name}`];
}
}
/**
* Alias for 'parameter'.
* @param {String} name
* @param {Number} value
*/
para(name, value=null) {
return this.parameter(name, value);
}
/**
* Alias for 'parameter'.
* @param {String} name
* @param {Number} value
*/
p(name, value=null) {
return this.parameter(name, value);
}
/**
* Computes the projection.
* @returns {Matrix} Returns the projection.
*/
transform() {
this.check_init();
return this.Y;
}
check_init() {
if (!this._is_initialized && typeof this.init === "function") {
this.init();
this._is_initialized = true;
}
}
/**
* @returns {Matrix} Returns the projection.
*/
get projection() {
return this._type === "matrix" ? this.Y : this.Y.to2dArray;
}
async transform_async() {
return this.transform();
}
static transform(...args) {
let dr = new this(...args);
return dr.transform();
}
static async transform_async(...args) {
return this.transform(...args);
}
}