<jupyter_start><jupyter_text>Fraud_Detection_Using_ADASYN_OVERSAMPLINGI am able to achieve the following accuracies in the validation data. These results can be further improved by reducing the parameter, number of frauds used to create features from category items. I have used a threshold of 100.* Logistic Regression :         Validation Accuracy: 70.0%, ROC_AUC_Score: 70.0%                * Random Forest :         Validation Accuracy: 98.9%, ROC_AUC_Score: 98.9%                * Linear Support Vector Machine :         Validation Accuracy: 51.0%, ROC_AUC_Score: 51.1%                * K Nearest Neighbors :         Validation Accuracy: 86.7%, ROC_AUC_Score: 86.7%                * Extra Trees Classifer :         Validation Accuracy: 99.2%, ROC_AUC_Score: 99.2%<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression, Ridge, Lasso, ElasticNet
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn import svm, neighbors
from sklearn.naive_bayes import GaussianNB
from imblearn.over_sampling import SMOTE, ADASYN
from sklearn.metrics import roc_auc_score
from sklearn.metrics import confusion_matrix
import itertools
% matplotlib inline<jupyter_output>C:\Users\kenneth\Anaconda3\envs\gpu\lib\site-packages\sklearn\ensemble\weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.
  from numpy.core.umath_tests import inner1d<jupyter_text>Loading Training Transactions Data<jupyter_code>tr_tr = pd.read_csv('data/train_transaction.csv', index_col='TransactionID')
print('Rows :', tr_tr.shape[0],'  Columns : ',tr_tr.shape[1] )
tr_tr.tail()
print('Memory Usage : ', (tr_tr.memory_usage(deep=True).sum()/1024).round(0))
tr_tr.tail()
tr_id = pd.read_csv('data/train_identity.csv', index_col='TransactionID')
print(tr_id.shape)
tr_id.tail()
tr = tr_tr.join(tr_id)
tr['data']='train'
print(tr.shape)
tr.head()
del tr_tr
del tr_id
te_tr = pd.read_csv('data/test_transaction.csv', index_col='TransactionID')
print(te_tr.shape)
te_tr.tail()
te_id = pd.read_csv('data/test_identity.csv', index_col='TransactionID')
print(te_id.shape)
te_id.tail()
te = te_tr.join(te_id)
te['data']='test'
te['isFraud']=2
print(te.shape)
te.head()
del te_tr
del te_id
tr.isFraud.describe()
tr.isFraud.value_counts().plot(kind='bar')
tr.isFraud.value_counts()
f, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(12,4))
ax1.hist(tr.TransactionAmt[tr.isFraud == 1], bins = 10)
ax1.set_title('Fraud Transactions ='+str(tr.isFraud.value_counts()[1]))
ax2.hist(tr.TransactionAmt[tr.isFraud == 0], bins = 10)
ax2.set_title('Normal Transactions ='+str(tr.isFraud.value_counts()[0]))
plt.xlabel('Amount ($)')
plt.ylabel('Number of Transactions')
plt.yscale('log')
plt.show()
sns.distplot(tr['TransactionAmt'], color='red')
sns.pairplot(tr[['TransactionAmt','isFraud']], hue='isFraud')
df = pd.concat([tr,te], sort=False)
print(df.shape)
df.head()
del tr
del te<jupyter_output><empty_output><jupyter_text>Make new category for items in Objects with A Fraud Count of more than 100<jupyter_code>fraud_threshold = 100

def map_categories(*args):
    columns = [col for col in args]
    for column in columns:
        if column == index:
            return 1
        else:
            return 0
new_categories = []
for i in df.columns:
    if i != 'data':
        if df[i].dtypes == str('object'):
            fraud_count = df[df.isFraud==1][i].value_counts(dropna=False)
            for index, value in fraud_count.items():
                if value>fraud_threshold:
                    df[(str(i)+'_'+str(index))]=list(map(map_categories, df[i]))
                    new_categories.append((str(i)+'_'+str(index)))
    #             else:
    #                 tr[(str(i)+'_'+str('other'))]=list(map(map_categories, tr[i]))
    #                 new_tr_categories.append((str(i)+'_'+str('other')))
            df.drop([i], axis=1, inplace=True)
print(new_categories)
print(df.shape)
df.head()
df.isna().any().mean()
df.fillna(0, inplace=True)
df.isna().any().mean()
X = df[df['data'] == 'train'].drop(['isFraud','data'], axis=1)
y = df[df['data'] == 'train']['isFraud']
X_predict = df[df['data'] == 'test'].drop(['isFraud','data'], axis=1)
print(X.shape, y.shape, X_predict.shape)<jupyter_output>(590540, 546) (590540,) (506691, 546)<jupyter_text>Oversampling using ADASYN<jupyter_code>ada = ADASYN(random_state=91)
X_sampled,y_sampled = ada.fit_sample(X,y)
#fraudlent records in original data
y.value_counts()
#fraudlent records in oversampled data is is almost equal to normal data
np.bincount(y_sampled)
X_train, X_test, y_train, y_test = train_test_split(X_sampled,y_sampled,test_size=0.3)
class_names = ['FRAUD', 'NORMAL']


