<jupyter_start><jupyter_text>Homework– Churn Prediction**02/19/2019****<NAME>**  Data Preprocessing<jupyter_code># Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Week3_Mocked_Customer_Data_With_Missing.csv')
dataset.head(5)
# Plot histograms of “age” group by “churn”
dataset['age'].hist(by=dataset['churn_flag'])
# separete independent and dependent variables 
X = dataset.iloc[:, 2:-1].values
y = dataset.iloc[:, 1].values

# Taking care of missing data
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values = np.nan, strategy = 'most_frequent')
imputer.fit(X[:, :])
X[:, :] = imputer.transform(X[:, :])
# t-test of “tot_bill” regarding “churn”
from scipy.stats import ttest_ind
tot_bill = X[:, 5]
t_test, p_value = ttest_ind(tot_bill, y)
print('t-test index = {}\np-value = {}'.format(t_test, p_value))
# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 0] = labelencoder_X_1.fit_transform(X[:, 0]) # gender
labelencoder_X_2 = LabelEncoder()
X[:, 1] = labelencoder_X_2.fit_transform(X[:, 1]) # marrital
labelencoder_X_3 = LabelEncoder()
X[:, 8] = labelencoder_X_3.fit_transform(X[:, 8].astype(str)) # fortune
onehotencoder = OneHotEncoder(categorical_features = [0, 1, 8])
X = onehotencoder.fit_transform(X).toarray()
# Avoid dummy variable trap
X = np.delete(X, 2, axis=1) # delete dummy variable for marrital 
X = np.delete(X, 4, axis=1) # delete dummy variable for fortune
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test) 
# Applying PCA
from sklearn.decomposition import PCA
pca = PCA(n_components = 6) # components with variance larger than 0.05
X_train = pca.fit_transform(X_train)
X_test = pca.transform(X_test)
explained_variance = pca.explained_variance_ratio_
print(explained_variance.round(4))<jupyter_output>[0.2197 0.1112 0.0839 0.083  0.0539 0.0527]<jupyter_text>Traning and Testing Logistic Model<jupyter_code># Fitting Logistic Regression to the Training set
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state = 0)
classifier.fit(X_train, y_train)
# Predicting the Test set results
y_pred = classifier.predict(X_test)<jupyter_output><empty_output><jupyter_text>Evaluation of LR ModelFrom confusion matrix, we could see that we predict most of y on the test set correctly (TP and TN), which means our model is well performed.<jupyter_code># Making the Confusion Matrix 
from sklearn.metrics import confusion_matrix 
cm = confusion_matrix(y_test, y_pred)
print("confusion matrix: ")
print(cm)
# Compute precision score
from sklearn.metrics import precision_score
precision = precision_score(y_test, y_pred, pos_label=1)
print("precision = %0.3f" % precision)
# Compute recall score
from sklearn.metrics import recall_score
recall = recall_score(y_test, y_pred, pos_label=1)
print("recall = %0.3f" % recall)
# Compute F1 score 
from sklearn.metrics import f1_score
F1 = f1_score(y_test, y_pred, pos_label=1)
print("F1 = %0.3F" % F1)<jupyter_output>F1 = 0.928<jupyter_text>Precision-Recall Curve and ROC CurveWe can see that  AUC of both Precision-Recall curve and ROC curve  lager than 0.9, which shows our logistic model is well performed.<jupyter_code># Compute Precision-Recall and plot curve
from sklearn.metrics import precision_recall_curve

precision, recall, thresholds = precision_recall_curve(y_test, y_pred, pos_label=1)
area = auc(recall, precision)
print("Area Under Curve: %0.2f" % area)

plt.clf()
plt.plot(recall, precision, 'b-', label='Precision-Recall curve')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('Precision-Recall curve: AUC=%0.2f' % area)
plt.legend(loc="lower left")
plt.show()
# Compute ROC curve AUC
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
fpr, tpr, threshold = roc_curve(y_test, y_pred, pos_label=1)

roc_auc = auc(fpr, tpr)
print('ROC AUC = {}'.format(roc_auc))
# Plot the ROC curve
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12073378839590444, 'fraction_numerical': 0.0253839590443686, 'mean_word_length': 6.338028169014085, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '581415', 'license': 'MIT', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1653, 'n_words': 441, 'path': 'homework3-ChurnPrediction/homework3-ChurnPrediction.ipynb', 'repo_name': 'bondxue/FRE9733-big-data', 'size': '4694'}