Note - Double Click to Copy Code Contact Us!

A neural network based database mining system for credit card fraud detection

Tech Doubility
A neural network based database mining system for credit card fraud detection



  1. import numpy as np
  2. import pandas as pd
  3. from sklearn.model_selection import train_test_split
  4. from sklearn.preprocessing import StandardScaler
  5. from tensorflow.keras.models import Sequential
  6. from tensorflow.keras.layers import Dense, Dropout
  7.  
  8. # Load the dataset
  9. data = pd.read_csv("credit_card_data.csv")
  10.  
  11. # Preprocessing the data
  12. scaler = StandardScaler()
  13. data["Amount"] = scaler.fit_transform(data["Amount"].values.reshape(-1, 1))
  14.  
  15. # Splitting the data into training and testing sets
  16. X = data.drop("Class", axis=1).values
  17. y = data["Class"].values
  18. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
  19.  
  20. # Building the neural network model
  21. model = Sequential()
  22. model.add(Dense(128, input_dim=X_train.shape[1], activation="relu"))
  23. model.add(Dropout(0.5))
  24. model.add(Dense(64, activation="relu"))
  25. model.add(Dropout(0.5))
  26. model.add(Dense(1, activation="sigmoid"))
  27.  
  28. # Compiling the model
  29. model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
  30.  
  31. # Training the model
  32. model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test))
  33.  
  34. # Evaluating the model
  35. _, accuracy = model.evaluate(X_test, y_test)
  36. print("Accuracy:", accuracy)
In this example, the code assumes that you have a CSV file named "credit_card_data.csv" containing your credit card transaction data, with the "Class" column denoting whether the transaction is fraudulent or not (1 for fraud, 0 for non-fraud). Make sure to replace the file path with the actual path to your dataset.

The code begins by loading the dataset and performing basic preprocessing, such as scaling the "Amount" feature using StandardScaler. Then, the data is split into training and testing sets using train_test_split from scikit-learn.

Next, a sequential neural network model is built using the Keras API. The model consists of three dense layers with ReLU activation and dropout layers for regularization. The last layer uses a sigmoid activation function to output a probability between 0 and 1 indicating the likelihood of fraud.

The model is compiled with binary cross-entropy loss and the Adam optimizer. Then, it is trained on the training data using the fit function. The number of epochs and batch size can be adjusted according to your dataset and computational resources.

Finally, the model is evaluated on the testing data, and the accuracy is printed.

Please note that this is a simplified example, and for a real-world credit card fraud detection system, you may need to perform more sophisticated data preprocessing, feature engineering, and hyperparameter tuning. Additionally, you might consider using more advanced techniques such as anomaly detection algorithms or incorporating other features and models to enhance the performance

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.