📘
Full Source Notebook:
The complete training and evaluation notebook is available on GitHub:
👉
View on GitHub
1# Inference example aligned to your dataset schema
2
3import pandas as pd
4import numpy as np
5
6CSV_PATH = "Data/Churn.csv"
7TARGET = "Exited"
8
9# Columns exactly as in your table
10ALL_COLS = [
11 "CustomerId","Surname","CreditScore","Geography","Gender","Age","Tenure",
12 "NumOfProducts","Balance","HasCrCard","EstimatedSalary","IsActiveMember","Exited"
13]
14
15NUM_COLS = ["CreditScore","Age","Tenure","NumOfProducts","Balance","EstimatedSalary","HasCrCard","IsActiveMember"]
16CAT_COLS = ["Geography","Gender"]
17DROP_COLS = ["CustomerId","Surname"]
18
19# Load data
20df = pd.read_csv(CSV_PATH)[ALL_COLS]
21
22def prepare_features(df_in, fit_cols=None):
23 X = df_in.drop(columns=[TARGET] + DROP_COLS).copy()
24 # one hot on the two categoricals
25 X = pd.get_dummies(X, columns=CAT_COLS, drop_first=True)
26 # align to training columns
27 if fit_cols is not None:
28 X = X.reindex(columns=fit_cols, fill_value=0)
29 return X
30
31# If you trained in this notebook, reuse `model` and `feature_cols` from training:
32# model.save("bank_churn_ann.keras")
33# feature_cols = X_train.columns.tolist()
34
35# If loading a saved model:
36# from tensorflow.keras.models import load_model
37# model = load_model("bank_churn_ann.keras")
38# feature_cols = [...] # same list you used during training after get_dummies
39
40# Single example constructed with your schema
41example = {
42 "CustomerId": 15788241,
43 "Surname": "Smith",
44 "CreditScore": 600,
45 "Geography": "Germany", # France, Germany, Spain in this dataset
46 "Gender": "Male", # Male or Female
47 "Age": 40,
48 "Tenure": 3,
49 "NumOfProducts": 2,
50 "Balance": 60000.0,
51 "HasCrCard": 1,
52 "EstimatedSalary": 50000.0,
53 "IsActiveMember": 1,
54 "Exited": 0 # ignored at inference
55}
56
57ex_df = pd.DataFrame([example])
58X_ex = prepare_features(ex_df, fit_cols=feature_cols)
59
60pred_prob = float(model.predict(X_ex, verbose=0)[0][0])
61pred = int(pred_prob >= 0.5)
62
63print(f"Churn Probability: {pred_prob:.4f}")
64print("Prediction:", "Likely to churn" if pred else "Likely to stay")
65