Update app.py
Browse files
app.py
CHANGED
@@ -245,91 +245,4 @@ gradio_interface.launch(share=True)
|
|
245 |
|
246 |
|
247 |
|
248 |
-
import numpy as np
|
249 |
-
import shap
|
250 |
-
import lime
|
251 |
-
from lime import lime_tabular
|
252 |
-
|
253 |
-
|
254 |
-
# SHAP Explanations
|
255 |
-
explainer = shap.Explainer(xgb_model)
|
256 |
-
shap_values = explainer(X_test)
|
257 |
-
|
258 |
-
# Visualize SHAP values (e.g., summary plot)
|
259 |
-
shap.summary_plot(shap_values, X_test)
|
260 |
-
|
261 |
-
# LIME Explanations
|
262 |
-
explainer = lime_tabular.LimeTabularExplainer(
|
263 |
-
training_data=np.array(X_train),
|
264 |
-
feature_names=X_train.columns,
|
265 |
-
class_names=['Not Fraud', 'Fraud'],
|
266 |
-
mode='classification'
|
267 |
-
)
|
268 |
-
|
269 |
-
# Explain a single prediction (e.g., the first instance in X_test)
|
270 |
-
i = 0
|
271 |
-
exp = explainer.explain_instance(
|
272 |
-
data_row=X_test.iloc[i],
|
273 |
-
predict_fn=xgb_model.predict_proba
|
274 |
-
)
|
275 |
-
|
276 |
-
# Visualize the LIME explanation
|
277 |
-
exp.show_in_notebook(show_table=True)
|
278 |
-
|
279 |
-
|
280 |
-
from tensorflow import keras
|
281 |
-
from tensorflow.keras import layers
|
282 |
-
|
283 |
-
# Define the model
|
284 |
-
model = keras.Sequential([
|
285 |
-
layers.Dense(128, activation='relu', input_shape=(X_train.shape[1],)),
|
286 |
-
layers.Dense(64, activation='relu'),
|
287 |
-
layers.Dense(1, activation='sigmoid')
|
288 |
-
])
|
289 |
-
|
290 |
-
# Compile the model
|
291 |
-
model.compile(optimizer='adam',
|
292 |
-
loss='binary_crossentropy',
|
293 |
-
metrics=['accuracy'])
|
294 |
-
|
295 |
-
# Train the model
|
296 |
-
model.fit(X_train, y_train, epochs=10, batch_size=32)
|
297 |
-
|
298 |
-
# Evaluate the model
|
299 |
-
loss, accuracy = model.evaluate(X_test, y_test)
|
300 |
-
print('Test accuracy:', accuracy)
|
301 |
-
|
302 |
-
# Make predictions
|
303 |
-
y_pred_nn = (model.predict(X_test) > 0.5).astype("int32")
|
304 |
-
|
305 |
-
# Analyze the results
|
306 |
-
analyze_results(y_test, y_pred_nn)
|
307 |
-
|
308 |
-
# Add NN to the Gradio Interface
|
309 |
-
def predict_fraud_with_nn(*features):
|
310 |
-
# ... (Same as before, but get prediction from the NN model) ...
|
311 |
-
input_data = [features]
|
312 |
-
nn_pred = (model.predict(input_data) > 0.5).astype("int32")[0][0]
|
313 |
-
return (
|
314 |
-
"Fraud" if nn_pred == 1 else "Not Fraud",
|
315 |
-
"Fraud" if xgb_pred == 1 else "Not Fraud",
|
316 |
-
"Fraud" if rf_pred == 1 else "Not Fraud",
|
317 |
-
"Fraud" if gb_pred == 1 else "Not Fraud"
|
318 |
-
)
|
319 |
-
|
320 |
-
# Update the Gradio interface with the new prediction function and output
|
321 |
-
gradio_interface = gr.Interface(
|
322 |
-
fn=predict_fraud_with_nn,
|
323 |
-
inputs=[gr.Number(label=label) for label in input_labels],
|
324 |
-
outputs=[
|
325 |
-
gr.Textbox(label="Neural Network Prediction"),
|
326 |
-
gr.Textbox(label="XGBoost Prediction"),
|
327 |
-
gr.Textbox(label="Random Forest Prediction"),
|
328 |
-
gr.Textbox(label="Gradient Boosting Prediction")
|
329 |
-
],
|
330 |
-
title="Credit Card Fraud Detection with XGBoost, Random Forest, Gradient Boosting, and NN",
|
331 |
-
description="Enter transaction details to get predictions from four different models."
|
332 |
-
)
|
333 |
|
334 |
-
# Launch the Gradio app
|
335 |
-
#gradio_interface.launch(share=True)
|
|
|
245 |
|
246 |
|
247 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
|
|
|
|