# 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]
n_obs

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
"""


X_70=X[0:2332,]
Y_70=Y[0:2332,]

X_30=X[2332:,]
Y_30=Y[2332:,]

# Insert code here


"""
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
"""


from sklearn.linear_model import LogisticRegression
from sklearn import linear_model

clf = linear_model.LogisticRegression()
clf.fit(X_70,Y_70)
print(clf.coef_)
print(clf.intercept_)




y_pred = clf.predict(X_70)
y_pred


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

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


from sklearn.linear_model import LogisticRegression
from sklearn import linear_model

clf = linear_model.LogisticRegression()
clf.fit(X_30,Y_30)
print(clf.coef_)
print(clf.intercept_)

from sklearn.metrics import confusion_matrix
y_pred = clf.predict(X_30)
cm = confusion_matrix(Y_30, y_pred)
np.set_printoptions(precision=2)
print(cm)



# Import libraries
%matplotlib inline
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib as mpl
cmap = mpl.colors.ListedColormap(['b', 'r', 'g'])

def plot_confusion_matrix(cm, title='Confusion matrix', cmap=plt.cm.Blues):
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

plt.figure()
plot_confusion_matrix(cm)

from sklearn.linear_model import LogisticRegression

from sklearn import cross_validation

errors=[]
n_samples=Y_30.shape
acc = (y_pred == Y_30).sum()
err = 1- acc / n_samples
errors.append(err)

errors = np.array(errors)
print(errors.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
End of explanation
"""


