added weights, converted data to arrow friendly format
Browse files
utils.py
CHANGED
@@ -35,7 +35,8 @@ def transformation(input, categories):
|
|
35 |
match_index = np.where(categories == cat)[0]
|
36 |
result_array[match_index] = 1
|
37 |
new_x.extend(result_array.tolist())
|
38 |
-
|
|
|
39 |
|
40 |
def get_request_body(datapoint):
|
41 |
data = datapoint.iloc[0].tolist()
|
@@ -51,8 +52,10 @@ def get_explainability_texts(shap_values, feature_texts):
|
|
51 |
sorted_positive_indices = [index for index, _ in sorted(positive_dict.items(), key=lambda item: abs(item[1]), reverse=True)]
|
52 |
positive_texts = [feature_texts[x] for x in sorted_positive_indices]
|
53 |
positive_texts = positive_texts[2:]
|
|
|
54 |
if len(positive_texts) > 5:
|
55 |
positive_texts = positive_texts[:5]
|
|
|
56 |
return positive_texts, sorted_positive_indices
|
57 |
|
58 |
|
@@ -67,11 +70,21 @@ def get_explainability_values(pos_indices, datapoint):
|
|
67 |
else:
|
68 |
val = transformed_data[idx]
|
69 |
vals.append(val)
|
70 |
-
vals = vals[2:]
|
71 |
-
if len(vals) > 5:
|
72 |
-
vals = vals[:5]
|
73 |
return vals
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
def get_fake_certainty():
|
76 |
# Generate a random certainty between 75% and 99%
|
77 |
fake_certainty = uniform(0.75, 0.99)
|
@@ -129,18 +142,26 @@ def get_comment_explanation(certainty, explainability_texts, explainability_valu
|
|
129 |
return comment
|
130 |
|
131 |
def create_data_input_table(datapoint, col_names):
|
132 |
-
st.subheader("
|
133 |
data = datapoint.iloc[0].tolist()
|
134 |
data[7:12] = [bool(value) for value in data[7:12]]
|
135 |
rounded_list = [round(value, 2) if isinstance(value, float) else value for value in data]
|
136 |
df = pd.DataFrame({"Feature name": col_names, "Value": rounded_list })
|
137 |
-
st.dataframe(df, hide_index=True,
|
138 |
|
139 |
# Create a function to generate a table
|
140 |
-
def create_table(texts, values, title):
|
141 |
-
df = pd.DataFrame({"Feature Explanation": texts, 'Value': values})
|
142 |
st.markdown(f'#### {title}') # Markdown for styling
|
143 |
-
st.dataframe(df, hide_index=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
|
146 |
def ChangeButtonColour(widget_label, font_color, background_color='transparent'):
|
|
|
35 |
match_index = np.where(categories == cat)[0]
|
36 |
result_array[match_index] = 1
|
37 |
new_x.extend(result_array.tolist())
|
38 |
+
python_objects = [np_type.item() if isinstance(np_type, np.generic) else np_type for np_type in new_x]
|
39 |
+
return python_objects
|
40 |
|
41 |
def get_request_body(datapoint):
|
42 |
data = datapoint.iloc[0].tolist()
|
|
|
52 |
sorted_positive_indices = [index for index, _ in sorted(positive_dict.items(), key=lambda item: abs(item[1]), reverse=True)]
|
53 |
positive_texts = [feature_texts[x] for x in sorted_positive_indices]
|
54 |
positive_texts = positive_texts[2:]
|
55 |
+
sorted_positive_indices = sorted_positive_indices[2:]
|
56 |
if len(positive_texts) > 5:
|
57 |
positive_texts = positive_texts[:5]
|
58 |
+
sorted_positive_indices = sorted_positive_indices[:5]
|
59 |
return positive_texts, sorted_positive_indices
|
60 |
|
61 |
|
|
|
70 |
else:
|
71 |
val = transformed_data[idx]
|
72 |
vals.append(val)
|
|
|
|
|
|
|
73 |
return vals
|
74 |
|
75 |
+
# def get_weights(shap_values, sorted_indices):
|
76 |
+
# weights = [shap_values[x] for x in sorted_indices]
|
77 |
+
# total_sum = sum(weights)
|
78 |
+
# scaled_values = [val/total_sum for val in weights]
|
79 |
+
# return scaled_values
|
80 |
+
|
81 |
+
def get_weights(shap_values, sorted_indices, target_sum=0.95):
|
82 |
+
weights = [shap_values[x] for x in sorted_indices]
|
83 |
+
total_sum = sum(weights)
|
84 |
+
# Scale to the target sum (0.95 in this case)
|
85 |
+
scaled_values = [val * (target_sum / total_sum) for val in weights]
|
86 |
+
return scaled_values
|
87 |
+
|
88 |
def get_fake_certainty():
|
89 |
# Generate a random certainty between 75% and 99%
|
90 |
fake_certainty = uniform(0.75, 0.99)
|
|
|
142 |
return comment
|
143 |
|
144 |
def create_data_input_table(datapoint, col_names):
|
145 |
+
st.subheader("Transaction details")
|
146 |
data = datapoint.iloc[0].tolist()
|
147 |
data[7:12] = [bool(value) for value in data[7:12]]
|
148 |
rounded_list = [round(value, 2) if isinstance(value, float) else value for value in data]
|
149 |
df = pd.DataFrame({"Feature name": col_names, "Value": rounded_list })
|
150 |
+
st.dataframe(df, hide_index=True, use_container_width=True, height=35*len(df)+38) #width=450
|
151 |
|
152 |
# Create a function to generate a table
|
153 |
+
def create_table(texts, values, weights, title):
|
154 |
+
df = pd.DataFrame({"Feature Explanation": texts, 'Value': values, 'Weight': weights})
|
155 |
st.markdown(f'#### {title}') # Markdown for styling
|
156 |
+
st.dataframe(df, hide_index=True, use_container_width=True, column_config={
|
157 |
+
'Weight': st.column_config.ProgressColumn(
|
158 |
+
'Weight',
|
159 |
+
width='small',
|
160 |
+
format="%.2f",
|
161 |
+
min_value=0,
|
162 |
+
max_value=1
|
163 |
+
)
|
164 |
+
}) #width=450 # Display a simple table
|
165 |
|
166 |
|
167 |
def ChangeButtonColour(widget_label, font_color, background_color='transparent'):
|