<jupyter_start><jupyter_text>Exercise 02Estimate a classifier to predict churn  Problem FormulationCustomer Churn: losing/attrition of the customers from the company. Especially, the industries that the user acquisition is costly, it is crucially important for one company to reduce and ideally make the customer churn to 0 to sustain their recurring revenue. If you consider customer retention is always cheaper than customer acquisition and generally depends on the data of the user(usage of the service or product), it poses a great/exciting/hard problem for machine learning. DataDataset is from a telecom service provider where they have the service usage(international plan, voicemail plan, usage in daytime, usage in evenings and nights and so on) and basic demographic information(state and area code) of the user. For labels, I have a single data point whether the customer is churned out or not.<jupyter_code># Download the dataset
from urllib import request
response = request.urlopen('https://raw.githubusercontent.com/EricChiang/churn/master/data/churn.csv')
raw_data = response.read().decode('utf-8')
# Convert to numpy
import numpy as np
data = []
for line in raw_data.splitlines()[1:]:
    words = line.split(',')
    data.append(words)
data = np.array(data)
column_names = raw_data.splitlines()[0].split(',')
n_obs = data.shape[0]
print(column_names)
print(data.shape)
data[:2]
# Select only the numeric features
X = data[:, [1,2,6,7,8,9,10]].astype(np.float)
# Convert bools to floats
X_ = (data[:, [4,5]] == 'no').astype(np.float)
X = np.hstack((X, X_))
Y = (data[:, -1] == 'True.').astype(np.int)
X[:2]
print('Number of churn cases ', Y.sum())<jupyter_output>Number of churn cases  483<jupyter_text>Exercise 02.1Split the training set in two sets with 70% and 30% of the data, respectively. ---Partir la base de datos es dos partes de 70%<jupyter_code># Insert code here
random_sample = np.random.rand(n_obs)
X_train, X_test = X[random_sample<0.6], X[random_sample>=0.6]
Y_train, Y_test = Y[random_sample<0.6], Y[random_sample>=0.6]

print(Y_train.shape, Y_test.shape)<jupyter_output>(2034,) (1299,)<jupyter_text>Exercise 02.2Train a logistic regression using the 70% set---Entrenar una regresion logistica usando la particion del 70%<jupyter_code># Insert code here
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression()
clf.fit(X_train, Y_train)<jupyter_output><empty_output><jupyter_text>Exercise 02.3a) Create a confusion matrix using the prediction on the 30% set.b) Estimate the accuracy of the model in the 30% set---a) Estimar la matriz de confusion en la base del  30%.b) Calcular el poder de prediccion usando la base del 30%.<jupyter_code># Insert code here
y_pred = clf.predict(X_test)

from sklearn.metrics import confusion_matrix
confusion_matrix(Y_test, y_pred)
(Y_test == y_pred).mean()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10326842837273992, 'fraction_numerical': 0.02294853963838665, 'mean_word_length': 5.675174013921113, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '272123', 'license': 'MIT', 'n_tokens_mistral': 965, 'n_tokens_neox': 906, 'n_words': 325, 'path': 'exercises/02-Churn model-solution.ipynb', 'repo_name': 'S7evenrg/PracticalMachineLearningClass', 'size': '2876'}