File size: 3,086 Bytes
1794e4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit.components.v1 as components

def ChangeButtonColour(widget_label, font_color, background_color='transparent'):
    htmlstr = f"""
        <script>
            var elements = window.parent.document.querySelectorAll('button');
            for (var i = 0; i < elements.length; ++i) {{ 
                if (elements[i].innerText == '{widget_label}') {{ 
                    elements[i].style.color ='{font_color}';
                    elements[i].style.background = '{background_color}'
                }}
            }}
        </script>
        """
    components.html(f"{htmlstr}", height=0, width=0)


def first_five_posneg_indices(response_body):
    values = response_body["explanations"][0]["shap_values"]
    # Separate positive and negative values, keep indice as corresponds to key
    positive_dict = {index: val for index, val in enumerate(values) if val > 0}
    negative_dict = {index: val for index, val in enumerate(values) if val < 0}

    # Sort dictionaries based on the magnitude of values
    sorted_positive_indices = [index for index, _ in sorted(positive_dict.items(), key=lambda item: abs(item[1]), reverse=True)]
    sorted_negative_indices = [index for index, _ in sorted(negative_dict.items(), key=lambda item: abs(item[1]), reverse=True)]

    return [sorted_positive_indices[:5], sorted_negative_indices[:5]]

def get_texts(posneg_indices, feature_texts):
    positive_texts = [feature_texts[x] for x in posneg_indices[0]]
    negative_texts = [feature_texts[x] for x in posneg_indices[1]]
    return positive_texts, negative_texts


def get_input_values(posneg_indices, data_instance):
    values = data_instance["instances"][0]
    outputs = []    
    for lst in posneg_indices:
        vals = []
        for idx in lst:
            if idx in range(7,12):
                val = str(bool(values[idx])).capitalize()
            else:
                val = values[idx]
            vals.append(val)
        outputs.append(vals)
    return outputs[0], outputs[1]

response = {
  "predictions": [
    True
  ],
  "explanations": [
    {
      "shap_values": [
        -0.020634920634920784,
        -0.053968253968253166,
        -0.0015873015873012486,
        0,
        0.04603174603174587,
        -0.12063492063492065,
        0.8365079365079348,
        -0.16349206349206302,
        0.12222222222222279,
        -0.04444444444444462,
        -0.02444444444444462,
        0.03603174603174587,
      ],
      "expected_value": 0.4
    }
  ]
}

feature_texts = {0: "Day out of 30", 1: "Type of transaction: ", 2: "Amount transferred: ", 3: "Initial balance of sender: ", 4: "New balance of sender: ", 
                 5: "Initial balance of recipient: ", 6: "New balance of recipient: ", 7: "Sender's balance was exactly credited: ",
                   8: "Receiver's balance was exactly credited: ", 9: "Transaction over 450.000: ", 10: "Frequent receiver of transactions: ", 11: "Receiver is merchant: ", 12: "Sender ID: ", 13: "Receiver ID: "}

example_input = {"instances":[[1,"PAYMENT",9839.64,170136,160296.36,0,0,1,1,0,0,1,84,2424]]}