def plot_confusion_matrix(cm, classes,normalize=False,title='Confusion matrix',cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.ylabel('Ground Truth')
    plt.xlabel('Predicted label')
    plt.tight_layout()
    plt.show()<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>lr = LogisticRegression(solver='lbfgs')
clf_lr = lr.fit(X_train, y_train)
confidence_lr=clf_lr.score(X_test, y_test)
print('Accuracy on Validation Data : ', confidence_lr.round(2)*100,'%')
test_prediction = clf_lr.predict(X_test)
print('ROC_AUC_SCORE ; ', roc_auc_score(y_test, test_prediction).round(3)*100,'%')
cnf_matrix = confusion_matrix(y_test, test_prediction)
plot_confusion_matrix(cnf_matrix, classes=class_names, title='Confusion Matrix')
prediction_lr = clf_lr.predict(X_predict)
test = df[df['data'] == 'test']
del df
test['prediction_lr'] = prediction_lr
test.prediction_lr.value_counts()
test.prediction_lr.to_csv('adLogistic_Regression_Prediction.csv')<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>rfor=RandomForestClassifier()
clf_rfor = rfor.fit(X_train, y_train)
confidence_rfor=clf_rfor.score(X_test, y_test)
print('Accuracy on Validation Data : ', confidence_rfor.round(3)*100,'%')
test_prediction = clf_rfor.predict(X_test)
print('ROC_AUC_SCORE ; ', roc_auc_score(y_test, test_prediction).round(3)*100,'%')
cnf_matrix = confusion_matrix(y_test, test_prediction)
plot_confusion_matrix(cnf_matrix, classes=class_names, title='Confusion Matrix')
prediction_rfor = clf_rfor.predict(X_predict)
test['prediction_rfor'] = prediction_rfor
test.prediction_rfor.value_counts()
test.prediction_rfor.to_csv('adRandom_Forest_Prediction.csv')<jupyter_output><empty_output><jupyter_text>Linear Support Vector Machine Algorithm<jupyter_code>lsvc=svm.LinearSVC()
clf_lsvc=lsvc.fit(X_train, y_train)
confidence_lsvc=clf_lsvc.score(X_test, y_test)
print('Accuracy on Validation Data : ', confidence_lsvc.round(3)*100,'%')
test_prediction = clf_lsvc.predict(X_test)
print('ROC_AUC_SCORE ; ', roc_auc_score(y_test, test_prediction).round(3)*100,'%')
cnf_matrix = confusion_matrix(y_test, test_prediction)
plot_confusion_matrix(cnf_matrix, classes=class_names, title='Confusion Matrix')<jupyter_output>ROC_AUC_SCORE ;  51.1 %
[[  5149 165881]
 [  1209 168466]]<jupyter_text>K-Nearest Neighbors Algorithm<jupyter_code>knn=neighbors.KNeighborsClassifier(n_neighbors=10, n_jobs=-1)
clf_knn=knn.fit(X_train, y_train)
confidence_knn=clf_knn.score(X_test, y_test)
print('Accuracy on Validation Data : ', confidence_knn.round(3)*100,'%')
test_prediction = clf_knn.predict(X_test)
print('ROC_AUC_SCORE ; ', roc_auc_score(y_test, test_prediction).round(3)*100,'%')
cnf_matrix = confusion_matrix(y_test, test_prediction)
plot_confusion_matrix(cnf_matrix, classes=class_names, title='Confusion Matrix')<jupyter_output>ROC_AUC_SCORE ;  86.7 %
[[136578  34452]
 [ 10938 158737]]<jupyter_text>Extra Trees Classifier<jupyter_code>etc=ExtraTreesClassifier()
clf_etc = etc.fit(X_train, y_train)
confidence_etc=clf_etc.score(X_test, y_test)
print('Accuracy on Validation Data : ', confidence_etc.round(3)*100,'%')
test_prediction = clf_etc.predict(X_test)
print('ROC_AUC_SCORE ; ', roc_auc_score(y_test, test_prediction).round(3)*100,'%')
cnf_matrix = confusion_matrix(y_test, test_prediction)
plot_confusion_matrix(cnf_matrix, classes=class_names, title='Confusion Matrix')
prediction_etc = clf_etc.predict(X_predict)
test['prediction_etc'] = prediction_etc
test.prediction_etc.value_counts()
test.prediction_etc.to_csv('adExtra_Trees_Prediction.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13730150156638218, 'fraction_numerical': 0.021929350761585826, 'mean_word_length': 5.797356828193832, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '11742', 'license': 'MIT', 'n_tokens_mistral': 3587, 'n_tokens_neox': 3371, 'n_words': 602, 'path': 'Fraud_Detection_Using_ADASYN_OVERSAMPLING.ipynb', 'repo_name': 'kenNicholaus/Fraud_Detection', 'size': '9257'}