0% found this document useful (0 votes)
23 views8 pages

Bis Micro Project

This document presents a micro project report on fraud detection in transactions, detailing the implementation using Python and pandas for data manipulation. It includes sample transaction data, defines thresholds for detecting fraud, and outlines a function to identify potential fraudulent activities based on transaction amounts, locations, and timing. The project concludes with a demonstration of detecting fraudulent transactions and outputting the results.

Uploaded by

kajalshrma7703
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

Bis Micro Project

This document presents a micro project report on fraud detection in transactions, detailing the implementation using Python and pandas for data manipulation. It includes sample transaction data, defines thresholds for detecting fraud, and outlines a function to identify potential fraudulent activities based on transaction amounts, locations, and timing. The project concludes with a demonstration of detecting fraudulent transactions and outputting the results.

Uploaded by

kajalshrma7703
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

BIS MICRO PROJECT

AN MICRO PROJRCT REPORT ON


FRAUD DETECTION IN TRANSACTIONS

Submitted by

KASHISH MAHOVIA (216300307006)


KARINA SHARMA (226300307120 )
PRUTHA PANCHAL (226300307078)

Student of

Diploma In Engineering
in

computer Department
Step-by-Step Explanation
 Importing Libraries:

o import pandas as pd

o The code begins by importing the pandas library, which is essential fo data
manipulation and analysis in Python.

 Sample Transaction Data:

data = {

'transaction_id': [1, 2, 3, 4, 5],

'amount': [100, 1500, 200, 3000, 50],

'location': ['USA', 'USA', 'Canada', 'USA', 'Canada'],

'time': ['2023-10-01 10:00', '2023-10-01 10:05', '2023-10-01 10:10', '2023-10-01 10:15', '2023-10-01 10:20'],

'user_id': [101, 101, 102, 101, 102]

}
transaction_id amount location time user_id
1 100 USA 2023-10-01 10:00 101
2 1500 USA 2023-10-01 10:05 101
3 200 Canada 2023-10-01 10:10 102
4 3000 USA 2023-10-01 10:15 101
5 50 Canada 2023-10-01 10:20 102
A dictionary named data is created, containing sample transaction information.
Each key represents a column in the dataset:
o transaction_id: Unique identifier for each transaction.

o amount: The monetary value of the transaction.

o location: The geographical location where the transaction occurred.

o time: The timestamp of the transaction.

o user_id: Identifier for the user who made the transaction.

 CREATING A DATAFRAME:
o transactions = pd.DataFrame(data)

o The data dictionary is converted into a pandas DataFrame


called transactions.

o This structure allows for easy manipulation and analysis of the transaction data.

 DEFINING THRESHOLDS FOR FRAUD DETECTION:


amount_threshold = 2000 # Example threshold for high transaction amount

location_change_threshold = 10 # Example threshold for location changes

suspicious_locations = ['USA', 'Canada'] # Example of known locations

 Several thresholds and criteria are defined for detecting


potential fraud:
o amount_threshold: A threshold of 2000 is set to flag transactions that exceed this
amoun as potentially fraudulent.
o location_change_threshold: This variable is defined but not used in the
current implementation. It could be intended for future enhancements.
o suspicious_locations: A list of known locations (USA and Canada) is defined.
Transactions from locations outside this list may be flagged
 FUNCTION TO DETECT FRAUD:
def detect_fraud(transactions):

fraud_cases = []

o A function named detect_fraud is defined, which takes the transactions

DataFrame as input and initializes an empty list fraud_cases to store any detected fraud
cases.

 ITERATING THROUGH TRANSACTIONS:


o for index, row in transactions.iterrows():

o The function iterates over each transaction in the DataFrame using iterrows(), which returns
both the index and the row data.

 CHECKING FOR HIGH TRANSACTION AMOUNT:


if row['amount'] > amount_threshold:

fraud_cases.append((row['transaction_id'], 'High transaction amount'))

o For each transaction, it checks if the amount exceeds the defined amount_threshold. If it
does, a tuple containing the transaction_id and the reason ('High transaction amount') is
appended to the fraud_cases list.

 CHECKING FOR UNUSUAL LOCATIONS


if row['location'] not in suspicious_locations:

fraud_cases.append((row['transaction_id'], 'Unusual location'))

o The code checks if the location of the transaction is not in


the suspicious_locations list. If it is not, it appends a tuple to fraud_cases indicating
an 'Unusual location'.

 CHECKING FOR MULTIPLE TRANSACTIONS IN A SHORT TIME FRAME


recent_transactions = transactions[(transactions['user_id'] == row['user_id']) &

(transactions['time'] < row['time'])]

if not recent_transactions.empty:
time_diff = pd.to_datetime(row['time']) - pd.to_datetime(recent_transactions['time'].max())

if time_diff.total_seconds() < 500: # 5 minutes

fraud_cases.append((row['transaction_id'], 'Multiple transactions in short time'))

FRAUD DETECTION IN TRANSACTIONS


Code:

import pandas as pd

# Sample transaction data

data = {

'transaction_id': [1, 2, 3, 4, 5],

'amount': [100, 1500, 200, 3000, 50],

'location': ['USA', 'USA', 'Canada', 'USA', 'Canada'],

'time': ['2023-10-01 10:00', '2023-10-01 10:05', '2023-10-01 10:10', '2023-10-01 10:15', '2023-10-01
10:20'],

'user_id': [101, 101, 102, 101, 102]

# Create a DataFrame

transactions = pd.DataFrame(data)

# Define thresholds for fraud detection

amount_threshold = 1000 # Example threshold for high transaction amount

location_change_threshold = 1 # Example threshold for location changes

suspicious_locations = ['USA', 'Canada'] # Example of known locations

# Function to detect fraud

def detect_fraud(transactions):

fraud_cases = []

for index, row in transactions.iterrows():


# Check for high transaction amount

if row['amount'] > amount_threshold:

fraud_cases.append((row['transaction_id'], 'High transaction amount'))

# Check for unusual location (assuming user has a known location)

if row['location'] not in suspicious_locations:

fraud_cases.append((row['transaction_id'], 'Unusual location'))

# Check for multiple transactions in a short time frame

recent_transactions = transactions[(transactions['user_id'] == row['user_id']) &

(transactions['time'] < row['time'])]

if not recent_transactions.empty:

time_diff = pd.to_datetime(row['time']) - pd.to_datetime(recent_transactions['time'].max())

if time_diff.total_seconds() < 300: # 5 minutes

fraud_cases.append((row['transaction_id'], 'Multiple transactions in short time'))

return fraud_cases

# Detect fraud in transactions

fraudulent_transactions = detect_fraud(transactions)

# Output results

if fraudulent_transactions:

print("Fraudulent Transactions Detected:")

for transaction in fraudulent_transactions:

print(f"Transaction ID: {transaction[0]}, Reason: {transaction[1]}")

else:

print("No fraudulent transactions detected.")


project output

You might also like