Upload 4 files
Browse files- Customer churing.ipynb +0 -0
- WA_Fn-UseC_-Telco-Customer-Churn.csv +0 -0
- app.py +69 -0
- random_forest_model.pkl +3 -0
Customer churing.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
WA_Fn-UseC_-Telco-Customer-Churn.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Initialize the Flask app
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# Load the trained model
|
| 9 |
+
model = joblib.load('random_forest_model.pkl')
|
| 10 |
+
|
| 11 |
+
@app.route('/')
|
| 12 |
+
def home():
|
| 13 |
+
return "Welcome to the Customer Churn Prediction API!"
|
| 14 |
+
|
| 15 |
+
# Define the prediction endpoint
|
| 16 |
+
@app.route('/predict', methods=['POST'])
|
| 17 |
+
def predict():
|
| 18 |
+
try:
|
| 19 |
+
# Get the JSON data from the request
|
| 20 |
+
data = request.get_json()
|
| 21 |
+
|
| 22 |
+
# Extract features from the input JSON
|
| 23 |
+
features = [
|
| 24 |
+
data.get("gender"),
|
| 25 |
+
data.get("SeniorCitizen"),
|
| 26 |
+
data.get("Partner"),
|
| 27 |
+
data.get("Dependents"),
|
| 28 |
+
data.get("tenure"),
|
| 29 |
+
data.get("PhoneService"),
|
| 30 |
+
data.get("MultipleLines"),
|
| 31 |
+
data.get("InternetService"),
|
| 32 |
+
data.get("OnlineSecurity"),
|
| 33 |
+
data.get("OnlineBackup"),
|
| 34 |
+
data.get("DeviceProtection"),
|
| 35 |
+
data.get("TechSupport"),
|
| 36 |
+
data.get("StreamingTV"),
|
| 37 |
+
data.get("StreamingMovies"),
|
| 38 |
+
data.get("Contract"),
|
| 39 |
+
data.get("PaperlessBilling"),
|
| 40 |
+
data.get("PaymentMethod"),
|
| 41 |
+
data.get("MonthlyCharges"),
|
| 42 |
+
data.get("TotalCharges")
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
# Convert the features to a NumPy array for the model
|
| 46 |
+
features_array = np.array([features])
|
| 47 |
+
|
| 48 |
+
# Perform prediction
|
| 49 |
+
prediction = model.predict(features_array)
|
| 50 |
+
prediction_probability = model.predict_proba(features_array)
|
| 51 |
+
|
| 52 |
+
# Map prediction result to a human-readable label
|
| 53 |
+
churn_label = "Yes" if prediction[0] == 1 else "No"
|
| 54 |
+
response = {
|
| 55 |
+
"prediction": churn_label,
|
| 56 |
+
"probability": {
|
| 57 |
+
"No": prediction_probability[0][0],
|
| 58 |
+
"Yes": prediction_probability[0][1]
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
return jsonify(response)
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return jsonify({"error": str(e)})
|
| 66 |
+
|
| 67 |
+
# Run the app
|
| 68 |
+
if __name__ == '__main__':
|
| 69 |
+
app.run(debug=True)
|
random_forest_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:06f3641f78b28056a53da6018b310d32a6dc12a70a1390ceab9d3207ddbf8fe2
|
| 3 |
+
size 15056825
|