Skip to main content

Machine Learning — Logistic Regression with Python

# Importing packages import pandas as pd # data processing import numpy as np # working with arrays import itertools import matplotlib.pyplot as plt # visualizations from matplotlib import rcParams # plot size customization from termcolor import colored as cl # text customization from sklearn.model_selection import train_test_split # splitting the data from sklearn.linear_model import LogisticRegression # model algorithm from sklearn.preprocessing import StandardScaler # data normalization from sklearn.metrics import jaccard_similarity_score as jss # evaluation metric from sklearn.metrics import precision_score # evaluation metric from sklearn.metrics import classification_report # evaluation metric from sklearn.metrics import confusion_matrix # evaluation metric from sklearn.metrics import log_loss # evaluation metric rcParams['figure.figsize'] = (20, 10) # Importing the data and EDA df = pd.read_csv('tele_customer_data.csv') df.drop(['Unnamed: 0', 'loglong', 'callwait', 'logtoll', 'voice', 'ebill', 'lninc', 'confer', 'internet', 'cardten', 'pager', 'longten', 'wiremon', 'equipmon', 'tollten', 'wireless', 'callcard', 'cardmon', 'tollmon'], axis = 1, inplace = True) for i in df.columns: df[i] = df[i].astype(int) df.head() df.describe() df.info() # Splitting the data X_var = np.asarray(df[['tenure', 'age', 'income', 'ed', 'employ', 'longmon', 'custcat']]) y_var = np.asarray(df['churn']) print(cl('X_var samples : ', attrs = ['bold']), X_var[:5]) print(cl('y_var samples : ', attrs = ['bold']), y_var[:5]) X_var = StandardScaler().fit(X_var).transform(X_var) print(cl(X_var[:5], attrs = ['bold'])) X_train, X_test, y_train, y_test = train_test_split(X_var, y_var, test_size = 0.3, random_state = 4) print(cl('X_train samples : ', attrs = ['bold']), X_train[:5]) print(cl('X_test samples : ', attrs = ['bold']), X_test[:5]) print(cl('y_train samples : ', attrs = ['bold']), y_train[:10]) print(cl('y_test samples : ', attrs = ['bold']), y_test[:10]) # Modelling lr = LogisticRegression(C = 0.1, solver = 'liblinear') lr.fit(X_train,y_train) yhat = lr.predict(X_test) yhat_prob = lr.predict_proba(X_test) print(cl('yhat samples : ', attrs = ['bold']), yhat[:10]) print(cl('yhat_prob samples : ', attrs = ['bold']), yhat_prob[:10]) # Evaluation # 1. Jaccard Index print(cl('Jaccard Similarity Score of our model is {}'.format(jss(y_test, yhat).round(2)), attrs = ['bold'])) # 2. Precision Score print(cl('Precision Score of our model is {}'.format(precision_score(y_test, yhat).round(2)), attrs = ['bold'])) # 3. Log loss print(cl('Log Loss of our model is {}'.format(log_loss(y_test, yhat).round(2)), attrs = ['bold'])) # 4. Classificaton report print(cl(classification_report(y_test, yhat), attrs = ['bold'])) def plot_confusion_matrix(cm, classes,normalize = False, title = 'Confusion matrix', cmap = plt.cm.Blues): if normalize: cm = cm.astype(float) / cm.sum(axis=1)[:, np.newaxis] plt.imshow(cm, interpolation = 'nearest', cmap = cmap) plt.title(title, fontsize = 22) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation = 45, fontsize = 13) plt.yticks(tick_marks, classes, fontsize = 13) 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', fontsize = 15, color = 'white' if cm[i, j] > thresh else 'black') plt.tight_layout() plt.ylabel('True label', fontsize = 16) plt.xlabel('Predicted label', fontsize = 16) # 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') plt.savefig('confusion_matrix.png')

Comments

Popular posts from this blog

Rethinking the way we build on the cloud: Layering the Cloud

To help, you can split your cloud architecture into three layers: Visible Volatile Persistant Visible This is the layer between the cloud and the rest of the world. It is mainly public DNS entries, load balancers, VPN connections etc. etc. These things are fairly static and consistent. If you have a website you will want the same A Name records pointing at your static IP. Things in the visible layer rarely change or if they do they are for very deliberate reasons. Volatile This is where the majority of your servers are. It’s called volatile because it tends to change a lot. The known state (how many servers, what versions of which software they run etc.) are changing frequently, perhaps even several times per hour. Even in a traditional data centre your servers are being upgraded, having security patches applied, new versions of software deployed, extra servers added etc. On the cloud this is even more volatile when you use patterns such as the Phoenix server and m...

Real-Time Talk: Windows 10 IoT Core Background Tasks and ASP.NET Core Web Apps

Display useful information from your Windows 10 IoT Core application in an ASP.NET Core web app, essential for integrating IoT data into a solution. Windows 10 IoT background task talk with a web application using WebSockets. Problems As my path to this solution has been troublesome, I am listing here the main problems I faced so my dear readers have a better idea of dead-end streets along the way: I was not able to make the ASP.NET Core web application run under a Windows 10 IoT background service. I found no information about when or if it will be supported in the near future. ASP.NET MVC and ASP.NET Core have different SignalR implementations. I was not able to make a SignalR client for .NET Core work with SignalR hosted on a web application. I was able to make things work by directly using a WebSocket. It’s not as nice a solution as I had in my mind, but it works until things get better. Making the Background Task and Web Application Talk I worked out sim...

DANGER (STILL) LURKS IN THE INTERNET OF THINGS (IOT)

All of the media, both “mainstream” and “tech”, has gushed over all of the new appliances and devices that are now in the category of what we would call the Internet of Things.  Items like home security,  home lighting, and refrigerators, to name a few. There are many advantages to having connected appliances and devices, Threats that can and will be exploited if unsuspecting users don’t secure them.   Two “layers” of security : The first layer of offering  is a security API that will provide [a way] to easily do a virtual patch, to prevent a remote attack, for example . . . the third layer is cloud: IoT cannot do anything without the cloud.  Most data is sent to the cloud and you will need to have proper protection and make sure the cloud is always available. In both situation Users are vulnerable, mostly due to their own apathy.  Users often either don’t know how to patch their own machines (and in this case, devices) or have glanced...