/
subspaces.ts
47 lines (44 loc) · 1.89 KB
/
subspaces.ts
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
// import { aggregate } from '../utils';
import aggregate from 'cube-core';
import { entropy, normalize } from '../statistics/index';
import { DataSource, OperatorType } from '../commonTypes';
import { crammersV, getCombination, pearsonCC, linearMapPositive } from '../statistics/index';
import { CrammersVThreshold } from './config';
import { Cluster } from '../ml/index';
import { CHANNEL } from '../constant';
// insights like outlier and trend both request high impurity of dimension.
function getDimCorrelationMatrix(dataSource: DataSource, dimensions: string[]): number[][] {
let matrix: number[][] = dimensions.map(d => dimensions.map(d => 0));
for (let i = 0; i < dimensions.length; i++) {
matrix[i][i] = 1;
for(let j = i + 1; j < dimensions.length; j++) {
matrix[i][j] = matrix[j][i] = crammersV(dataSource, dimensions[i], dimensions[j]);
}
}
return matrix;
}
export function getDimSetsBasedOnClusterGroups(dataSource: DataSource, dimensions: string[]): string[][] {
const maxDimNumberInView = 4;
let dimSets: string[][] = [];
let dimCorrelationMatrix = getDimCorrelationMatrix(dataSource, dimensions);
// groupMaxSize here means group number.
let groups: string[][] = Cluster.kruskal({
matrix: dimCorrelationMatrix,
measures: dimensions,
groupMaxSize: Math.round(dimensions.length / maxDimNumberInView),
threshold: CrammersVThreshold
});
// todo: maybe a threhold would be better ?
for (let group of groups) {
let combineDimSet: string[][] = getCombination(group, 1, CHANNEL.maxDimensionNumber);
dimSets.push(...combineDimSet);
}
return dimSets;
}
export function subspaceSearching(dataSource: DataSource, dimensions: string[], shouldDimensionsCorrelated: boolean | undefined = true): string[][] {
if (shouldDimensionsCorrelated) {
return getDimSetsBasedOnClusterGroups(dataSource, dimensions);
} else {
return getCombination(dimensions)
}
}