<jupyter_start><jupyter_text>Phase 1- Business requirements detailed in phase 0 Exploratory Data Analysis (EDA) - customer support  Main objective with this notebook -> starting point to identify what drives customer churn- Load the dataset and and analyse some features- Evaluate some historical information and characteristics of the data- Run one machine learning algorithm (Ensemble: Random Forest in this example) and evaluate the current data- Plot the most important features / characteristics that drives the decision to churn or not   Dataset - the dataset used in this process can be accessed trought IBM website below    https://www.ibm.com/communities/analytics/watson-analytics-blog/guide-to-sample-datasets/<jupyter_code>## Import libraries used in this notebook
import pandas as pd
import pandas_profiling as pp
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier 
df = pd.read_csv('../data/WA_Fn-UseC_-Telco-Customer-Churn.csv')
df.head(5).T<jupyter_output><empty_output><jupyter_text>Basic evaluation of the data<jupyter_code>## Profile data set
pp.ProfileReport(df)<jupyter_output><empty_output><jupyter_text>Prepare the data to run Random Forest Classifier     - and also drop the information customer_id  (will not be used)<jupyter_code>drop_items = ['customerID']

df.drop(drop_items, axis = 1, inplace = True)

# LabelEncoder
le = LabelEncoder()

# apply "le.fit_transform"
df = df.apply(le.fit_transform)

target = 'Churn'
features = df.columns.tolist()
features.remove(target)

X = df[features]
y = df[target]<jupyter_output><empty_output><jupyter_text>Run Random Forest and print the best score<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=12345, stratify=y)


## Import the random forest model.
from sklearn.ensemble import RandomForestClassifier 
## This line instantiates the model. 
fit_rf = RandomForestClassifier() 
## Fit the model on your training data.
fit_rf.fit(X_train, y_train) 
## And score it on your testing data.
fit_rf.score(X_test, y_test)


print('----------------------- The best score/accuracy is: ' + str(fit_rf.score(X_test, y_test)))
fit_rf.score(X_test, y_test)<jupyter_output>----------------------- The best score/accuracy is: 0.7771469127040455<jupyter_text>Plot Feature Importance<jupyter_code>import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

# Plot feature importance
feature_importances = pd.DataFrame({'features': X_train.columns, 
                            'importance': fit_rf.feature_importances_}).sort_values('importance', ascending=False)

ax = sns.barplot(x="importance", y="features", data=feature_importances)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10261930391101542, 'fraction_numerical': 0.010046645138141371, 'mean_word_length': 5.223214285714286, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '303421', 'license': 'MIT', 'n_tokens_mistral': 880, 'n_tokens_neox': 841, 'n_words': 252, 'path': 'Customer_Support/bin/Phase_1_Exploratory_Data_Analysis_and_ML_Random_Forest_using_Python.ipynb', 'repo_name': 'ThiagoBarsante/DataScience_projects', 'size': '2787'}