<jupyter_start><jupyter_text>Terlihat bahwa tipe data setiap kolom bernilai numerik dan tidak ada missing data.<jupyter_code>#Get column names
df.columns
#Changing column names
df.columns = ['account_length', 'vmail_message', 'day_mins', 'eve_mins', 'night_mins',
       'intl_mins', 'custserv_calls', 'churn', 'intl_plan', 'day_calls',
       'day_charge', 'eve_calls', 'eve_charge', 'night_calls', 'night_charge',
       'intl_calls', 'intl_charge']

df.head(10)
#Labeling y and x as features
y = df.churn
lists = ['churn']
x = df.drop(lists, axis = 1)
x.head(10)
# Plotting customer churn
ax = sns.countplot(y, label = "Count")
no,yes = y.value_counts()
print('Numbers of staying customer: ', no)
print('Numbers of leaving customer: ', yes)
#Correlation map
f,ax = plt.subplots(figsize=(18, 18))
sns.heatmap(x.corr(), annot=True, linewidths=.5, fmt= '.1f',ax=ax)<jupyter_output><empty_output><jupyter_text>Dari tabel korelasi, telihat bahwa day_mins berkorelasi dengan day_charge, eve_mins berkorelasi dengan eve_charge, night_mins berkorelasi dengan night_charge, dan intl_mins berkorelasi dengan intl_charge. Dengan demikian dipilih salah satu di antara dua feature yang saling berkorelasi yaitu day_charge, eve_charge, night_charge, dan intl_charge.<jupyter_code>#New drop list
drop_list1 = ['day_mins', 'eve_mins', 'night_mins','intl_mins']
x_1 = x.drop(drop_list1, axis = 1)
x_1.head()
#Removing correlated features from data frame
df = df.drop(drop_list1, axis = 1)
numerical_features1 = ['account_length', 'vmail_message', 'custserv_calls', 'intl_plan']

fig, ax = plt.subplots(1, 4, figsize=(20, 4))
df[df.churn == 0][numerical_features1].hist(bins=30, color="blue", alpha=0.5, ax=ax)
df[df.churn == 1][numerical_features1].hist(bins=30, color="red", alpha=0.5, ax=ax)
numerical_features2 = ['day_calls', 'eve_calls', 'night_calls', 'intl_calls']

fig, ax = plt.subplots(1, 4, figsize=(20, 4))
df[df.churn == 0][numerical_features2].hist(bins=30, color="blue", alpha=0.5, ax=ax)
df[df.churn == 1][numerical_features2].hist(bins=30, color="red", alpha=0.5, ax=ax)
numerical_features3 = ['day_charge', 'eve_charge', 'night_charge', 'intl_charge']

fig, ax = plt.subplots(1, 4, figsize=(20, 4))
df[df.churn == 0][numerical_features3].hist(bins=30, color="blue", alpha=0.5, ax=ax)
df[df.churn == 1][numerical_features3].hist(bins=30, color="red", alpha=0.5, ax=ax)
#Split train and test dataset
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x_1,y, test_size=0.33, random_state=54)
#Comparing methods
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression as LR
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
import warnings
warnings.filterwarnings("ignore")

# Test options and evaluation metric
seed = 7
scoring = 'accuracy'

# Spot Check Algorithms
models = []
models.append(('LR', LR()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC()))
models.append(('RF', RandomForestClassifier()))
# evaluate each model in turn
results = []
names = []
for name, model in models:
	kfold = model_selection.KFold(n_splits=10, random_state=seed)
	cv_results = model_selection.cross_val_score(model, x_train, y_train, cv=kfold, scoring=scoring)
	results.append(cv_results)
	names.append(name)
	msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
	print(msg)
    

# Make predictions on validation dataset
rfmodel = RandomForestClassifier()
rfmodel = rfmodel.fit(x_train, y_train)
predictions = rfmodel.predict(x_test)
print('Accuracy: ', accuracy_score(y_test, predictions))
cm = confusion_matrix(y_test, predictions)
sns.heatmap(cm, annot = True, fmt = 'd')
# Feature Importances
importances = rfmodel.feature_importances_
std = np.std([tree.feature_importances_ for tree in rfmodel.estimators_],
             axis=0)
indices = np.argsort(importances)[::-1]

# Print the feature ranking
print("Feature ranking:")

for f in range(x_train.shape[1]):
    print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))

# Plot the feature importances of the forest

plt.figure(1, figsize=(14, 13))
plt.title("Feature importances")
plt.bar(range(x_train.shape[1]), importances[indices],
       color="g", yerr=std[indices], align="center")
plt.xticks(range(x_train.shape[1]), x_train.columns[indices],rotation=90)
plt.xlim([-1, x_train.shape[1]])
plt.show()<jupyter_output>Feature ranking:
1. feature 5 (0.223445)
2. feature 7 (0.124862)
3. feature 2 (0.112646)
4. feature 3 (0.088568)
5. feature 11 (0.076936)
6. feature 9 (0.060958)
7. feature 4 (0.058462)
8. feature 6 (0.058249)
9. feature 0 (0.053863)
10. feature 8 (0.052035)
11. feature 10 (0.047379)
12. feature 1 (0.042596)
{'fraction_non_alphanumeric': 0.143184421534937, 'fraction_numerical': 0.03990072546773578, 'mean_word_length': 6.5381294964028775, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '597237', 'license': 'Apache-2.0', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1913, 'n_words': 455, 'path': 'data-challenge-astra-digital/Astra Digital - Churn Analysis.ipynb', 'repo_name': 'lppratama/data-science-practice', 'size': '5238'}