File size: 5,412 Bytes
b486442
 
 
 
 
 
d349a77
 
 
b486442
 
 
2d4d414
 
 
b8c8baf
 
2d4d414
fa45d0b
2d4d414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1180b9
2d4d414
 
f1180b9
 
 
2d4d414
 
f1180b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d4d414
f1180b9
 
2d4d414
f1180b9
 
2d4d414
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
---
license: mit
language:
- en
metrics:
- accuracy
- precision
- recall
- f1
library_name: keras
tags:
- finance
---
# 💰 Bank Churn Prediction — AI for Smarter Customer Retention  

![Model2_Thumbnail](https://cdn-uploads.huggingface.co/production/uploads/68faf2c58b9b8d06b47b769c/ShPfAY5zBzUUM1BRG9oga.png)

## 🧩 Overview  
Businesses like banks which provide service have to worry about the problem of *Customer Churn*, i.e. customers leaving and joining another service provider. It is important to understand which aspects of the service influence a customer's decision in this regard.  
Management can concentrate efforts on improvement of service, keeping in mind these priorities.  

**Objective**  
You as a Data Scientist with the bank need to build a neural network–based classifier that can determine whether a customer will leave the bank or not in the next 6 months.

---

## 🤖 Model Details  
- **Model Type:** Feed-forward ANN (Artificial Neural Network) — Binary Classifier  
- **Framework:** TensorFlow / Keras  
- **Dataset:** `Churn.csv` *(Bank Customer Churn Dataset, 10,000+ customers)*  
- **Input:** Structured customer profile (credit score, age, balance, tenure, activity, salary, etc.)  
- **Output:** Binary churn prediction (`0 = stays`, `1 = churn`)  

---

## 📊 Data Dictionary  
| Feature             | Description                                                                |
|---------------------|----------------------------------------------------------------------------|
| `CustomerId`        | Unique ID assigned to each customer                                        |
| `Surname`           | Customer’s last name                                                       |
| `CreditScore`       | Customer credit history                                                    |
| `Geography`         | Customer location                                                          |
| `Gender`            | Gender of the customer                                                     |
| `Age`               | Age of the customer                                                        |
| `Tenure`            | Number of years with the bank                                              |
| `NumOfProducts`     | Number of bank products purchased                                          |
| `Balance`           | Account balance                                                            |
| `HasCrCard`         | Whether the customer has a credit card                                     |
| `EstimatedSalary`   | Estimated salary                                                           |
| `IsActiveMember`    | Whether the customer is an active member                                   |
| `Exited`            | Target label — 0: No (customer stays), 1: Yes (customer churns)            |

---

## 🌟 Why It Matters  
✅ Detects early signs of potential churn  
✅ Enables targeted retention strategies  
✅ Improves customer engagement and loyalty  
✅ Helps maximize profitability and reduce attrition rates  

📘 **Full Source Notebook:**  
The complete training and evaluation notebook is available on GitHub:  
👉 [View on GitHub](https://github.com/joyjitroy/Machine_Learning/blob/main/Bank_Customer_Churn_Prediction_using_Artificial_Neural_Networks.ipynb)

---
## 🚀 Example Usage

```python
# Inference example aligned to your dataset schema

import pandas as pd
import numpy as np

CSV_PATH = "Data/Churn.csv"
TARGET = "Exited"

# Columns exactly as in your table
ALL_COLS = [
    "CustomerId","Surname","CreditScore","Geography","Gender","Age","Tenure",
    "NumOfProducts","Balance","HasCrCard","EstimatedSalary","IsActiveMember","Exited"
]

NUM_COLS = ["CreditScore","Age","Tenure","NumOfProducts","Balance","EstimatedSalary","HasCrCard","IsActiveMember"]
CAT_COLS = ["Geography","Gender"]
DROP_COLS = ["CustomerId","Surname"]

# Load data
df = pd.read_csv(CSV_PATH)[ALL_COLS]

def prepare_features(df_in, fit_cols=None):
    X = df_in.drop(columns=[TARGET] + DROP_COLS).copy()
    # one hot on the two categoricals
    X = pd.get_dummies(X, columns=CAT_COLS, drop_first=True)
    # align to training columns
    if fit_cols is not None:
        X = X.reindex(columns=fit_cols, fill_value=0)
    return X

# If you trained in this notebook, reuse `model` and `feature_cols` from training:
#   model.save("bank_churn_ann.keras")
#   feature_cols = X_train.columns.tolist()

# If loading a saved model:
# from tensorflow.keras.models import load_model
# model = load_model("bank_churn_ann.keras")
# feature_cols = [...]  # same list you used during training after get_dummies

# Single example constructed with your schema
example = {
    "CustomerId": 15788241,
    "Surname": "Smith",
    "CreditScore": 600,
    "Geography": "Germany",   # France, Germany, Spain in this dataset
    "Gender": "Male",         # Male or Female
    "Age": 40,
    "Tenure": 3,
    "NumOfProducts": 2,
    "Balance": 60000.0,
    "HasCrCard": 1,
    "EstimatedSalary": 50000.0,
    "IsActiveMember": 1,
    "Exited": 0               # ignored at inference
}

ex_df = pd.DataFrame([example])
X_ex = prepare_features(ex_df, fit_cols=feature_cols)

pred_prob = float(model.predict(X_ex, verbose=0)[0][0])
pred = int(pred_prob >= 0.5)

print(f"Churn Probability: {pred_prob:.4f}")
print("Prediction:", "Likely to churn" if pred else "Likely to stay")