<jupyter_start><jupyter_text>From the above information we know that there are no Null Values.<jupyter_code>plt.figure(figsize=(13, 10))
sns.heatmap(df.corr(), annot=True);
pd.crosstab(df['type'], df['isFraud'])<jupyter_output><empty_output><jupyter_text>As we see from the above table that fraud happens only if the type is 'Cash_out' or 'Transfer'.<jupyter_code># count number of fraud and not fraud data
print(df.isFraud.value_counts())
sns.countplot(data=df, x='isFraud')
plt.ylabel('Count')
plt.show()<jupyter_output>0    6354407
1       8213
Name: isFraud, dtype: int64<jupyter_text>Above bar chart, number of fraud is 8213<jupyter_code># Count number of data point in each type of transaction
print(df.type.value_counts())
df.type.value_counts().plot(kind='bar')
plt.show()<jupyter_output>CASH_OUT    2237500
PAYMENT     2151495
CASH_IN     1399284
TRANSFER     532909
DEBIT         41432
Name: type, dtype: int64<jupyter_text><jupyter_code>pd.crosstab(df.isFraud,df.isFlaggedFraud)
#Groupby type
#df.groupby('type')['isFraud','isFlaggedFraud'].sum()
plt.figure(figsize=(10, 5))
sns.boxplot(x='type', y=np.log10(df['amount'] + 1), hue='isFraud', data=df);<jupyter_output><empty_output><jupyter_text>From the above boxplot, we can see most number of fraud look place while "Tranfer" and "Cash out".<jupyter_code># Amount Vs number of transaction
fraud = df[df['isFraud']==1]
normal = df[df['isFraud']==0]
f, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
f.suptitle('Amount per transaction by class')
bins = 50
ax1.hist(fraud.amount, bins = bins)
ax1.set_title('Fraud')
ax2.hist(normal.amount, bins = bins)
ax2.set_title('Non Fraud')
plt.xlabel('Amount ($)')
plt.ylabel('Number of Transactions')
# plt.xlim((0, 20000))
plt.yscale('log')
plt.show();<jupyter_output><empty_output><jupyter_text>From the about graph, there are less number of transaction amount in fraud compared to non fraud data<jupyter_code>df['hour'] = df['step'].apply(lambda x: x % 24)

fig, ax = plt.subplots(1, 2, figsize=(10, 5))
df['hour'].value_counts().sort_index().plot(kind='bar', ax=ax[0])
df[df['isFraud']==1]['hour'].value_counts().sort_index().plot(kind='bar', ax=ax[1])
ax[0].set_title('Non-Fraud Cases')
ax[1].set_title('Fraud Cases');<jupyter_output><empty_output><jupyter_text>From the above graph we see that most number of fraud take place during the night time maybe because of different time zone.<jupyter_code>#importing packages
from sklearn.model_selection import train_test_split, learning_curve
from sklearn.metrics import average_precision_score, f1_score,confusion_matrix


import seaborn as sns
import matplotlib.pyplot as plt

from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report
droplist=['isFlaggedFraud','type','nameDest','nameOrig']<jupyter_output><empty_output><jupyter_text>Removed the 'isFlaggedFraud','type','nameDest','nameOrig' from our data set for taking main features.<jupyter_code>data=df
MLData=data.drop(labels=droplist,axis=1)
X=MLData.drop('isFraud',1)
Y=MLData.isFraud
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.3,random_state=42, shuffle=False)<jupyter_output><empty_output><jupyter_text>the key feature of our dataset is isFraud, and the dividing the dataset in to training and testing 70%-30% respectively.<jupyter_code>from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)<jupyter_output><empty_output><jupyter_text>Using standardScalar to remove the mean and scaling to unit variance<jupyter_code>from sklearn.tree import DecisionTreeClassifier

clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)

pred_y = clf.predict(X_test)
print(confusion_matrix(y_test, pred_y))
dfc = confusion_matrix(y_test, pred_y)
print(classification_report(y_test, pred_y))
accuracy_score(y_test, pred_y)
sns.heatmap(dfc, cmap="Blues", annot=True,annot_kws={"size": 16})
from sklearn.ensemble import RandomForestClassifier
r_clf=RandomForestClassifier()
r_clf.fit(X_train,y_train)
pred_y_r = r_clf.predict(X_test)
print(confusion_matrix(y_test, pred_y_r))
rfc = confusion_matrix(y_test, pred_y_r)
print(classification_report(y_test, pred_y_r))
accuracy_score(y_test, pred_y_r)
sns.heatmap(rfc, cmap="Blues", annot=True,annot_kws={"size": 16})
 
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state = 0)
classifier.fit(X_train, y_train)
from sklearn.metrics import confusion_matrix, accuracy_score
import seaborn
import matplotlib.pyplot as plt
y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test, y_pred)
print(cm)
print(classification_report(y_test, y_pred))
accuracy_score(y_test, y_pred)
sns.heatmap(cm, cmap="Blues", annot=True,annot_kws={"size": 16})
print('Decision tree',accuracy_score(y_test, pred_y))
print('Random Forest',accuracy_score(y_test, pred_y_r))
print('Logistic regression',accuracy_score(y_test, y_pred))<jupyter_output>Decision tree 0.9777811656204519
Random Forest 0.992326536342995
Logistic regression 0.9985084760680348
{'fraction_non_alphanumeric': 0.12440915106825487, 'fraction_numerical': 0.03044053696350917, 'mean_word_length': 8.412811387900355, 'pattern_counts': {'":': 3, '<': 38, '<?xml version=': 0, '>': 38, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '363596', 'license': 'Unlicense', 'n_tokens_mistral': 2084, 'n_tokens_neox': 1925, 'n_words': 406, 'path': 'Synthetic Financial Datasets For Fraud Detection.ipynb', 'repo_name': 'vatsal-gandhi/Synthetic-Financial-Datasets-For-Fraud-Detection--Case-study1', 'size': '5289'}