<jupyter_start><jupyter_text>Customer churn with Logistic Regression<jupyter_code>import pandas as pd
import pylab as pl
import numpy as np
import scipy.optimize as opt
from sklearn import preprocessing
%matplotlib inline 
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>About the datasetWe will use a telecommunications dataset for predicting customer churn. This is a historical customer dataset where each row represents one customer. The data is relatively easy to understand, and you may uncover insights you can use immediately. Typically it is less expensive to keep customers than acquire new ones, so the focus of this analysis is to predict the customers who will stay with the company. This data set provides information to help you predict what behavior will help you to retain customers. You can analyze all relevant customer data and develop focused customer retention programs.The dataset includes information about:*   Customers who left within the last month – the column is called Churn*   Services that each customer has signed up for – phone, multiple lines, internet, online security, online backup, device protection, tech support, and streaming TV and movies*   Customer account information – how long they had been a customer, contract, payment method, paperless billing, monthly charges, and total charges*   Demographic info about customers – gender, age range, and if they have partners and dependents<jupyter_code>#Click here and press Shift+Enter
!wget -O ChurnData.csv https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-ML0101EN-SkillsNetwork/labs/Module%203/data/ChurnData.csv<jupyter_output>--2021-12-02 14:46:09--  https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-ML0101EN-SkillsNetwork/labs/Module%203/data/ChurnData.csv
Resolving cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud... 172.16.31.10
Connecting to cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud|172.16.31.10|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 35943 (35K) [text/csv]
Saving to: 'ChurnData.csv'

ChurnData.csv       100%[===================>]  35.10K  --.-KB/s    in 0.02s   

2021-12-02 14:46:10 (1.63 MB/s) - 'ChurnData.csv' saved [35943/35943]<jupyter_text>Load Data From CSV File<jupyter_code>churn_df = pd.read_csv("ChurnData.csv")
churn_df.head()<jupyter_output><empty_output><jupyter_text>Data pre-processing and selection<jupyter_code>churn_df = churn_df[['tenure', 'age', 'address', 'income', 'ed', 'employ', 'equip',   'callcard', 'wireless','churn']]
churn_df['churn'] = churn_df['churn'].astype('int')
churn_df.head()
# write your code here
print(churn_df.shape)
print(churn_df.columns)<jupyter_output>(200, 10)
Index(['tenure', 'age', 'address', 'income', 'ed', 'employ', 'equip',
       'callcard', 'wireless', 'churn'],
      dtype='object')<jupyter_text>Let's define X, and y for our dataset:<jupyter_code>X = np.asarray(churn_df[['tenure', 'age', 'address', 'income', 'ed', 'employ', 'equip']])
X[0:5]
y = np.asarray(churn_df['churn'])
y [0:5]<jupyter_output><empty_output><jupyter_text>Also, we normalize the dataset:<jupyter_code>from sklearn import preprocessing
X = preprocessing.StandardScaler().fit(X).transform(X)
X[0:5]<jupyter_output><empty_output><jupyter_text>Train/Test dataset<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=4)
print ('Train set:', X_train.shape,  y_train.shape)
print ('Test set:', X_test.shape,  y_test.shape)<jupyter_output>Train set: (160, 7) (160,)
Test set: (40, 7) (40,)<jupyter_text>Modeling (Logistic Regression with Scikit-learn)<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
LR = LogisticRegression(C=0.01, solver='liblinear').fit(X_train,y_train)
LR<jupyter_output><empty_output><jupyter_text>Now we can predict using our test set:<jupyter_code>yhat = LR.predict(X_test)
yhat<jupyter_output><empty_output><jupyter_text>**predict_proba**  returns estimates for all classes, ordered by the label of classes. So, the first column is the probability of class 0, P(Y=0|X), and second column is probability of class 1, P(Y=1|X):<jupyter_code>yhat_prob = LR.predict_proba(X_test)
yhat_prob[0:5]<jupyter_output><empty_output><jupyter_text>Evaluation  jaccard index<jupyter_code>from sklearn.metrics import jaccard_score
jaccard_score(y_test, yhat,pos_label=0)<jupyter_output><empty_output><jupyter_text>confusion matrixAnother way of looking at the accuracy of the classifier is to look at **confusion matrix**.<jupyter_code>from sklearn.metrics import classification_report, confusion_matrix
import itertools
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
print(confusion_matrix(y_test, yhat, labels=[1,0]))
# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test, yhat, labels=[1,0])
np.set_printoptions(precision=2)


# Plot non-normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=['churn=1','churn=0'],normalize= False,  title='Confusion matrix')
print (classification_report(y_test, yhat))

from sklearn.metrics import log_loss
log_loss(y_test, yhat_prob)
# write your code here
LR2 = LogisticRegression(C=0.01, solver='sag').fit(X_train,y_train)
yhat_prob2 = LR2.predict_proba(X_test)
print ("LogLoss: : %.2f" % log_loss(y_test, yhat_prob2))<jupyter_output>LogLoss: : 0.61
{'fraction_non_alphanumeric': 0.12001213776361705, 'fraction_numerical': 0.024427249279320284, 'mean_word_length': 5.356798457087753, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 47, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '255178', 'license': 'MIT', 'n_tokens_mistral': 2309, 'n_tokens_neox': 2163, 'n_words': 564, 'path': 'ML-models/11_Machine_learning-Logistic-Regression_Customer-churn.ipynb', 'repo_name': 'sbeniwal19/Data-Science-Projects', 'size': '6595'}