<jupyter_start><jupyter_text>Customer Churn Decision TreeNote: on top of `numpy`, `pandas`, `matplotlib`, you need two more packages for this assignment: `scikit-learn` (for building decision tree and other machine learning models) and `pydotplus` (for visualizing the decision trees), you want to add those packages to your `requirements.txt` file and install them before running this notebook.<jupyter_code># load packages
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


%matplotlib inline<jupyter_output><empty_output><jupyter_text>Question 1- what's the shape of the dataframe created from the csv file?  >Shape is (3333,7)- how many features and what are their datatype?>7 featuresstate is object  account length is int64  international plan is object  total day minutes is float64  total day calls is int64  customer service calls  is int64  churn is bool- are there any null values in the dataset?>There are zero null values- create a histogram for the target feature and calculate the churn rate- if you want to make a "naive" prediction on customer churn rate, what's the rule? > My naive prediction is that a customer is not gonna churn(leave) because the churn rate is only about 14.49%<jupyter_code># load the csv file
df_train=pd.read_csv('customer-churn-example-simple.csv')

# check the basic info for the df
display(df_train.info())
display(df_train.shape)
df_train.head()
# change churn to string from boolean for histogram and decision tree modeling
df_train['churn'] = df_train['churn'].apply(str)
# create a histogram for churn
df_train['churn'].hist(bins=50)
# calculate the churn rate
counts=df_train['churn'].value_counts()
counts[1]/(counts[1]+counts[0])*100<jupyter_output><empty_output><jupyter_text>TaskBuild a decision tree:- use only 'account length', 'total day calls', 'customer service calls' features- use entropy as the criterion and set the max depth for the tree to 4 - visualize the tree as shown in the sample code provided in the class. - explain the first node (root) of your generated tree (example each line in the node - what they mean)- make predictions for three customers based on the sample input found in the comments below and explain your predictions - do they make sense?- by looking at the decision tree image and change one value for customer 2 to flip the prediction and explain why<jupyter_code># build your DT using 'account length', 'total day calls', 'customer service calls' features
X=df_train[['account length','total day calls','customer service calls']]
Y=df_train['churn']
print(X.shape)
print(Y.shape)
from sklearn.tree import DecisionTreeClassifier

# train a DT model
tree_clf=DecisionTreeClassifier(criterion='entropy',max_depth=4)
tree_clf.fit(X,Y)
# load the packages for visulizing the tree

from IPython.display import Image 
from sklearn import tree 
import pydotplus
# get feature and class names for visulization
print(X.columns.values.tolist())
print(Y.unique().tolist())
cls_names=['churn(left)' if i=='True' else'stayed'for i in Y.unique().tolist()]
cls_names
# Create DOT data and visualize the tree
dot_data=tree.export_graphviz(tree_clf,feature_names=X.columns.values.tolist(),class_names=cls_names,)
graph=pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())<jupyter_output><empty_output><jupyter_text>>the tree is splitted based on the true or false of the condition customer service calls<=3.5  The current entropy of the node is = 0.597  The total number of samples in this node is 3333  How many samples belong to each class, 483 belong to class churn True (left) and 2850 belong to class churn False (stayed)  This shows the prediction of this node, because its node 0 its the naive prediction based on the churn rate of all customers, and the prediciton is that they stay<jupyter_code># make predictions for the following customers
# customer 1 who open the account 25 days ago with total day calls of 100 and called customer support 5 times
# customer 2 who open the account 125 days ago with total day calls of 150 and called customer support 0 times
# customer 3 who open the account 600 days ago with total day calls of 1150 and called customer support 8 times
customer1=tree_clf.predict([[25,100,5]])
customer2=tree_clf.predict([[125,150,0]])
customer3=tree_clf.predict([[600,1150,8]])
print(customer1,customer2,customer3)<jupyter_output>['True'] ['False'] ['True']<jupyter_text>>The first and thirs customer will churn (leave ) based on the prediciton the second one will stay.They make sense based on the modell, but its a bird weird that customer 3 will leave after 600 days thats a long time, but based on the modell it makes sense<jupyter_code>customer2_after_change=tree_clf.predict([[125,150,3.51]])
print(customer2_after_change)<jupyter_output>['True']
{'fraction_non_alphanumeric': 0.07785431016489251, 'fraction_numerical': 0.023794614902943018, 'mean_word_length': 5.8262108262108265, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '313194', 'license': 'MIT', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1349, 'n_words': 608, 'path': 'churn-dt-basics-finished.ipynb', 'repo_name': 'TheodorMariusWienert/misy467-assignments', 'size': '4791'}