pip install pygal
import numpy as np 
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt 
import plotly as plot
import pygal as py
import squarify as sq
import missingno as msg
import sklearn
import warnings
warnings.filterwarnings("ignore")
%matplotlib inline 
df=pd.read_csv("../input/internet-service-churn/internet_service_churn.csv",engine="python",encoding="utf-8 ")
df
df.head(10)
df.shape
df.dtypes
df["churn"].dtype
df_cols=df.columns.tolist()
for i in df.columns.tolist():
    print(i)
df.describe().transpose()
df.isnull().sum()
df.info()
msg.heatmap(df);plt.show()
df.drop('id',axis='columns', inplace=True)
df
list_sub1=df[["is_tv_subscriber","is_movie_package_subscriber"]].sum()
list_sub1
print ("How many are not tv subscriber :" , df[df["is_tv_subscriber"]==0].count()["churn"])
print ("How many are not movie_package_subscriber :" , df[df["is_movie_package_subscriber"]==0].count()["churn"])
list_sub1.plot.bar()
# create data
x = ["is_tv_subscriber","is_movie_package_subscriber"]
y1 = [df[df["is_tv_subscriber"]==1].count()["is_tv_subscriber"],df[df["is_tv_subscriber"]==0].count()["is_tv_subscriber"]]
y2 = [df[df["is_movie_package_subscriber"]==1].count()["is_movie_package_subscriber"],df[df["is_movie_package_subscriber"]==0].count()["is_movie_package_subscriber"]]
  
plt.bar(x, y1, color='g')
plt.bar(x, y2, bottom=y1, color='b')
plt.legend(["subscriber","not a subscriber"])
plt.title("subscribers both for tv and mobile")
plt.show()
sns.boxplot(x="churn",y="subscription_age",data=df);plt.show() 
df["is_tv_subscriber"].count()
subscribers=["is_tv_subscriber","is_movie_package_subscriber"]
for i in subscribers:
    sns.categorical.boxenplot(x="churn",y="bill_avg",data=df,hue=i);plt.title("avg bill of "+ i)
    plt.show()
df.loc[:,["subscription_age","bill_avg"]]
list2=list(df.loc[:,["subscription_age","bill_avg"]].columns)
for i in list2:
    df[i].plot.hist(bins=20,title= str(i)+ " historgram")
    plt.show()
correlation=df.corr();correlation
sns.set_palette("Accent")
plt.figure(figsize=(15,8))
sns.heatmap(correlation,vmin=-1,vmax=1,annot=True);plt.show()
df2=df.loc[:,["subscription_age","bill_avg","reamining_contract","service_failure_count","download_avg","upload_avg"]]
for i in df2.columns.tolist():
    df[i].plot.box(patch_artist = True,notch ='True')
    plt.title("boxplot for "+ str(i))
    plt.show()
    print([df[i].quantile(0.25),df[i].quantile(0.5),df[i].quantile(0.75),df[i].quantile(1)])
sns.set_style("darkgrid")
sns.countplot(x="download_over_limit",hue="churn",data=df);plt.show()
df=df.dropna()
sns.pairplot(df,hue="churn");plt.show() 
"""
# Feature Selection
"""
"""
# univariate selection
"""
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
df["subscription_age"]=[abs(i) for i in df["subscription_age"]]
y=df["churn"]
x=df.loc[:,["is_tv_subscriber","is_movie_package_subscriber","subscription_age","bill_avg","reamining_contract","service_failure_count","download_avg","upload_avg","download_over_limit"]]
bestcolumns=SelectKBest(score_func=chi2,k="all")
fit=bestcolumns.fit(x,y)
df_scores=pd.DataFrame(fit.scores_)
dfcolumns=pd.DataFrame(x.columns)
featureScores=pd.concat([dfcolumns,df_scores],axis=1)
featureScores.columns=["Features","scores"]
featureScores
X_kbest = bestcolumns.fit_transform(x, y)
print('Original number of features:', x.shape[1])
print('Reduced number of features:', X_kbest.shape[1])
featureScores.plot.bar(x="Features",y="scores") #using chi method for feature selection
plt.show()
#result doesn't show well . so,we used another method
"""
# Filter method for the feature selection using information gain
"""
from sklearn.feature_selection import mutual_info_classif
important_features =mutual_info_classif(x,y)
feat_importances=pd.Series(important_features,df.columns[0:len(df.columns)-1])
feat_importances
feat_importances.plot.barh(color="teal");plt.show()
# Import libraries
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
# Normalize feature vector
X2 = StandardScaler().fit_transform(x)
x_train, x_test, y_train, y_test = train_test_split(X2, y, test_size = 0.30, random_state = 0)
clf = RandomForestClassifier(n_estimators=100, random_state=0)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
y_pred

plt.figure(num=None, figsize=(10,8), dpi=80, facecolor='firebrick', edgecolor='pink')

feat_importances = pd.Series(clf.feature_importances_, index= x.columns)

feat_importances.nlargest(10).plot(kind='barh')
"""
Most important features or columns are:"subscription_age","bill_avg","reamining_contract","download_avg","upload_avg"
"""
df.reset_index()
from sklearn.tree import DecisionTreeClassifier
df2=df.loc[:,["subscription_age","bill_avg","reamining_contract","download_avg","upload_avg","churn"]]
df2=df2.reset_index();df2
a=df2.loc[:,["subscription_age","bill_avg","reamining_contract","download_avg","upload_avg"]]
y=df2["churn"]
#Using the train_test_split to create train and test sets.
a_train, a_test, y_train, y_test = train_test_split(a, y, random_state = 50, test_size = 0.25)
a
clf = DecisionTreeClassifier(criterion = 'entropy')
#Training the decision tree classifier. 
clf.fit(a_train, y_train)
y_pred=clf.predict(a_train)
y_pred
from sklearn.metrics import accuracy_score
print('Accuracy Score on train data: ', accuracy_score(y_true=y_train, y_pred=clf.predict(a_train)))
print('Accuracy Score on test data: ', accuracy_score(y_true=y_test, y_pred=clf.predict(a_test)))
clf.predict([[11.94,32,1.38,69.4,4.0]])
# Creating some predictions.
from sklearn.model_selection import cross_val_predict
y_train_pred = cross_val_predict(clf, a_train, y_train, cv=3)


# Constructing the confusion matrix.
from sklearn.metrics import confusion_matrix
matrix=confusion_matrix(y_train, y_train_pred)
matrix
confusion_matrix(y_test,clf.predict(a_test))
sns.heatmap(confusion_matrix(y_train, y_train_pred),annot=True);
plt.title("Confusion matrix for train data");plt.show()
print("accuracy using confusion matrix for train data =" , (matrix[0,0]+matrix[1,1])*100/(matrix[0,0]+matrix[1,1]+matrix[0,1]+matrix[1,0]))
matrix2=confusion_matrix(y_test,clf.predict(a_test))

print("accuracy using confusion matrix for test data =" , (matrix2[0,0]+matrix2[1,1])*100/(matrix2[0,0]+matrix2[1,1]+matrix2[0,1]+matrix2[1,0]))
pip install pydotplus
#create tree for decision tree classifier
from io import StringIO  
from IPython.display import Image as image  
from sklearn.tree import export_graphviz
import pydotplus
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
image(graph.create_png())