text_plain_output_1.png image_output_1.png
import numpy as np

import pandas as pd

import seaborn as sns

import six

import warnings

import warnings
warnings.filterwarnings('ignore')
import sys, joblib
import six
sys.modules['sklearn.externals.six'] = six
sys.modules['sklearn.externals.joblib'] = joblib
import numpy as np
import pandas as pd
import re
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_context('talk', font_scale=0.65, rc={'grid.linewidth': 5})
pd.set_option('display.max_columns', 300)
pd.set_option('display.max_rows', 400)
from sklearn.linear_model import LogisticRegression, LinearRegression, LassoCV, Lasso, Ridge, LogisticRegressionCV
from sklearn.feature_selection import RFE
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA, IncrementalPCA
from sklearn.model_selection import GridSearchCV, cross_val_score, KFold, StratifiedKFold, RandomizedSearchCV
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score, precision_score, recall_score
from sklearn.metrics import precision_recall_curve, roc_auc_score, roc_curve
from imblearn.over_sampling import SMOTE, RandomOverSampler, ADASYN
from sklearn.preprocessing import StandardScaler, MinMaxScaler, QuantileTransformer
from scipy.stats import skew
from fancyimpute import IterativeImputer, KNN
from sklearn.impute import IterativeImputer
from sklearn.impute import KNNImputer
from sklearn.naive_bayes import MultinomialNB, BernoulliNB, GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, AdaBoostClassifier
from sklearn.svm import SVC
from xgboost import XGBClassifier
from statsmodels.stats.outliers_influence import variance_inflation_factor
from sklearn.feature_selection import RFE
import statsmodels.api as sm

telecom = pd.read_csv('../input/telecom-churn-dataset/telecom_churn_data.csv')

def calnullpercentage(df):
    missing_num = df[df.columns].isna().sum().sort_values(ascending=False)
    missing_perc = (df[df.columns].isna().sum() / len(df) * 100).sort_values(ascending=False)
    missing = pd.concat([missing_num, missing_perc], keys=['Total', 'Percentage'], axis=1)
    missing = missing[missing['Percentage'] > 0]
    return missing

telecom['tot_rech_amt_data_6'] = telecom['total_rech_data_6'] * telecom['av_rech_amt_data_6']
telecom['tot_rech_amt_data_7'] = telecom['total_rech_data_7'] * telecom['av_rech_amt_data_7']
telecom['tot_amt_6'] = telecom[['total_rech_amt_6', 'tot_rech_amt_data_6']].sum(axis=1)
telecom['tot_amt_7'] = telecom[['total_rech_amt_7', 'tot_rech_amt_data_7']].sum(axis=1)
telecom['avg_amt_6_7'] = telecom[['tot_amt_6', 'tot_amt_7']].mean(axis=1)
telecom = telecom.loc[telecom['avg_amt_6_7'] >= np.percentile(telecom['avg_amt_6_7'], 70)]
telecom.shape

catg = []
for col in telecom.columns:
    if len(telecom[col].unique()) == 2 | 3:
        catg.append(col)
telecom[catg] = telecom[catg].apply(lambda x: x.astype('object'))
col_tmp = ['total_rech_num_6', 'total_rech_num_7', 'total_rech_num_8', 'total_rech_num_9', 'total_rech_data_6', 'total_rech_data_7', 'total_rech_data_8', 'total_rech_data_9']
telecom[col_tmp] = telecom[col_tmp].apply(lambda x: x.astype('object'))

telecom.drop(['tot_rech_amt_data_6', 'tot_rech_amt_data_7', 'tot_rech_amt_data_8', 'tot_rech_amt_data_9'], inplace=True, axis=1)

telecom.drop(telecom.filter(regex='_9|sep', axis=1).columns, axis=1, inplace=True)

pd.DataFrame(round(telecom['churn'].value_counts(normalize=True) * 100, 2))