stevedep's avatar
stevedep
Memorable Member
5 years ago

Entropy

Hi,

 

The below code will create a measure that displays the entropy of an attribute. The lower the entropy value the better the separation between two classes. The example used here is from this webpage: http://www.learnbymarketing.com/481/decision-tree-flavors-gini-info-gain/ 

 

The table was loaded and then uppivoted. The attributes themselves have become values to filter on. The attachment contains the Power BI file where you can review the Power M code.  Hope that you find this usefull.

Entropy = MINX (
    ADDCOLUMNS (
        VALUES ( 'Table (2)'[Value] ),
        "v",
            VAR _selVal =
                CALCULATE ( SELECTEDVALUE ( 'Table (2)'[Value] ) )
            VAR _PercALT =
                CALCULATE ( [PercA], FILTER ( 'Table (2)', [Value] <= _selVal ) )
            VAR _PercBLT =
                CALCULATE ( [PercB], FILTER ( 'Table (2)', [Value] <= _selVal ) )
            VAR _PercAGT =
                CALCULATE ( [PercA], FILTER ( 'Table (2)', [Value] > _selVal ) )
            VAR _PercBGT =
                CALCULATE ( [PercB], FILTER ( 'Table (2)', [Value] > _selVal ) )
            VAR _CountRowsLT =
                COUNTROWS ( FILTER ( 'Table (2)', [Value] <= _selVal ) )
            VAR _CountRowsGT =
                COUNTROWS ( FILTER ( 'Table (2)', [Value] > _selVal ) )
            VAR _CountRows =
                COUNTROWS ( 'Table (2)' )
            VAR _EntLT =
                -1
                    * (
                         ( _PercALT * LOG ( _PercALT, 2 ) )
                            + ( _PercBLT * LOG ( _PercBLT, 2 ) )
                    )
            VAR _EntGT =
                -1
                    * (
                         ( _PercAGT * LOG ( _PercAGT, 2 ) )
                            + ( _PercBGT * LOG ( _PercBGT, 2 ) )
                    )
            RETURN
                 ( _CountRowsLT / _CountRows ) * _EntLT + ( _CountRowsGT / _CountRows ) * _EntGT
    ),
    [v]
)
ClassA = COUNTROWS ( FILTER ( 'Table (2)', [Class] = "A" ) )
ClassAll = COUNTROWS ( 'Table (2)' )
ClassB = COUNTROWS ( FILTER ( 'Table (2)', [Class] = "B" ) )
PercA = [ClassA] / [ClassAll]
PercB = [ClassB] / [ClassAll]

Kind regards, Steve. 

 

No RepliesBe the first to reply