<jupyter_start><jupyter_text>Class: 0 - valid transaction, 1 - fraud<jupyter_code>df["Class"].value_counts()
df_class = df["Class"].value_counts().reset_index()
df_class['name'] = df_class['index'].map({0: "Valid", 1: 'Fraud'})
px.bar(df_class, x='name' , y='Class', width=800, height=600, title="Valid and fraud transactions")
Image(filename='img/bar_plot.png')<jupyter_output><empty_output><jupyter_text>Preparing the dataset<jupyter_code># Functions are responsible for preparing the data for the model

RANDOM_SEED = 42

def preporcess_data(data: object) -> object:
    data = data.drop("Time", axis=1).copy()
    data['Amount'] = StandardScaler().fit_transform(data['Amount'].values.reshape(-1, 1))
    return data

def get_train_test_data(processed_data: object) -> list:
    target = processed_data['Class'].copy()
    data = processed_data.drop(['Class'], axis=1)
    
    X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.25, random_state=RANDOM_SEED)
    y_train = y_train.values.reshape(-1, 1)
    y_test = y_test.values.reshape(-1, 1)
    
    return X_train, X_test, y_train, y_test
    
def get_only_valid_transations(X_train: object) -> object:
    ''' gets only valid transaction '''
    X_train['class'] = y_train
    X_train = X_train[X_train['class'] == 0]
    X_train = X_train.drop(['class'], axis=1)
    
    return X_train<jupyter_output><empty_output><jupyter_text>Autodecoders model<jupyter_code># prepares the dataset
data = preporcess_data(df)
X_train, X_test, y_train, y_test = get_train_test_data(data)
X_train = get_only_valid_transations(X_train)
# creates the model
autocoder_model = Autocoder([25, 2, 2, 25])

# trains the model
autocoder_model.train_model(X_train)
# predicts label 
auto_test_results = autocoder_model.predict(X_test)

# crates a pandas Series object
auto_test_results = pd.Series(auto_test_results)
# creates a graph object to draw charts
graphs = Graphs()

# draws a histogram
graphs.plot_hist(auto_test_results)
# finds outliers
auto_test_datasets = X_test.copy()
auto_test_datasets['score'] = auto_test_results.values.reshape(-1, 1)

# data more than 13 are split as fraud
auto_test_datasets['cluster'] = np.where(auto_test_datasets['score']<13, 0, 1)

# counts valid and fraud transatcions
auto_test_datasets['cluster'].value_counts()
# shows mean value for each cluster and column
auto_test_datasets.groupby('cluster').mean()
result = round(accuracy_score(y_test, auto_test_datasets['cluster']), 4)
print(f"Model accuracy: {result}")
graphs.plot_confusion_matrix(y_test, auto_test_datasets['cluster'])
Image(filename='img/cm_a.png') 
print(classification_report(y_test, auto_test_datasets['cluster']))<jupyter_output>precision    recall  f1-score   support

           0       1.00      0.99      0.99     71089
           1       0.08      0.70      0.14       113

    accuracy                           0.99     71202
   macro avg       0.54      0.84      0.57     71202
weighted avg       1.00      0.99      0.99     71202<jupyter_text>This model have very good results, but we have less value to recall than precision for Fraud. Also we classified 943 valid as fraud. Additional we classified 34 fraud as valid, we should try to minimize this error and maximize value for recall.  Neural networks<jupyter_code># prepares the dataset
data = preporcess_data(df)
X_train, X_test, y_train, y_test = get_train_test_data(data)

# creates Neural Networks model
model = Model(X_train, X_test, y_train, y_test)

# defines layers for the model
model.define_model()
# trains the model
model.train_model(save_path='/home/lukas/deep_learning/fraud_detector')
graphs = Graphs()

# gets metrics from history object
metrics = model.get_metrics()
graphs.plot_loss_curves(pd.DataFrame(metrics.history), save_path='/home/lukas/deep_learning/fraud_detector')
# loads the weights for the lowest loss function
model.load_weights('best_model.h5')

# predicts labels
y_pred = model.predict()

# counts a model accuracy
result = round( accuracy_score(y_test, y_pred), 4)
print(f"Model accuracy: {result}")
graphs.plot_confusion_matrix(y_test, y_pred)
Image("img/cm_n.png")
print(classification_report(y_test, y_pred))<jupyter_output>precision    recall  f1-score   support

           0       1.00      1.00      1.00     71089
           1       0.84      0.79      0.81       113

    accuracy                           1.00     71202
   macro avg       0.92      0.89      0.91     71202
weighted avg       1.00      1.00      1.00     71202
{'fraction_non_alphanumeric': 0.1090667257814232, 'fraction_numerical': 0.03923741964087785, 'mean_word_length': 4.007769145394007, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '603856', 'license': 'Apache-2.0', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1525, 'n_words': 360, 'path': 'fraud_detector.ipynb', 'repo_name': 'LukaszMikol/Fraud-detection', 'size': '4511'}