<jupyter_start><jupyter_text>Exercise 10- Fraud Detection Dataset from Microsoft Azure: [data](http://gallery.cortanaintelligence.com/Experiment/8e9fe4e03b8b4c65b9ca947c72b8e463)Fraud detection is one of the earliest industrial applications of data mining and machine learning. Fraud detection is typically handled as a binary classification problem, but the class population is unbalanced because instances of fraud are usually very rare compared to the overall volume of transactions. Moreover, when fraudulent transactions are discovered, the business typically takes measures to block the accounts from transacting to prevent further losses.<jupyter_code>import pandas as pd
import zipfile
with zipfile.ZipFile('../datasets/fraud_detection.csv.zip', 'r') as z:
    f = z.open('15_fraud_detection.csv')
    data = pd.io.parsers.read_table(f, index_col=0, sep=',')
data.head()
X = data.drop(['Label'], axis=1)
y = data['Label']
y.value_counts(normalize=True)<jupyter_output><empty_output><jupyter_text>Exercice 10.1Estimate a Logistic Regression, GaussianNB, K-nearest neighbors and a Decision Tree **Classifiers**Evaluate using the following metrics:* Accuracy* F1-Score* F_Beta-Score (Beta=10)Comment about the resultsCombine the classifiers and comment<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
models = {'lr': LogisticRegression(),
          'dt': DecisionTreeClassifier(),
          'nb': GaussianNB(),
          'nn': KNeighborsClassifier()}

from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=2)
# Train all the models
for model in models.keys():
    models[model].fit(X_train, y_train)
# predict test for each model
y_pred = pd.DataFrame(index=X_test.index, columns=models.keys())
for model in models.keys():
    y_pred[model] = models[model].predict(X_test)
y_pred
y_pred_ensemble1 = (y_pred.mean(axis=1) > 0.5).astype(int)
y_pred_ensemble1.mean()
from sklearn.metrics import accuracy_score, f1_score, recall_score, precision_score
stats = {'acc': accuracy_score,
         'f1': f1_score,
         'rec': recall_score,
         'pre': precision_score}
res = pd.DataFrame(index=models.keys(), columns=stats.keys())
for model in models.keys():
    for stat in stats.keys():
        res.loc[model, stat] = stats[stat](y_test, y_pred[model])
res
res.loc['ensemble1'] = 0
for stat in stats.keys():
    res.loc['ensemble1', stat] = stats[stat](y_test, y_pred_ensemble1)
res<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1050245561012467, 'fraction_numerical': 0.01662259161314696, 'mean_word_length': 5.720812182741117, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '39369', 'license': 'MIT', 'n_tokens_mistral': 871, 'n_tokens_neox': 831, 'n_words': 232, 'path': 'exercises/10-fraud_ensemble_bagging.ipynb', 'repo_name': 'Torroledo/ML_RiskManagement', 'size': '2647'}