# 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())

"""
Explanation: Exercise 02
Estimate a classifier to predict churn
Problem Formulation
Customer 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.
Data
Dataset 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.
End of explanation
"""



# 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)

"""
Explanation: Exercise 02.1
Split the training set in two sets with 70% and 30% of the data, respectively. 

Partir la base de datos es dos partes de 70%
End of explanation
"""



# Insert code here
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression()
clf.fit(X_train, Y_train)

"""
Explanation: Exercise 02.2
Train a logistic regression using the 70% set

Entrenar una regresion logistica usando la particion del 70%
End of explanation
"""


# 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()

"""
Explanation: Exercise 02.3
a) 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%.
End of explanation
"""


