File size: 2,599 Bytes
ac08d9b
 
 
 
 
53fe5e4
ac08d9b
 
53fe5e4
ac08d9b
 
53fe5e4
ac08d9b
53fe5e4
ac08d9b
 
53fe5e4
 
ac08d9b
53fe5e4
ac08d9b
 
53fe5e4
 
 
 
 
 
 
 
 
 
 
 
ac08d9b
 
 
53fe5e4
ac08d9b
 
53fe5e4
ac08d9b
53fe5e4
 
ac08d9b
53fe5e4
 
cebbb7b
53fe5e4
 
ac08d9b
 
 
 
 
 
 
53fe5e4
 
 
 
 
 
ac08d9b
53fe5e4
 
ac08d9b
cebbb7b
ac08d9b
 
 
53fe5e4
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
import joblib
import pandas as pd
from flask import Flask, request, jsonify

# Initialize Flask app with a name
cust_churn_predictor_api = Flask ("Customer Churn Predictor Week1")

# Load the trained churn prediction model
model = joblib.load ("churn_prediction_model_v2_0.joblib")

# Define a route for the home page
@cust_churn_predictor_api.get ('/')
def home ():
    return "Welcome to the Customer Churn Prediction Week1 API!"

# Define an endpoint to predict churn for a single customer
@cust_churn_predictor_api.post ('/v1/customer')
def predict_churn ():
    # Get JSON data from the request
    customer_data = request.get_json ()

    # Extract relevant customer features from the input data
    sample = {
        'customerID'      : customer_data ['customerID'],
        'SeniorCitizen'   : customer_data ['SeniorCitizen'],
        'tenure'          : customer_data ['tenure'],
        'MonthlyCharges'  : customer_data ['MonthlyCharges'],
        'TotalCharges'    : customer_data ['TotalCharges'],
        'Partner'         : customer_data ['Partner'],
        'Dependents'      : customer_data ['Dependents'],
        'PhoneService'    : customer_data ['PhoneService'],
        'InternetService' : customer_data ['InternetService'],
        'Contract'        : customer_data ['Contract'],
        'PaymentMethod'   : customer_data ['PaymentMethod']
    }

    # Convert the extracted data into a DataFrame
    input_data = pd.DataFrame ([sample])

    # Make a churn prediction using the trained model
    prediction = model.predict (input_data).tolist ()[0]

    # Map prediction result to a human-readable label
    prediction_label = "churn" if prediction == 1 else "not churn"

    # Return the prediction as a JSON response
    return jsonify ({'Prediction': prediction_label})

# Define an endpoint to predict churn for a batch of customers
@cust_churn_predictor_api.post ('/v1/customerbatch')
def predict_churn_batch ():
    # Get the uploaded CSV file from the request
    file = request.files ['file']

    # Read the file into a DataFrame
    input_data = pd.read_csv (file)

    # Make predictions for the batch data and convert raw predictions into a readable format
    predictions = [
        'Churn' if x == 1
        else "Not Churn"
        for x in model.predict (input_data.drop ("customerID",axis=1)).tolist ()
    ]

    cust_id_list = input_data.customerID.values.tolist ()
    output_dict = dict(zip (cust_id_list, predictions))

    return output_dict

# Run the Flask app in debug mode
if __name__ == '__main__':
    cust_churn_predictor_api.run (debug=True)