File size: 5,069 Bytes
fc0f7bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
112
import { INumericRange, ICategoricalRange, RangeTypes } from "mlchartlib";
import { IBinnedResponse } from "./IBinnedResponse";
import _ from "lodash";

export class BinnedResponseBuilder {
    public static buildCategorical(featureRange: INumericRange | ICategoricalRange, index: number, sensitiveFeatures: any[][]): IBinnedResponse {
        if (featureRange.rangeType === RangeTypes.categorical) {
            return {
                hasError: false,
                array: (featureRange as ICategoricalRange).uniqueValues,
                featureIndex: index,
                rangeType: RangeTypes.categorical,
                labelArray:  (featureRange as ICategoricalRange).uniqueValues
            };
        }
        const uniqueValues = BinnedResponseBuilder.getIntegerUniqueValues(sensitiveFeatures, index);
        return {
            hasError: false,
            array: uniqueValues,
            featureIndex: index,
            rangeType: RangeTypes.categorical,
            labelArray:  uniqueValues.map(num => num.toString())
        };
    }

    public static buildNumeric(featureRange: INumericRange, index: number, sensitiveFeatures: any[][], binCount?: number): IBinnedResponse {
        if (binCount === undefined) {
            if (featureRange.rangeType === RangeTypes.integer) {
                const uniqueValues = BinnedResponseBuilder.getIntegerUniqueValues(sensitiveFeatures, index);
                binCount = Math.min(5, uniqueValues.length);
            }
            if (binCount === undefined) {
                binCount = 5;
            }
        }
        let delta = featureRange.max - featureRange.min;
        if (delta === 0 || binCount === 0) {
            return {
                hasError: false,
                array: [featureRange.max],
                featureIndex: index,
                rangeType: RangeTypes.categorical,
                labelArray:  [featureRange.max.toString()]
            };
        }
        // make uniform bins in these cases
        if (featureRange.rangeType === RangeTypes.numeric || delta < (binCount - 1)) {
            const binDelta = delta / binCount;
            const array = new Array(binCount).fill(0).map((unused, index) => {
                return index !== binCount - 1 ?
                    featureRange.min + (binDelta * (1+ index)) :
                    featureRange.max;
            });
            let prevMax = featureRange.min;
            const labelArray = array.map((num) => {
                const label = `${prevMax.toLocaleString(undefined, {maximumSignificantDigits: 3})} - ${num.toLocaleString(undefined, {maximumSignificantDigits: 3})}`;
                prevMax = num;
                return label;
            });
            return {
                hasError: false,
                array,
                featureIndex: index,
                rangeType: RangeTypes.numeric,
                labelArray
            };
        }
        // handle integer case, increment delta since we include the ends as discrete values
        const intDelta = delta / binCount;
        const array = new Array(binCount).fill(0).map((unused, index) => {
            if (index === binCount - 1) {
                return featureRange.max;
            }
            return Math.ceil( featureRange.min - 1 + intDelta * (index + 1));
        });
        let previousVal = featureRange.min;
        const labelArray = array.map((num) => {
            const label = previousVal === num ?
            previousVal.toLocaleString(undefined, {maximumSignificantDigits: 3}) :
                `${previousVal.toLocaleString(undefined, {maximumSignificantDigits: 3})} - ${num.toLocaleString(undefined, {maximumSignificantDigits: 3})}`
            previousVal = num + 1;
            return label;
        });
        return {
            hasError: false,
            array,
            featureIndex: index,
            rangeType: RangeTypes.integer,
            labelArray
        };
    }

    public static buildDefaultBin(featureRange: INumericRange | ICategoricalRange, index: number, sensitiveFeatures: any[][]): IBinnedResponse {
        if (featureRange.rangeType === RangeTypes.categorical) {
            return BinnedResponseBuilder.buildCategorical(featureRange, index, sensitiveFeatures);
        }
        if (featureRange.rangeType === RangeTypes.integer) {
            const uniqueValues = BinnedResponseBuilder.getIntegerUniqueValues(sensitiveFeatures, index);
            if (uniqueValues.length <= BinnedResponseBuilder.UpperBoundUniqueIntegers) {
                return BinnedResponseBuilder.buildCategorical(featureRange, index, sensitiveFeatures);
            }
        }
        return BinnedResponseBuilder.buildNumeric(featureRange, index, sensitiveFeatures);
    }

    private static getIntegerUniqueValues(sensitiveFeatures: any[][], index: number): number[] {
        const column = sensitiveFeatures.map(row => row[index]) as number[];
        return _.uniq(column).sort((a, b) => {return a - b;});
    }

    private static readonly UpperBoundUniqueIntegers = 10;
}